Is there any reason to prefer a CharBuffer
to a char[]
in the following:
CharBuffer buf = CharBuffer.allocate(DEFAULT_BUFFER_SIZE); while( in.read(buf) >= 0 ) { out.append( buf.flip() ); buf.clear(); }
vs.
char[] buf = new char[DEFAULT_BUFFER_SIZE]; int n; while( (n = in.read(buf)) >= 0 ) { out.write( buf, 0, n ); }
(where in
is a Reader
and out
in a Writer
)?
CharBuffer is an abstract class of the java. nio package. CharBuffer is the buffer for characters. CharBuffer object is created by calling allocate(). We need to pass the capacity of CharBuffer to allocate method.
Char buffers can be created either by allocation , which allocates space for the buffer's content, by wrapping an existing char array or string into a buffer, or by creating a view of an existing byte buffer. Like a byte buffer, a char buffer is either direct or non-direct.
No, there's really no reason to prefer a CharBuffer
in this case.
In general, though, CharBuffer
(and ByteBuffer
) can really simplify APIs and encourage correct processing. If you were designing a public API, it's definitely worth considering a buffer-oriented API.
I wanted to mini-benchmark this comparison.
Below is the class I have written.
The thing is I can't believe that the CharBuffer performed so badly. What have I got wrong?
EDIT: Since the 11th comment below I have edited the code and the output time, better performance all round but still a significant difference in times. I also tried out2.append((CharBuffer)buff.flip()) option mentioned in the comments but it was much slower than the write option used in the code below.
Results: (time in ms)
char[] : 3411
CharBuffer: 5653
public class CharBufferScratchBox { public static void main(String[] args) throws Exception { // Some Setup Stuff String smallString = "1111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000"; StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < 1000; i++) { stringBuilder.append(smallString); } String string = stringBuilder.toString(); int DEFAULT_BUFFER_SIZE = 1000; int ITTERATIONS = 10000; // char[] StringReader in1 = null; StringWriter out1 = null; Date start = new Date(); for (int i = 0; i < ITTERATIONS; i++) { in1 = new StringReader(string); out1 = new StringWriter(string.length()); char[] buf = new char[DEFAULT_BUFFER_SIZE]; int n; while ((n = in1.read(buf)) >= 0) { out1.write( buf, 0, n); } } Date done = new Date(); System.out.println("char[] : " + (done.getTime() - start.getTime())); // CharBuffer StringReader in2 = null; StringWriter out2 = null; start = new Date(); CharBuffer buff = CharBuffer.allocate(DEFAULT_BUFFER_SIZE); for (int i = 0; i < ITTERATIONS; i++) { in2 = new StringReader(string); out2 = new StringWriter(string.length()); int n; while ((n = in2.read(buff)) >= 0) { out2.write( buff.array(), 0, n); buff.clear(); } } done = new Date(); System.out.println("CharBuffer: " + (done.getTime() - start.getTime())); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With