How can I wrap a byteArray into a stream

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

How can I wrap a byteArray into a stream

Stephane Ducasse-3
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

Reply | Threaded
Open this post in threaded view
|

Re: How can I wrap a byteArray into a stream

Stephane Ducasse-3
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
<[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

Re: How can I wrap a byteArray into a stream

Stephane Ducasse-3
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
<[hidden email]> 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
> <[hidden email]> 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