G
Guest
Can I write all contents of an InputStream to a String at once?
something like
String a = inputStream.readAll();
something like
String a = inputStream.readAll();
sure:something like
String a = inputStream.readAll();
ak said:sure:Can I write all contents of an InputStream to a String at once?
something like
String a = inputStream.readAll();
public String readAll(InputStream inputStream) {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte [] buffer = new byte[1024];
while(true) {
int len = inputStream.read[buffer];
if(len < 0) {
break;
}
bout.write(buffer, 0, len);
}
Paul Lutus said:ak said:sure:Can I write all contents of an InputStream to a String at once?
something like
String a = inputStream.readAll();
public String readAll(InputStream inputStream) {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte [] buffer = new byte[1024];
while(true) {
int len = inputStream.read[buffer];
if(len < 0) {
break;
}
bout.write(buffer, 0, len);
}
??? Please consider the following, and avoid constructions like
"while(true)" and "break" when possible:
int len;
while((len = inputStream.read[buffer]) > 0) {
bout.write(buffer, 0, len);
}
Mike said:Paul Lutus said:ak said:Can I write all contents of an InputStream to a String at once?
something like
String a = inputStream.readAll();
sure:
public String readAll(InputStream inputStream) {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte [] buffer = new byte[1024];
while(true) {
int len = inputStream.read[buffer];
if(len < 0) {
break;
}
bout.write(buffer, 0, len);
}
??? Please consider the following, and avoid constructions like
"while(true)" and "break" when possible:
int len;
while((len = inputStream.read[buffer]) > 0) {
bout.write(buffer, 0, len);
}
I find the other version clearer, myself.
The nesting of method call and
test in the while statement is too much information in one line,
and it's
unfortunate to declare len outside the loop that it's only used inside of.
(three
instead of two items as in my example). Want to object to that also?
I could instead have said:
for (int len = inputStream.read[buffer]); len > 0 {
}
Vincent said:Although I agree with you for the while loop, I don't think that this is a
good example. A for loop should always be that kind (I think):
for(T i = start; i<end; i++)
(where T could be int, Iterator...)
There is only few information here: start and end, and assuming you are
always using the for loop that way, you get use to it and understand
easily the statement
I could instead have said:
for (int len = inputStream.read[buffer]); len > 0 {
}
And this is the example of what not to do.
First I'm not sure it would
compile,
then it should be
for(int len = inputStream.read(buffer); len>0; len =
inputStream.read(buffer)) {
//...
}
And this is too much...
ak said:int len = inputStream.read[buffer];
while(len > 0) {
bout.write(buffer, 0, len);
len = inputStream.read[buffer];
}
or
for(int len = inputStream.read[buffer]; len > 0 {
bout.write(buffer, 0, len);
len = inputStream.read[buffer];
}
Paul Lutus said:Mike said:Paul Lutus said:ak wrote:
Can I write all contents of an InputStream to a String at once?
something like
String a = inputStream.readAll();
sure:
public String readAll(InputStream inputStream) {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte [] buffer = new byte[1024];
while(true) {
int len = inputStream.read[buffer];
if(len < 0) {
break;
}
bout.write(buffer, 0, len);
}
??? Please consider the following, and avoid constructions like
"while(true)" and "break" when possible:
int len;
while((len = inputStream.read[buffer]) > 0) {
bout.write(buffer, 0, len);
}
I find the other version clearer, myself.
You can't be serious. The original arrangement has two control structures
where only one is needed. Also, both "while(true)" and "break" are frowned
upon in any case where they are not required. They are not particularly
good examples of structure programming.
A while() clause should actually perform a test, not be added as a patch
to
create a loop that must later be broken out of, by force, using break.
1. A for-loop typically contains one additional piece of information
(three
instead of two items as in my example). Want to object to that also?
2. Compared to the original, this version is simpler to read and
interpret.
It is also better coding practice -- it doesn't misuse while().
and it's
unfortunate to declare len outside the loop that it's only used inside
of.
I could instead have said:
for (int len = inputStream.read[buffer]); len > 0 {
}
But this replaces one arbitrary choice with another, and uses a for clause
in a place where it is not the ideal choice.
In any case, writing "while(true)" simply builds a perpetual loop that
requires "break" to terminate, creating the illusion of appropriate usage.
Paul Lutus said:What? No wonder you think there's something wrong. Your example is broken
(it discards the first block of input), mine is not.
No. The third field is optional. If T is an iterator, one might want to:
for(T i = start; i.hasNext() {
something q = i.next();
}
I could instead have said:
for (int len = inputStream.read[buffer]); len > 0 {
}
And this is the example of what not to do.
Quite false. It is entirely appropriate and a common coding practice.
I compiled it, then posted it. You could do the same.
What? No wonder you think there's something wrong. Your example is broken
(it discards the first block of input), mine is not.
while(len > 0) {
bout.write(buffer, 0, len);
len = inputStream.read[buffer];
}
A maintenance nightmare. This is an example of the kind of coding practice
that drives software administrators up the wall. Always avoid this silly
construction, the kind that uses two identical lines of code to avoid a
little thought.
even if this one line looks ugly?Summary: Never write two identical lines of code when you can instead write
one.
while(len > 0) {
bout.write(buffer, 0, len);
len = inputStream.read[buffer];
}
A maintenance nightmare. This is an example of the kind of coding practice
that drives software administrators up the wall. Always avoid this silly
construction, the kind that uses two identical lines of code to avoid a
little thought.
Summary: Never write two identical lines of code when you can instead write
one.
Side Note: I'm with Paul Lutus all the way so far, but then, I'm more of a
"purist" than most.
Mike said:Paul Lutus said:Mike said:ak wrote:
Can I write all contents of an InputStream to a String at once?
something like
String a = inputStream.readAll();
sure:
public String readAll(InputStream inputStream) {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte [] buffer = new byte[1024];
while(true) {
int len = inputStream.read[buffer];
if(len < 0) {
break;
}
bout.write(buffer, 0, len);
}
??? Please consider the following, and avoid constructions like
"while(true)" and "break" when possible:
int len;
while((len = inputStream.read[buffer]) > 0) {
bout.write(buffer, 0, len);
}
I find the other version clearer, myself.
You can't be serious. The original arrangement has two control structures
where only one is needed. Also, both "while(true)" and "break" are
frowned upon in any case where they are not required. They are not
particularly good examples of structure programming.
I am entirely serious.
They're nicely punctuated..
2. Compared to the original, this version is simpler to read and
interpret.
It is also better coding practice -- it doesn't misuse while().
and it's
unfortunate to declare len outside the loop that it's only used inside
of.
I could instead have said:
for (int len = inputStream.read[buffer]); len > 0 {
}
But this replaces one arbitrary choice with another, and uses a for
clause in a place where it is not the ideal choice.
And it's wrong It reads from the stream only once, rather than each
time through the loop.
You need
len = inputStream.read[buffer]
as the iteration clause.
In any case, writing "while(true)" simply builds a perpetual loop that
requires "break" to terminate, creating the illusion of appropriate
usage.
You make a lot of absolute assertions. I disagree with many of them.
I'm
not sure what that leaves us to discuss.
Tony said:I'm waiting for someone to notice in this thread, as I thought it was
inevitable, but to no avail.
Take a closer look at the following line of code that I keep seeing
posted:
int len = inputStream.read[buffer];
Mike said:You like infinite for loops?
If I understand your code (I am not sure I do), and if it is equivalent to
for(int len = inputStream.read(buffer); len>0; ) { /* Body */ }
then it is equivalent to
{ int len = inputStream.read(buffer); while(len>0) { /* Body */ } }
Since /*Body*/ is not supposed to change len, you have an infinite loop
(for a non empty inputStream)
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.