• Post Reply Bookmark Topic Watch Topic
  • New Topic

ByteArrayStream steam!  RSS feed

 
Allion Salvador
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is a ByteArrayStream for! To me, ObjectOutputStreams and PrintWriters are much, MUCH cooler. Could you answer me?
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cooler? What are you? 11 years old? Oh, wait, I see that you are.
The short answer is that there are different Readers and Streams for different purposes. Not all data is Objects and Strings. Have a look at the Java Tutorial chapter on IO for the basics on Java Streams.
 
Allion Salvador
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HEY!!! What kind of comment is that?!!! Thanks, though. I just didn't think they were too useful.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Allion]: HEY!!! What kind of comment is that?!!!

A good-natured one, I think. Most adult programmers aren't too concerned with the "coolness" of a class. If we cared about coolness, we probably wouldn't have become programmers.

I just didn't think they were too useful.

They aren't - unless you want to write data to a byte array, sequentially. Then they're very useful. There are other ways this could be done, of course, but if you're using streams elsewhere in your code then BAOS and BAIS are very convenient ways to connect streams to arrays (and vice versa).
[ November 17, 2005: Message edited by: Jim Yingst ]
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Allion Salvador:
HEY!!! What kind of comment is that?!!!


I'm just playing with you. Notice the smiley.
As Jim said, "cool" isn't as big a factor as "utility" is to a programmer. You never know exactly what problems you are going to have, so it's best to have a large number of tools to choose from.
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most adult programmers aren't too concerned with the "coolness" of a class. If we cared about coolness, we probably wouldn't have become programmers.


I am offended by that comment.

Who you calling an Adult?

Mark
 
zhangChina lei
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what are you talking about??
i don't understand your question!!!
who can help me to deal with my problem which topic name is [ask for advice]

thank you very much
 
Allion Salvador
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No,no! I was talking about the "superiority complex" thingamabob. It didn't sound too encouraging or stimulating! I understand the stream thingy, though. Thank you!

Zwetschenwasser tastes better when fermented in a brine of acephalous crustaceans
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Despite the smilies, Allion, now I can't tell if you are joking!

That "superiority" quote is Joe's signature file -- it gets automatically appended to every message he posts. It's something somebody said to him here at the Ranch one time, back in the olden times before we went and made him a Bartender. You're right that it wasn't a nice thing for that person to say at the time. Joe, being a real mensch, knows how to laugh at himself, though.
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've actually used the ByteArray streams once.

I was given a network protocol written in C++ which could send char[]. I had to use this protocol to send Java objects over a network. And how do you convert a Java object to a char[] / byte[]*? By using ByteArray streams: write to an ObjectOutputStream which uses a ByteArrayOutputStream, write to the ObjectOutputStream and then use toByteArray() of the ByteArrayOutputStream. Other way around, create a ByteArrayInputStream from an existing byte[], use it to create an ObjectInputStream and use its readObject method.


* Actually, a Java char is 2 bytes, and C++ chars are mostly 1 byte (although it is compiler specific). A Java char[] would not be useful most of the times.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getting back to the topic, constructing streams is like connecting pipes
together. Some are meant just to connect to other streams, like ObjectOutputStream,
while others are terminals, like ByteArrayInputStream and ByteArrayOutputStream.
These two give you in-memory sourcs/targets for reading/writing data, and as
such, are very useful. Here's a standard example: suppose you have a
serializable class and you need to implement clone -- hey wait, you
don't have to code that up, just serialize to a byte[] and then materialize
back into an object:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!