2019-11-11 14:23:10 +00:00
|
|
|
interface StreamInterface
|
|
|
|
{
|
|
|
|
method close {}
|
|
|
|
method next {} // if readable
|
|
|
|
method nextPut: item {} // if writable
|
|
|
|
method atEnd {}
|
|
|
|
method contents {}
|
|
|
|
method isReadable {}
|
|
|
|
method isWritable {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FileStream(Object) [StreamInterface]
|
|
|
|
{
|
|
|
|
| accessor |
|
|
|
|
|
|
|
|
method(#class) on: path for: flags
|
|
|
|
{
|
|
|
|
| stream |
|
|
|
|
stream := self basicNew.
|
|
|
|
stream accessor: (FileAccessor on: path for: flags).
|
|
|
|
^stream
|
|
|
|
}
|
|
|
|
|
|
|
|
method close
|
|
|
|
{
|
|
|
|
ifnot (self.accessor isNil)
|
|
|
|
{
|
|
|
|
self.accessor close.
|
|
|
|
self.accessor := nil.
|
|
|
|
}.
|
|
|
|
}
|
|
|
|
|
|
|
|
method next
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-09 16:48:36 +00:00
|
|
|
class Stream(Object)
|
|
|
|
{
|
|
|
|
method(#class) new
|
|
|
|
{
|
2019-06-27 06:29:09 +00:00
|
|
|
// you must use dedicated class methods for instantiation
|
2018-06-09 16:48:36 +00:00
|
|
|
self messageProhibited: #new.
|
|
|
|
}
|
|
|
|
|
|
|
|
method(#class) on: object
|
|
|
|
{
|
|
|
|
^self subclassResponsibility: #on:.
|
|
|
|
}
|
|
|
|
|
|
|
|
method contents
|
|
|
|
{
|
|
|
|
^self subclassResponsibility: #contents.
|
|
|
|
}
|
|
|
|
|
|
|
|
method next
|
|
|
|
{
|
2019-06-27 06:29:09 +00:00
|
|
|
// return the next object in the receiver
|
2018-06-09 16:48:36 +00:00
|
|
|
^self subclassResponsibility: #next.
|
|
|
|
}
|
|
|
|
|
|
|
|
method nextPut: object
|
|
|
|
{
|
2019-06-27 06:29:09 +00:00
|
|
|
// insert an object at the next position in the receiver
|
2018-06-09 16:48:36 +00:00
|
|
|
^self subclassResponsibility: #next.
|
|
|
|
}
|
|
|
|
|
|
|
|
method nextPutAll: collection
|
|
|
|
{
|
|
|
|
collection do: [:elem | self nextPut: elem ].
|
|
|
|
^collection.
|
|
|
|
}
|
|
|
|
|
|
|
|
method atEnd
|
|
|
|
{
|
|
|
|
^self subclassResponsibility: #next.
|
|
|
|
}
|
|
|
|
|
|
|
|
method print: object
|
|
|
|
{
|
|
|
|
object printOn: self.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PositionableStream(Stream)
|
|
|
|
{
|
|
|
|
var collection.
|
|
|
|
var(#get) position.
|
|
|
|
var readLimit.
|
|
|
|
|
|
|
|
method(#class) on: collection
|
|
|
|
{
|
|
|
|
^self new __on collection.
|
|
|
|
}
|
|
|
|
|
|
|
|
method initialize
|
|
|
|
{
|
|
|
|
super initialize.
|
|
|
|
self.position := 0.
|
|
|
|
self.readLimit := collection size.
|
|
|
|
}
|
|
|
|
|
|
|
|
method __on: collection
|
|
|
|
{
|
|
|
|
self.collection := collection.
|
|
|
|
}
|
|
|
|
|
|
|
|
method contents
|
|
|
|
{
|
|
|
|
^self.collection copyFrom: 0 to: self.readLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
method next: count
|
|
|
|
{
|
|
|
|
| newobj i |
|
|
|
|
newobj := self contents class new: count.
|
|
|
|
while (i < count)
|
|
|
|
{
|
|
|
|
newobj at: i put: self next.
|
|
|
|
i := i + 1.
|
|
|
|
}.
|
|
|
|
}
|
|
|
|
|
|
|
|
method peek
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
method atEnd
|
|
|
|
{
|
|
|
|
^self.position >= self.readLimit.
|
|
|
|
}
|
|
|
|
|
|
|
|
method isEmpty
|
|
|
|
{
|
|
|
|
^self.position == 0.
|
|
|
|
}
|
|
|
|
|
|
|
|
method position: pos
|
|
|
|
{
|
2019-06-27 06:29:09 +00:00
|
|
|
//if (pos >= 0 and
|
2018-06-09 16:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
method reset
|
|
|
|
{
|
|
|
|
self.position := 0.
|
|
|
|
}
|
|
|
|
|
|
|
|
method setToEnd
|
|
|
|
{
|
|
|
|
self.position := self.readLimit.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ReadWriteStream(PositionableStream)
|
|
|
|
{
|
|
|
|
method next
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ExternalStream(ReadWriteStream)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-27 06:29:09 +00:00
|
|
|
class ByteStreamAdapter(Object) //# [ByteStreamable, ByteXXX]
|
2018-06-09 16:48:36 +00:00
|
|
|
{
|
|
|
|
var bsobj.
|
|
|
|
var inbuf.
|
|
|
|
var inpos.
|
|
|
|
var inlen.
|
|
|
|
var indown.
|
|
|
|
|
2018-06-16 16:45:17 +00:00
|
|
|
var outbuf.
|
|
|
|
var outlen.
|
|
|
|
var outdown.
|
|
|
|
|
2018-06-09 16:48:36 +00:00
|
|
|
method(#class) new
|
|
|
|
{
|
|
|
|
self messageProhibited: #new.
|
|
|
|
}
|
|
|
|
|
|
|
|
method(#class) new: obj
|
|
|
|
{
|
|
|
|
self messageProhibited: #new:.
|
|
|
|
}
|
|
|
|
|
2018-06-16 16:45:17 +00:00
|
|
|
method initialize
|
2018-06-09 16:48:36 +00:00
|
|
|
{
|
|
|
|
super initialize.
|
|
|
|
self.bsobj := nil.
|
2018-06-16 16:45:17 +00:00
|
|
|
|
2018-06-09 16:48:36 +00:00
|
|
|
self.inbuf := ByteArray new: 1024.
|
|
|
|
self.inpos := 0.
|
|
|
|
self.inlen := 0.
|
|
|
|
self.indown := false.
|
2018-06-16 16:45:17 +00:00
|
|
|
|
|
|
|
self.outbuf := ByteArray new: 1024.
|
|
|
|
self.outlen := 0.
|
|
|
|
self.outdown := false.
|
2018-06-09 16:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
method(#class) on: bsobj
|
|
|
|
{
|
|
|
|
^super new __on: bsobj.
|
|
|
|
}
|
|
|
|
|
|
|
|
method __on: bsobj
|
|
|
|
{
|
|
|
|
self.bsobj := bsobj.
|
|
|
|
}
|
|
|
|
|
|
|
|
method __fill_inbuf
|
|
|
|
{
|
|
|
|
| v |
|
2018-06-16 16:45:17 +00:00
|
|
|
v := self.bsobj readBytesInto: self.inbuf.
|
2019-06-27 06:29:09 +00:00
|
|
|
// if the streamable object is not blocking, it may return an
|
|
|
|
// error object when data is not ready.
|
2018-06-09 16:48:36 +00:00
|
|
|
if (v isError) { ^v }.
|
|
|
|
if (v == 0)
|
|
|
|
{
|
2019-06-27 06:29:09 +00:00
|
|
|
// end of input
|
2018-06-09 16:48:36 +00:00
|
|
|
self.indown := true.
|
|
|
|
^v
|
|
|
|
}.
|
|
|
|
|
|
|
|
self.inlen := v.
|
|
|
|
self.inpos := 0.
|
|
|
|
^v.
|
|
|
|
}
|
|
|
|
|
|
|
|
method next
|
|
|
|
{
|
|
|
|
| v |
|
|
|
|
|
|
|
|
if (self.indown) { ^nil }.
|
|
|
|
if (self.inpos >= self.inlen)
|
|
|
|
{
|
|
|
|
v := self __fill_inbuf.
|
2019-06-27 06:29:09 +00:00
|
|
|
if (v isError) { ^v }. // TODO: change error handling
|
2018-06-09 16:48:36 +00:00
|
|
|
if (v <= 0) { ^nil }.
|
2019-06-27 06:29:09 +00:00
|
|
|
////if (self.inpos >= self.inlen) { ^nil }.
|
2018-06-09 16:48:36 +00:00
|
|
|
}.
|
|
|
|
|
|
|
|
v := self.inbuf at: self.inpos.
|
|
|
|
self.inpos := self.inpos + 1.
|
|
|
|
^v.
|
|
|
|
}
|
|
|
|
|
|
|
|
method next: count into: byte_array startingAt: pos
|
|
|
|
{
|
2019-06-27 06:29:09 +00:00
|
|
|
// return the count bytes
|
2018-06-16 16:45:17 +00:00
|
|
|
| taken avail needed v incapa |
|
2018-06-09 16:48:36 +00:00
|
|
|
|
|
|
|
if (self.indown) { ^0 }.
|
|
|
|
|
2019-06-27 06:29:09 +00:00
|
|
|
// i assume the given byte array is large enough
|
|
|
|
// to store as many as count bytes starting at the pos position.
|
|
|
|
// if the parameters cannot meet this assumption, you will get
|
|
|
|
// into various system exceptions.
|
2018-06-09 16:48:36 +00:00
|
|
|
needed := count.
|
2018-06-16 16:45:17 +00:00
|
|
|
incapa := self.inbuf size.
|
2018-06-09 16:48:36 +00:00
|
|
|
|
|
|
|
while (needed > 0)
|
|
|
|
{
|
|
|
|
avail := self.inlen - self.inpos.
|
|
|
|
if (avail <= 0)
|
|
|
|
{
|
2018-06-16 16:45:17 +00:00
|
|
|
if (needed >= incapa)
|
|
|
|
{
|
2019-06-27 06:29:09 +00:00
|
|
|
// don't rely on the internal buffer if the number of bytes
|
|
|
|
// needed are equal to or greater than the capacity of the
|
|
|
|
// buffer.
|
2019-10-23 16:40:02 +00:00
|
|
|
v := self.bsobj readBytesInto: byte_array at: pos for: needed.
|
2019-06-27 06:29:09 +00:00
|
|
|
if (v isError or v <= 0) { break }. // <<< TODO: change the error handling
|
2018-06-16 16:45:17 +00:00
|
|
|
pos := pos + v.
|
|
|
|
needed := needed - v.
|
|
|
|
continue.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
v := self __fill_inbuf.
|
2019-06-27 06:29:09 +00:00
|
|
|
if (v isError or v <= 0) { break }. // <<< TODO: change the error handling
|
2018-06-16 16:45:17 +00:00
|
|
|
}.
|
2018-06-09 16:48:36 +00:00
|
|
|
}.
|
|
|
|
|
|
|
|
taken := if (avail <= needed) { avail } else { needed }.
|
|
|
|
byte_array replaceFrom: pos count: taken with: self.inbuf startingAt: self.inpos.
|
|
|
|
self.inpos := self.inpos + taken.
|
|
|
|
pos := pos + taken.
|
|
|
|
needed := needed - taken.
|
|
|
|
}.
|
|
|
|
|
|
|
|
^count - needed.
|
|
|
|
}
|
|
|
|
|
2018-06-16 16:45:17 +00:00
|
|
|
method nextPut: count from: byte_array startingAt: pos
|
|
|
|
{
|
|
|
|
| consumed free rem outcapa |
|
|
|
|
|
|
|
|
if (self.outdown) { ^0 }.
|
|
|
|
|
|
|
|
rem := count.
|
|
|
|
outcapa := self.outbuf size.
|
|
|
|
|
|
|
|
while (rem > 0)
|
|
|
|
{
|
|
|
|
free := outcapa - self.outlen.
|
|
|
|
if (free <= 0)
|
|
|
|
{
|
2019-06-27 06:29:09 +00:00
|
|
|
self flush. // TODO: error handling...
|
2018-06-16 16:45:17 +00:00
|
|
|
}.
|
|
|
|
|
|
|
|
if (self.outlen <= 0 and rem >= outcapa)
|
|
|
|
{
|
2019-10-23 16:40:02 +00:00
|
|
|
consumed := self.bsobj writeBytesFrom: byte_array at: pos for: rem.
|
2019-06-27 06:29:09 +00:00
|
|
|
if (consumed <= 0) { break }. // TODO: error handling. also handle exceptions
|
2018-06-16 16:45:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
consumed := if (free <= rem) { free } else { rem }.
|
|
|
|
self.outbuf replaceFrom: self.outlen count: consumed with: byte_array startingAt: pos.
|
|
|
|
self.outlen := self.outlen + consumed.
|
|
|
|
}.
|
|
|
|
|
|
|
|
pos := pos + consumed.
|
|
|
|
rem := rem - consumed.
|
|
|
|
}.
|
|
|
|
|
|
|
|
^count - rem.
|
|
|
|
}
|
|
|
|
|
|
|
|
method flush
|
|
|
|
{
|
|
|
|
| v pos |
|
|
|
|
|
|
|
|
pos := 0.
|
|
|
|
while (pos < self.outlen)
|
|
|
|
{
|
2019-10-23 16:40:02 +00:00
|
|
|
v := self.bsobj writeBytesFrom: self.outbuf at: pos for: (self.outlen - pos).
|
2019-06-27 06:29:09 +00:00
|
|
|
if (v <= 0) { break }. // TODO: error handling. also handle exceptions
|
2018-06-16 16:45:17 +00:00
|
|
|
pos := pos + v.
|
|
|
|
}.
|
2018-06-09 16:48:36 +00:00
|
|
|
|
2018-06-16 16:45:17 +00:00
|
|
|
self.outlen := 0.
|
|
|
|
}
|
2018-06-09 16:48:36 +00:00
|
|
|
}
|
2018-06-16 16:45:17 +00:00
|
|
|
|
|
|
|
class ByteStream(ByteStreamAdapter)
|
|
|
|
{
|
|
|
|
method close
|
|
|
|
{
|
|
|
|
if (self.bsobj notNil)
|
|
|
|
{
|
|
|
|
self.bsobj close.
|
|
|
|
self.bsobj := nil.
|
|
|
|
}.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 12:38:09 +00:00
|
|
|
/*
|
2018-06-16 16:45:17 +00:00
|
|
|
class TextStream(ByteStream)
|
|
|
|
{
|
2018-06-17 17:41:04 +00:00
|
|
|
method initialize
|
|
|
|
{
|
|
|
|
super initialize.
|
|
|
|
self.
|
|
|
|
}
|
2018-06-16 16:45:17 +00:00
|
|
|
|
2018-06-17 17:41:04 +00:00
|
|
|
method next
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
method __utf8_to_uc
|
|
|
|
{
|
|
|
|
if (self.seq at: 0) bitAnd:
|
|
|
|
}
|
2019-06-19 12:38:09 +00:00
|
|
|
}*/
|
2019-11-11 14:23:10 +00:00
|
|
|
|