What Collection is suitable for binary stream

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

What Collection is suitable for binary stream

Andrey Larionov
I'm a novice for Smalltalk. Can any body suggest proper collection for
binary stream. I try ByteArray - it dosn't allow Characters, try
ByteString - it not allow non literals.
Which collection (or maybe pattern) should be used to write binary and
character data?

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: What Collection is suitable for binary stream

Schwab,Wilhelm K
Andrey,

You mention both character and binary data.  One of the many wonderful things about Smalltalk is that it can handle situations like yours.

First, let's suppose (no insult implied, all of us were newbies once) that you are simply confused about what you need.  You will find Smalltalk very forgiving when you make changes toward a correct implementation.  Drop in the correct collection class, and things will start to work, or at least be closer to working than what you have now.

Second, you might truly need a mix of characters, numbers, and maybe other objects.  In that case, try Array.  More on that below.

If all of your data is text, then String or ByteString will do; it appears that one can specify String and ByteString will "magically" appear as needed.  I have not had problems with it, so I've never bothered to delve further into the coercion process.  Likewise, if everything is binary, ByteArray is a good choice.

There are other "dense" collection classes (see ArrayedCollection and subclasses), such as FloatArray.  Notice all of the methods starting in #bytes (#byteSize, #bytesPerBasicElement, etc.).  These classes "think" in binary and have two reasons for existing.  First, a sequence of a million floats is much better stored in a FloatArray than an ordinary collection, because the garbage collector sees that as one object, plus any that you spin off in working with the collection; in contrast, an Array or OrderedCollection of one million floats puts a nice load on the gc.  Further, FloatArray can be passed[*] as a C-style array into external functions expecting a float * argument; an Array is not C-friendly.  However, an Array (or OrderedCollection, Dictionary, etc. - the "ordinary" collections) can store any mix of objects you care to throw at them. For example, try

  OrderedCollection with:2 with:Float pi with:'Try this in C!'.

Put the above in a workspace and inspect or explore it.

Also try

   FloatArray with:3.0 with:2.

Did anything interesting happen?  Why did it happen?  It might help to try

   FloatArray with:3.0 with:'This will cause an error'.

Go ahead; the debugger is your friend.  

Does that help?

Bill


[*] actually, I _think_ a FloatArray can be passed into a float * argument. They certainly can in other dialects.  I am still learning my way around Pharo.  Maybe an external interfacing guru can enlighten us both.



-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Andrey Larionov
Sent: Friday, October 02, 2009 6:51 AM
To: Pharo Development
Subject: [Pharo-project] What Collection is suitable for binary stream

I'm a novice for Smalltalk. Can any body suggest proper collection for binary stream. I try ByteArray - it dosn't allow Characters, try ByteString - it not allow non literals.
Which collection (or maybe pattern) should be used to write binary and character data?

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project