com.twelvemonkeys.io
Class SeekableOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by com.twelvemonkeys.io.SeekableOutputStream
All Implemented Interfaces:
Seekable, Closeable, Flushable

public abstract class SeekableOutputStream
extends OutputStream
implements Seekable

Abstract base class for OutputStreams implementing the Seekable interface.

Version:
$Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/io/SeekableOutputStream.java#2 $
Author:
Harald Kuhr
See Also:
SeekableInputStream

Field Summary
protected  Stack<Long> markedPositions
           
 
Constructor Summary
SeekableOutputStream()
           
 
Method Summary
protected  void checkOpen()
           
 void close()
          Closes the stream.
protected abstract  void closeImpl()
           
 void flush()
          Discards the initial position of the stream prior to the current stream position.
 void flushBefore(long pPosition)
          Discards the initial portion of the stream prior to the indicated postion.
protected abstract  void flushBeforeImpl(long pPosition)
           
 long getFlushedPosition()
          Returns the earliest position in the stream to which seeking may be performed.
 long getStreamPosition()
          Returns the current byte position of the stream.
 void mark()
          Marks a position in the stream to be returned to by a subsequent call to reset.
 void reset()
          Returns the file pointer to its previous position, at the time of the most recent unmatched call to mark.
 void seek(long pPosition)
          Sets the current stream position to the desired location.
protected abstract  void seekImpl(long pPosition)
           
 void write(byte[] pBytes)
           
 
Methods inherited from class java.io.OutputStream
write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.twelvemonkeys.io.Seekable
isCached, isCachedFile, isCachedMemory
 

Field Detail

markedPositions

protected Stack<Long> markedPositions
Constructor Detail

SeekableOutputStream

public SeekableOutputStream()
Method Detail

write

public final void write(byte[] pBytes)
                 throws IOException
Overrides:
write in class OutputStream
Throws:
IOException

seek

public final void seek(long pPosition)
                throws IOException
Description copied from interface: Seekable
Sets the current stream position to the desired location. The next read will occur at this location.

An IndexOutOfBoundsException will be thrown if pPosition is smaller than the flushed position (as returned by Seekable.getFlushedPosition()).

It is legal to seek past the end of the file; an EOFException will be thrown only if a read is performed.

Specified by:
seek in interface Seekable
Parameters:
pPosition - a long containing the desired file pointer position.
Throws:
IOException - if any other I/O error occurs.

seekImpl

protected abstract void seekImpl(long pPosition)
                          throws IOException
Throws:
IOException

mark

public final void mark()
Description copied from interface: Seekable
Marks a position in the stream to be returned to by a subsequent call to reset. Unlike a standard InputStream, all Seekable streams upport marking. Additionally, calls to mark and reset may be nested arbitrarily.

Unlike the mark methods declared by the Reader or InputStream interfaces, no readLimit parameter is used. An arbitrary amount of data may be read following the call to mark.

Specified by:
mark in interface Seekable

reset

public final void reset()
                 throws IOException
Description copied from interface: Seekable
Returns the file pointer to its previous position, at the time of the most recent unmatched call to mark.

Calls to reset without a corresponding call to mark will either:

An IOException will be thrown if the previous marked position lies in the discarded portion of the stream.

Specified by:
reset in interface Seekable
Throws:
IOException - if an I/O error occurs.
See Also:
InputStream.reset()

flushBefore

public final void flushBefore(long pPosition)
                       throws IOException
Description copied from interface: Seekable
Discards the initial portion of the stream prior to the indicated postion. Attempting to seek to an offset within the flushed portion of the stream will result in an IndexOutOfBoundsException.

Calling flushBefore may allow classes implementing this interface to free up resources such as memory or disk space that are being used to store data from the stream.

Specified by:
flushBefore in interface Seekable
Parameters:
pPosition - a long containing the length of the file prefix that may be flushed.
Throws:
IOException - if an I/O error occurs.

flushBeforeImpl

protected abstract void flushBeforeImpl(long pPosition)
                                 throws IOException
Throws:
IOException

flush

public final void flush()
                 throws IOException
Description copied from interface: Seekable
Discards the initial position of the stream prior to the current stream position. Equivalent to flushBefore(getStreamPosition()).

Specified by:
flush in interface Seekable
Specified by:
flush in interface Flushable
Overrides:
flush in class OutputStream
Throws:
IOException - if an I/O error occurs.

getFlushedPosition

public final long getFlushedPosition()
                              throws IOException
Description copied from interface: Seekable
Returns the earliest position in the stream to which seeking may be performed. The returned value will be the maximum of all values passed into previous calls to flushBefore.

Specified by:
getFlushedPosition in interface Seekable
Returns:
the earliest legal position for seeking, as a long.
Throws:
IOException - if an I/O error occurs.

getStreamPosition

public final long getStreamPosition()
                             throws IOException
Description copied from interface: Seekable
Returns the current byte position of the stream. The next read will take place starting at this offset.

Specified by:
getStreamPosition in interface Seekable
Returns:
a long containing the position of the stream.
Throws:
IOException - if an I/O error occurs.

checkOpen

protected final void checkOpen()
                        throws IOException
Throws:
IOException

close

public final void close()
                 throws IOException
Description copied from interface: Seekable
Closes the stream.

Specified by:
close in interface Seekable
Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
Throws:
IOException - if the stream can't be closed.

closeImpl

protected abstract void closeImpl()
                           throws IOException
Throws:
IOException


Copyright © 2015. All Rights Reserved.