• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple array question

 
Sunny Gibbony
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • 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 ]
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe so....

0 - 4095

Justin Fox
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic