• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

simple array question

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, just wanted to verify how this method works.

Is the size array 'byte' is 4096 slots big?




private String slurp (InputStream in) {
try {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1 ;) {
out.append(new String(b, 0, n));
}
return out.toString();

}
catch(Exception e) {
e.printStackTrace();
return null;
}
}
[edit]Disable smilies CR[/edit]
[ June 15, 2008: Message edited by: Campbell Ritchie ]
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe so....

0 - 4095

Justin Fox
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works like this:

1. Create a new StringBuffer object
2. Create an array of 4096 bytes
3. Loop: Read some data into the byte array, assign the number of bytes read to n, do this until n == -1 (which means the end of the input stream has been reached)
4. Inside the loop: Create a new String object from the bytes read (bytes 0 to n-1 in the array) and append the string to the StringBuffer object
5. After the loop, return the contents of the StringBuffer as a String
6. Exception handling code: If an exception occurs, print the stack trace and return null as the result
 
Legend has it that if you rub the right tiny ad, a genie comes out.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic