
// // Copyright (c) 2006, Brian Frank and Andy Frank // Licensed under the Academic Free License version 3.0 // // History: // 16 Mar 06 Brian Frank Creation // ** ** OutStream is used to write binary and text data ** to an output stream. ** class OutStream { ////////////////////////////////////////////////////////////////////////// // Construction ////////////////////////////////////////////////////////////////////////// ** ** Make an output stream designed to write characters into the specified ** StrBuf. The output stream is designed to write character data, attempts ** to do binary writes will throw UnsupportedErr. ** static OutStream makeForStrBuf(StrBuf strBuf) ** ** Constructor for an OutStream which wraps another stream. ** All writes to this stream will be routed to the specified ** inner stream. ** ** If out is null, then it is the subclass responsibility to ** handle writes by overriding the following methods: `write` ** and `writeBuf`. ** protected new make(OutStream out) ////////////////////////////////////////////////////////////////////////// // Virtuals ////////////////////////////////////////////////////////////////////////// ** ** Write a byte to the output stream. Throw IOErr on error. ** Return this. ** virtual OutStream write(Int byte) ** ** Write n bytes from the specified Buf at it's current position to ** this output stream. If n is defaulted to buf.remaining(), then ** everything left in the buffer is drained to this output stream. ** The buf's position is advanced n bytes upon return. Throw ** IOErr on error. Return this. ** virtual OutStream writeBuf(Buf buf, Int n := buf.remaining) ** ** Flush the stream so any buffered bytes are written out. Default ** implementation does nothing. Throw IOErr on error. Return this. ** virtual OutStream flush() ** ** Close the input stream. This method is guaranteed to never ** throw an IOErr. Return true if the stream was closed successfully ** or false if the stream was closed abnormally. Default implementation ** does nothing and returns true. ** virtual Bool close() ////////////////////////////////////////////////////////////////////////// // Binary Data ////////////////////////////////////////////////////////////////////////// ** ** Write two bytes as a 16-bit number in network byte order. This method ** may be paired with `InStream.readU2` or `InStream.readS2`. Throw IOErr ** on error. Return this. ** OutStream writeI2(Int n) ** ** Write four bytes as a 32-bit number in network byte order. This ** method may be paired with `InStream.readU4` or `InStream.readS4`. Throw ** IOErr on error. Return this. ** OutStream writeI4(Int n) ** ** Write eight bytes as a 64-bit number in network byte order. This ** is paired with `InStream.readS8`. Throw IOErr on error. Return this. ** OutStream writeI8(Int n) ** ** Write four bytes as a 32-bit floating point number in network byte ** order according to `Float.bits32`. This is paired with `InStream.readF4`. ** Throw IOErr on error. Return this. ** OutStream writeF4(Float r) ** ** Write eight bytes as a 64-bit floating point number in network byte ** order according to `Float.bits`. This is paired with `InStream.readF8`. ** Throw IOErr on error. Return this. ** OutStream writeF8(Float r) ** ** Write one byte, one if true or zero if false. This method is paired ** with `InStream.readBool`. Throw IOErr on error. Return this. ** OutStream writeBool(Bool b) ** ** Write a Str in modified UTF-8 format according the 'java.io.DataOutput' ** specification. This method is paired with `InStream.readUtf`. Throw ** IOErr on error. Return this. ** OutStream writeUtf(Str s) ////////////////////////////////////////////////////////////////////////// // Text Data ////////////////////////////////////////////////////////////////////////// ** ** The current charset used to encode Unicode characters into ** bytes. The default charset should always be UTF-8. ** virtual Charset charset ** ** Write one or more bytes to the stream for the specified Unicode ** character based on the current charset encoding. ** OutStream writeChar(Int char) ** ** Write the Unicode characters in the specified string to the ** stream using the current charset encoding. Off specifies ** the index offset to start writing characters and len the ** number of characters in str to write. OutStream writeChars(Str str, Int off := 0, Int len := str.size-off) ** ** Convenience for 'writeChars(obj.toStr)'. If obj is null, ** then print the string "null". ** virtual OutStream print(Obj s) ** ** Convenience for 'writeChars(obj.toStr + "\n")'. If obj ** is null then print the string "null\n". ** virtual OutStream printLine(Obj obj := "") ** ** Write a serialized object from the stream according to ** the Fan [serialization format]`docLang::Serialization`. ** Throw IOErr on error. Return this. ** ** The options may be used to specify the format of the output: ** - "indent": Int specifies how many spaces to indent ** each level. Default is 0. ** - "skipDefaults": Bool specifies if we should skip fields ** at their default values. Field values are compared according ** to the 'equals' method. Default is false. ** OutStream writeObj(Obj obj, Str:Obj options := null) ////////////////////////////////////////////////////////////////////////// // Utils ////////////////////////////////////////////////////////////////////////// ** ** Write the given map of Str name/value pairs to the output stream ** according to the Fan props file format (see `InStream.readProps` for ** full specification). The props are written using UTF-8 regardless ** of this stream's current charset. If close argument is true, then ** automatically close the stream. Return this. ** OutStream writeProps(Str:Str props, Bool close := true) } ************************************************************************** ** SysOutStream ************************************************************************** internal class SysOutStream : OutStream { override OutStream write(Int byte) override OutStream writeBuf(Buf buf, Int n) override OutStream flush() override Bool close() }