moo/kernel/Stream.moo

380 lines
6.1 KiB
Smalltalk
Raw Permalink Normal View History

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
{
// 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
{
// return the next object in the receiver
2018-06-09 16:48:36 +00:00
^self subclassResponsibility: #next.
}
method nextPut: object
{
// 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
{
//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)
{
}
class ByteStreamAdapter(Object) //# [ByteStreamable, ByteXXX]
2018-06-09 16:48:36 +00:00
{
var bsobj.
var inbuf.
var inpos.
var inlen.
var indown.
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:.
}
method initialize
2018-06-09 16:48:36 +00:00
{
super initialize.
self.bsobj := nil.
2018-06-09 16:48:36 +00:00
self.inbuf := ByteArray new: 1024.
self.inpos := 0.
self.inlen := 0.
self.indown := false.
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 |
v := self.bsobj readBytesInto: self.inbuf.
// 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)
{
// 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.
if (v isError) { ^v }. // TODO: change error handling
2018-06-09 16:48:36 +00:00
if (v <= 0) { ^nil }.
////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
{
// return the count bytes
| taken avail needed v incapa |
2018-06-09 16:48:36 +00:00
if (self.indown) { ^0 }.
// 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.
incapa := self.inbuf size.
2018-06-09 16:48:36 +00:00
while (needed > 0)
{
avail := self.inlen - self.inpos.
if (avail <= 0)
{
if (needed >= incapa)
{
// 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.
if (v isError or v <= 0) { break }. // <<< TODO: change the error handling
pos := pos + v.
needed := needed - v.
continue.
}
else
{
v := self __fill_inbuf.
if (v isError or v <= 0) { break }. // <<< TODO: change the error handling
}.
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.
}
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)
{
self flush. // TODO: error handling...
}.
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.
if (consumed <= 0) { break }. // TODO: error handling. also handle exceptions
}
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).
if (v <= 0) { break }. // TODO: error handling. also handle exceptions
pos := pos + v.
}.
2018-06-09 16:48:36 +00:00
self.outlen := 0.
}
2018-06-09 16:48:36 +00:00
}
class ByteStream(ByteStreamAdapter)
{
method close
{
if (self.bsobj notNil)
{
self.bsobj close.
self.bsobj := nil.
}.
}
}
/*
class TextStream(ByteStream)
{
method initialize
{
super initialize.
self.
}
method next
{
}
method __utf8_to_uc
{
if (self.seq at: 0) bitAnd:
}
}*/
2019-11-11 14:23:10 +00:00