Skip to content

Commit

Permalink
Improve uploading of binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Nov 9, 2021
1 parent 742130d commit c305504
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 52 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Proteus Changelog.
## Unreleased
### No issue

**Bump compiler version for JDK 17 support.**


[742130deaad3aae](https://github.com/noboomu/proteus/commit/742130deaad3aae) Joshua Bauer *2021-10-30 21:28:03*

**Cleanup OpenAPI module and add json spec support.**


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.sinistral.proteus.utilities;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -9,12 +11,14 @@
/**
* This class extends OutputStream and bypasses the default safety mechanisms to allow for greater performance.
*
* @author Joshua Bauer
* @since 1.0
* @author Joshua Bauer
* @since 1.0
*/

public class AsyncByteOutputStream extends OutputStream {

private static final Logger log = LoggerFactory.getLogger(AsyncByteOutputStream.class.getName());

/**
* The buffer where data is stored.
*/
Expand All @@ -30,20 +34,23 @@ public class AsyncByteOutputStream extends OutputStream {
* initially 32 bytes, though its size increases if necessary.
*/
public AsyncByteOutputStream() {
this(8196*4);

this(8196 * 4);
}

/**
* Creates a new byte array output stream, with a buffer capacity of
* the specified size, in bytes.
*
* @param size the initial size.
* @exception IllegalArgumentException if size is negative.
* @param size the initial size.
* @throws IllegalArgumentException if size is negative.
*/
public AsyncByteOutputStream(int size) {
if (size < 0) {

if (size < 0)
{
throw new IllegalArgumentException("Negative initial size: "
+ size);
+ size);
}
buf = new byte[size];
}
Expand All @@ -55,13 +62,15 @@ public AsyncByteOutputStream(int size) {
*
* @param minCapacity the desired minimum capacity
* @throws OutOfMemoryError if {@code minCapacity < 0}. This is
* interpreted as a request for the unsatisfiably large capacity
* {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}.
* interpreted as a request for the unsatisfiably large capacity
* {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}.
*/
private void ensureCapacity(int minCapacity) {
// overflow-conscious code
if (minCapacity - buf.length > 0)
{
grow(minCapacity);
}
}

/**
Expand All @@ -75,10 +84,15 @@ private void grow(int minCapacity) {
int oldCapacity = buf.length;
int newCapacity = oldCapacity << 1;
if (newCapacity - minCapacity < 0)
{
newCapacity = minCapacity;
if (newCapacity < 0) {
}
if (newCapacity < 0)
{
if (minCapacity < 0) // overflow
{
throw new OutOfMemoryError();
}
newCapacity = Integer.MAX_VALUE;
}
buf = Arrays.copyOf(buf, newCapacity);
Expand All @@ -87,9 +101,10 @@ private void grow(int minCapacity) {
/**
* Writes the specified byte to this byte array output stream.
*
* @param b the byte to be written.
* @param b the byte to be written.
*/
public void write(int b) {

ensureCapacity(count + 1);
buf[count] = (byte) b;
count += 1;
Expand All @@ -99,13 +114,15 @@ public void write(int b) {
* Writes <code>len</code> bytes from the specified byte array
* starting at offset <code>off</code> to this byte array output stream.
*
* @param b the data.
* @param off the start offset in the data.
* @param len the number of bytes to write.
* @param b the data.
* @param off the start offset in the data.
* @param len the number of bytes to write.
*/
public void write(byte b[], int off, int len) {

if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) - b.length > 0)) {
((off + len) - b.length > 0))
{
throw new IndexOutOfBoundsException();
}
ensureCapacity(count + len);
Expand All @@ -118,10 +135,11 @@ public void write(byte b[], int off, int len) {
* the specified output stream argument, as if by calling the output
* stream's write method using <code>out.write(buf, 0, count)</code>.
*
* @param out the output stream to which to write the data.
* @exception IOException if an I/O error occurs.
* @param out the output stream to which to write the data.
* @throws IOException if an I/O error occurs.
*/
public void writeTo(OutputStream out) throws IOException {

out.write(buf, 0, count);
}

Expand All @@ -130,10 +148,9 @@ public void writeTo(OutputStream out) throws IOException {
* stream to zero, so that all currently accumulated output in the
* output stream is discarded. The output stream can be used again,
* reusing the already allocated buffer space.
*
* @see java.io.ByteArrayInputStream#count
*/
public void reset() {

count = 0;
}

Expand All @@ -142,21 +159,24 @@ public void reset() {
* size of this output stream and the valid contents of the buffer
* have been copied into it.
*
* @return the current contents of this output stream, as a byte array.
* @see java.io.ByteArrayOutputStream#size()
* @return the current contents of this output stream, as a byte array.
* @see java.io.ByteArrayOutputStream#size()
*/
public byte toByteArray()[] {
return buf;

byte[] slice = new byte[count];
System.arraycopy(buf, 0, slice, 0, count);
return slice;
}

/**
* Returns the current size of the buffer.
*
* @return the value of the <code>count</code> field, which is the number
* of valid bytes in this output stream.
* @see java.io.ByteArrayOutputStream#count
* @return the value of the <code>count</code> field, which is the number
* of valid bytes in this output stream.
*/
public int size() {

return buf.length;
}

Expand All @@ -173,9 +193,10 @@ public int size() {
* required.
*
* @return String decoded from the buffer's contents.
* @since JDK1.1
* @since JDK1.1
*/
public String toString() {

return new String(buf, 0, count);
}

Expand All @@ -190,16 +211,16 @@ public String toString() {
* java.nio.charset.CharsetDecoder} class should be used when more control
* over the decoding process is required.
*
* @param charsetName the name of a supported
* {@linkplain java.nio.charset.Charset </code>charset<code>}
* @param charsetName the name of a supported
* {@linkplain java.nio.charset.Charset </code>charset<code>}
* @return String decoded from the buffer's contents.
* @exception UnsupportedEncodingException
* If the named charset is not supported
* @since JDK1.1
* @throws UnsupportedEncodingException If the named charset is not supported
* @since JDK1.1
*/
public String toString(String charsetName)
throws UnsupportedEncodingException
throws UnsupportedEncodingException
{

return new String(buf, 0, count, charsetName);
}

Expand All @@ -213,20 +234,20 @@ public String toString(String charsetName)
* c == (char)(((hibyte &amp; 0xff) &lt;&lt; 8) | (b &amp; 0xff))
* </pre></blockquote>
*
* @param hibyte the high byte of each resulting Unicode character.
* @return the current contents of the output stream, as a string.
* @see java.io.ByteArrayOutputStream#size()
* @see java.io.ByteArrayOutputStream#toString(String)
* @see java.io.ByteArrayOutputStream#toString()
* @deprecated This method does not properly convert bytes into characters.
* As of JDK&nbsp;1.1, the preferred way to do this is via the
* <code>toString(String enc)</code> method, which takes an encoding-name
* argument, or the <code>toString()</code> method, which uses the
* platform's default character encoding.
*
* @param hibyte the high byte of each resulting Unicode character.
* @return the current contents of the output stream, as a string.
* @see java.io.ByteArrayOutputStream#size()
* @see java.io.ByteArrayOutputStream#toString(String)
* @see java.io.ByteArrayOutputStream#toString()
*/
@Deprecated
public String toString(int hibyte) {

return new String(buf, hibyte, 0, count);
}

Expand All @@ -235,9 +256,9 @@ public String toString(int hibyte) {
* this class can be called after the stream has been closed without
* generating an <tt>IOException</tt>.
* <p>
*
*/
public void close() throws IOException {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,11 @@ public static void writeStreamToPath(InputStream inputStream, Path path) throws

}


public static ByteBuffer streamToBuffer(InputStream stream) throws IOException {

AsyncByteOutputStream buffer = new AsyncByteOutputStream(16384);

int nRead;
byte[] data = new byte[16384];
while ((nRead = stream.read(data, 0, data.length)) != -1)
{
buffer.write(data, 0, nRead);
}

buffer.flush();

return ByteBuffer.wrap(buffer.toByteArray());
final io.sinistral.proteus.utilities.AsyncByteOutputStream byteArrayOutputStream = new io.sinistral.proteus.utilities.AsyncByteOutputStream(65536);
stream.transferTo(byteArrayOutputStream);
return ByteBuffer.wrap(byteArrayOutputStream.toByteArray());
}

public static ByteBuffer readAllBytes(Path fp) throws IOException {
Expand Down

0 comments on commit c305504

Please sign in to comment.