[Pharo-dev] How can I wrap a byteArray into a stream

Stephane Ducasse stepharo.self at gmail.com
Fri Dec 21 16:53:15 EST 2018


I found it :)

initializeWithByteArray: aByteArrayStream
| A B C D |
in := ZnBufferedReadStream on: aByteArrayStream.
"Read the end-of-data value"
A := in next bitAnd: 16rFF.
B := (in next bitAnd: 16rff) << 8.
C := (in next bitAnd: 16rff) << 16.
D := (in next bitAnd: 16rff) << 24.
eod := A bitOr: (B bitOr: (C bitOr: D)).

"Skip the rest of the hashtable"
in skip: 2048 - 4.

"Current data pointer"
pos := 2048

On Fri, Dec 21, 2018 at 10:44 PM Stephane Ducasse
<stepharo.self at gmail.com> wrote:
>
> Of course this is not next but int16 :)
>
>
> I will look further into
>
> testInt16Aliases
> | input writer |
> writer := [ :block |
> ByteArray streamContents: [ :out |
> ZnBufferedWriteStream on: out do: block ] ].
> input := ByteArray readHexFrom: '04D2'.
> self assert: (ZnBufferedReadStream on: input readStream) int16 equals: 1234.
> self assert: (ZnBufferedReadStream on: input readStream) uint16 equals: 1234.
> self assert: (ZnBufferedReadStream on: input readStream) nextWord equals: 1234.
> self assert: ((ZnBufferedReadStream on: input readStream) nextNumber:
> 2) equals: 1234.
> self assert: ((ZnBufferedReadStream on: input reversed readStream)
> nextLittleEndianNumber: 2) equals: 1234.
> self assert: (writer value: [ :out | out int16: 1234 ]) equals: input.
> self assert: (writer value: [ :out | out uint16: 1234 ]) equals: input.
> self assert: (writer value: [ :out | out nextWordPut: 1234 ]) equals: input.
> self assert: (writer value: [ :out | out nextNumber: 2 put: 1234 ])
> equals: input.
> self assert: (writer value: [ :out | out nextLittleEndianNumber: 2
> put: 1234 ]) equals: input reversed.
> input := ByteArray readHexFrom: 'FB2E'.
> self assert: (ZnBufferedReadStream on: input readStream) int16 equals: -1234.
> self assert: (writer value: [ :out | out int16: -1234 ]) equals: input
>
>
>
>
>
> On Fri, Dec 21, 2018 at 10:40 PM Stephane Ducasse
> <stepharo.self at gmail.com> wrote:
> >
> > Hi
> >
> >  I'm fighting with super badly written code and my first reaction is a
> > to be able to write tests and parametrize the code.
> >
> > I have some byteArray (contents of a CDB database) that I would like
> > to manipulate using stream API.
> > I'm looking for something like this
> >
> > ZnBinaryStream on: aByteArray;
> >     next; next; skip: 10; ...
> >
> > Stef



More information about the Pharo-dev mailing list