• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

char[] cbuf = new char[]

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Master Rancher
Posts: 5161
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 48
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 5161
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic