Package emissary.util.io
Class FastStringBuffer
- java.lang.Object
-
- java.io.OutputStream
-
- emissary.util.io.FastStringBuffer
-
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
public class FastStringBuffer extends OutputStream
This buffer implementation attempts to improve file creation performance by preventing conversion from byte array to string and then back to byte array for writing to a stream. The append method here accepts byte arrays and strings. In the case of a byte array it simply copies the bytes. Very fast. In the case of strings it attempts to convert the string to a byte array once and save the results. In this way we avoid converting each time. If many constant string literals are passed to this class, it will be faster. This string buffer will now accept an output stream in addition which allows this string buffer to act as a buffered output stream.
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]
buffer
protected int
bytesWritten
protected int
curPos
protected static org.slf4j.Logger
logger
static int
MAX_CACHE_SIZE
protected String
myString
protected OutputStream
stream
-
Constructor Summary
Constructors Constructor Description FastStringBuffer()
FastStringBuffer(int initialSize)
FastStringBuffer(int initialSize, OutputStream stream)
FastStringBuffer(OutputStream stream)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description FastStringBuffer
append(byte[] a)
FastStringBuffer
append(byte[] a, int start, int length)
FastStringBuffer
append(int i)
FastStringBuffer
append(String s)
FastStringBuffer
append(String s, String charset)
FastStringBuffer
appendCls(String s)
Appends constant string literals only!!!!!FastStringBuffer
appendCls(String s, String charset)
Appends constant string literals only!!!!!FastStringBuffer
appendCr()
FastStringBuffer
appendCrLf()
FastStringBuffer
appendEscaped(String s)
FastStringBuffer
appendEscaped(String s, String charset)
FastStringBuffer
appendEscapedUtf8(byte[] data, String charset, int start, int end)
FastStringBuffer
appendUtf8(byte[] data, String charset)
Write UTF8 data to the output page buffer without specifying a start or end position, defaults to 0,-1FastStringBuffer
appendUtf8(byte[] data, String charset, int end)
Write UTF8 data to the output page buffer without specifying a start position, defaults to 0FastStringBuffer
appendUtf8(byte[] data, String charset, int start, int end)
Write UTF8 data to the output page buffer Pass in 0 and -1 for start and end to do the whole thingvoid
close()
void
flush()
byte[]
getBytes()
int
getBytesWritten()
int
getSize()
void
setLength(int len)
protected byte[]
stringToBytes(String s, String charset)
String
toString()
void
write(byte[] a)
void
write(byte[] a, int start, int length)
void
write(int b)
-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Field Detail
-
logger
protected static final org.slf4j.Logger logger
-
MAX_CACHE_SIZE
public static final int MAX_CACHE_SIZE
- See Also:
- Constant Field Values
-
curPos
protected int curPos
-
buffer
protected byte[] buffer
-
myString
@Nullable protected String myString
-
stream
protected OutputStream stream
-
bytesWritten
protected int bytesWritten
-
-
Constructor Detail
-
FastStringBuffer
public FastStringBuffer()
-
FastStringBuffer
public FastStringBuffer(@Nullable OutputStream stream)
-
FastStringBuffer
public FastStringBuffer(int initialSize)
-
FastStringBuffer
public FastStringBuffer(int initialSize, @Nullable OutputStream stream)
-
-
Method Detail
-
append
public FastStringBuffer append(String s) throws IOException
- Throws:
IOException
-
append
public FastStringBuffer append(int i) throws IOException
- Throws:
IOException
-
append
public FastStringBuffer append(byte[] a) throws IOException
- Throws:
IOException
-
append
public FastStringBuffer append(byte[] a, int start, int length) throws IOException
- Throws:
IOException
-
append
public FastStringBuffer append(@Nullable String s, String charset) throws IOException
- Throws:
IOException
-
appendEscaped
public FastStringBuffer appendEscaped(String s) throws IOException
- Throws:
IOException
-
appendEscaped
public FastStringBuffer appendEscaped(@Nullable String s, String charset) throws IOException
- Throws:
IOException
-
appendCls
public FastStringBuffer appendCls(String s) throws IOException
Appends constant string literals only!!!!!- Throws:
IOException
-
appendCls
public FastStringBuffer appendCls(@Nullable String s, String charset) throws IOException
Appends constant string literals only!!!!!- Throws:
IOException
-
appendCr
public FastStringBuffer appendCr() throws IOException
- Throws:
IOException
-
appendCrLf
public FastStringBuffer appendCrLf() throws IOException
- Throws:
IOException
-
getBytes
public byte[] getBytes()
-
getSize
public int getSize()
-
setLength
public void setLength(int len)
-
write
public void write(int b) throws IOException
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
public void write(@Nullable byte[] a) throws IOException
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
write
public void write(byte[] a, int start, int length) throws IOException
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
flush
public void flush() throws IOException
- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
- Throws:
IOException
-
getBytesWritten
public int getBytesWritten()
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
-
appendUtf8
public FastStringBuffer appendUtf8(byte[] data, String charset) throws IOException
Write UTF8 data to the output page buffer without specifying a start or end position, defaults to 0,-1- Throws:
IOException
-
appendUtf8
public FastStringBuffer appendUtf8(byte[] data, String charset, int end) throws IOException
Write UTF8 data to the output page buffer without specifying a start position, defaults to 0- Throws:
IOException
-
appendUtf8
public FastStringBuffer appendUtf8(byte[] data, @Nullable String charset, int start, int end) throws IOException
Write UTF8 data to the output page buffer Pass in 0 and -1 for start and end to do the whole thing- Throws:
IOException
-
appendEscapedUtf8
public FastStringBuffer appendEscapedUtf8(byte[] data, @Nullable String charset, int start, int end) throws IOException
- Throws:
IOException
-
-