• Post Reply Bookmark Topic Watch Topic
  • New Topic

char[] cbuf = new char[]

 
Vikki Fiawoo
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Evening,

Below is a sample code using StringBuffer and char [] cbuf.

I am trying to understand the purpose of using the char [] cbuf in the code. I have included comments as to how I have decipher the code.
If you can please provide feedback, by removing or editing any part of the comments that I have included that is not true or does not apply, so that I can better understand this piece of code. I have worked with BufferedReader before but not really StringBuffer, so I am lost here.

 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The parameter is for initial capacity. The stringbuffer can grow bigger than that.



This is the character buffer that will be used to store the read bytes. And since it is sized at 1K, that will be the maximum that can be returned in a single read() call.

Henry
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vikki Fiawoo wrote:

If you look at the Javadocs for java.io.Reader#read(char[]), it describes the return value as "The number of characters read, or -1 if the end of the stream has been reached". So this line keeps looping, reading characters into the cbuf array, until the end of the file.

The Javadocs should generally be your first port of call for understanding what methods do for classes in the core libraries.
 
Vikki Fiawoo
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Thank you for your explanation. It really helped me.

Could you please elaborate a little further on the this part of the code?

 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the API for java.lang.String (<-- that's a link) and look at the valueOf() method. Specifically, look at the one that takes three arguments, of the types given in the example.
 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reader.read(cbuf))

There is a read I think that takes no params and another that takes 3. I don't see a read(char[]) in the BufferedReader API. (At least not in Java 7 SE.)

 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's there. You have to look also at the methods inherited from superclasses, such as java.io.Reader in this case.
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vikki Fiawoo wrote:Could you please elaborate a little further on the this part of the code?



This is a BAD thing to do. You create a new String with the read chars here, and thats totally unneccessary. As others said above, go and check the API for the classes you use. In this case, StringBuffer: You will see that there is an append() method which allows you to directly append data from a char[] to the buffer.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D. Ogranos wrote:In this case, StringBuffer

I'd recommend StringBuilder over StringBuffer, as you don't need the synchronization StringBuffer provides.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!