org.scijava.util
Class LineOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.scijava.util.LineOutputStream
All Implemented Interfaces:
Closeable, Flushable

public abstract class LineOutputStream
extends OutputStream

This class hides a line-based output behind an OutputStream.

Only the abstract method println(String) needs to be overridden; the other methods in OutputStream are overridden in this class already to buffer lines until they are complete and then flush them.

Author:
Johannes Schindelin

Field Summary
 byte[] buffer
           
 int len
           
 
Constructor Summary
LineOutputStream()
           
 
Method Summary
 void close()
          Flushes the current line buffer if any bytes are left in it.
protected  void ensure(int length)
          Increases the size of the line buffer if necessary.
 void flush()
          If any bytes are in the current line buffer, output the line via println(String), stripping any trailing new-line characters.
abstract  void println(String line)
          This method is all that needs to be implemented.
 void write(byte[] buf)
          Adds bytes to the current line buffer.
 void write(byte[] buf, int offset, int length)
          Adds bytes to the current line buffer.
 void write(int b)
          Adds a single byte to the current line buffer, or flush() the current line if it is a new-line character.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buffer

public byte[] buffer

len

public int len
Constructor Detail

LineOutputStream

public LineOutputStream()
Method Detail

println

public abstract void println(String line)
                      throws IOException
This method is all that needs to be implemented.

Parameters:
line - the line to print
Throws:
IOException

write

public void write(int b)
           throws IOException
Adds a single byte to the current line buffer, or flush() the current line if it is a new-line character.

Specified by:
write in class OutputStream
Parameters:
b - the byte to write
Throws:
IOException

write

public void write(byte[] buf)
           throws IOException
Adds bytes to the current line buffer. Whenever a new-line character is encountered, flush() the current line buffer.

Overrides:
write in class OutputStream
Parameters:
buf - the bytes to write
Throws:
IOException

write

public void write(byte[] buf,
                  int offset,
                  int length)
           throws IOException
Adds bytes to the current line buffer. Whenever a new-line character is encountered, flush() the current line buffer.

Overrides:
write in class OutputStream
Parameters:
buf - the bytes to write
offset - the offset into the buffer
length - how many bytes to add
Throws:
IOException

close

public void close()
           throws IOException
Flushes the current line buffer if any bytes are left in it.

Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
Throws:
IOException

flush

public void flush()
           throws IOException
If any bytes are in the current line buffer, output the line via println(String), stripping any trailing new-line characters.

Specified by:
flush in interface Flushable
Overrides:
flush in class OutputStream
Throws:
IOException

ensure

protected void ensure(int length)
Increases the size of the line buffer if necessary.

Parameters:
length - the required minimal length


Copyright © 2009–2015 SciJava. All rights reserved.