Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CharBuffer vs. char[]

Tags:

java

io

buffer

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)?

like image 710
Chris Conway Avatar asked Nov 16 '08 20:11

Chris Conway


People also ask

What is CharBuffer?

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.

What is char* buffer?

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.


2 Answers

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.

like image 200
erickson Avatar answered Oct 04 '22 15:10

erickson


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()));     } } 
like image 26
Ron Tuffin Avatar answered Oct 04 '22 15:10

Ron Tuffin