Package com.backblaze.b2.json
Class B2JsonBoundedByteArrayOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- com.backblaze.b2.json.B2JsonBoundedByteArrayOutputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
public class B2JsonBoundedByteArrayOutputStream extends java.io.OutputStream
A B2Json implementation of OutputStream that - stores stream content in a byte array - expands capacity when needed (typically doubles) - has an upper bound limit on output array size (max capacity) - throws IOException if the max capacity threshold is crossed. The last behavior is different from standard Java ByteArrayOutputStream Implementation where an OutOfMemoryError would be thrown. OutOfMemoryError is typically interpreted as JVM running out of heap space, and JVM could be killed if configured so. This may not be desired for Java applications configured with heap space much larger than 2 GB, and such applications may want to continue to run after catching this threshold-crossing exception. THREAD-SAFE
-
-
Field Summary
Fields Modifier and Type Field Description static int
SYSTEM_MAX_CAPACITY
-
Constructor Summary
Constructors Constructor Description B2JsonBoundedByteArrayOutputStream(int maxCapacity)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
getMaxCapacity()
returns the max capacity of output arrayint
getSize()
returns the current output array sizebyte[]
toByteArray()
returns a copy of the output byte arrayjava.lang.String
toString(java.lang.String charsetName)
makes a new String by decoding the bytes in output array using the specified charset.void
write(byte[] bytes, int offset, int length)
writes an array of bytes into the output arrayvoid
write(int i)
writes one byte into the output array
-
-
-
Field Detail
-
SYSTEM_MAX_CAPACITY
public static final int SYSTEM_MAX_CAPACITY
- See Also:
- Constant Field Values
-
-
Method Detail
-
write
public void write(int i) throws java.io.IOException
writes one byte into the output array- Specified by:
write
in classjava.io.OutputStream
- Parameters:
i
- one byte- Throws:
java.io.IOException
- if expanding capacity would cross the maxCapacity threshold
-
write
public void write(byte[] bytes, int offset, int length) throws java.io.IOException
writes an array of bytes into the output array- Overrides:
write
in classjava.io.OutputStream
- Parameters:
bytes
- input array of bytesoffset
- offset for the input arraylength
- number of bytes to write- Throws:
java.io.IOException
- if expanding capacity would cross the maxCapacity threshold
-
toString
public java.lang.String toString(java.lang.String charsetName) throws java.io.UnsupportedEncodingException
makes a new String by decoding the bytes in output array using the specified charset.- Parameters:
charsetName
- the charset to be used in decoding- Returns:
- the string representation of output bytes after decoding
- Throws:
java.io.UnsupportedEncodingException
- if the named charset is not supported
-
toByteArray
public byte[] toByteArray()
returns a copy of the output byte array- Returns:
- a copy of output buffer
-
getMaxCapacity
public int getMaxCapacity()
returns the max capacity of output array- Returns:
- the max capacity
-
getSize
public int getSize()
returns the current output array size- Returns:
- output output array size
-
-