uk.me.parabola.imgfmt.app
Class BufferedImgFileWriter

java.lang.Object
  extended by uk.me.parabola.imgfmt.app.BufferedImgFileWriter
All Implemented Interfaces:
java.io.Closeable, ImgFileWriter

public class BufferedImgFileWriter
extends java.lang.Object
implements ImgFileWriter

A straight forward implementation that just keeps all the data in a buffer until the file needs to be written to disk.

Author:
Steve Ratcliffe

Constructor Summary
BufferedImgFileWriter(ImgChannel chan)
           
 
Method Summary
 void close()
          Called when the stream is closed.
 java.nio.ByteBuffer getBuffer()
           
 long getSize()
          Get the size of the file as written.
 int position()
          Get the position.
 void position(long pos)
          Set the position of the file.
 void put(byte b)
          Write out a single byte.
 void put(byte[] val)
          Write out an arbitrary length sequence of bytes.
 void put(byte[] src, int start, int length)
          Write out part of a byte array.
 void put3(int val)
          Write out a 3 byte value in the correct byte order etc.
 void putChar(char c)
          Write out two bytes.
 void putInt(int val)
          Write out 4 byte value.
 void setMaxSize(long maxSize)
           
 void sync()
          Called to write out any saved buffers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BufferedImgFileWriter

public BufferedImgFileWriter(ImgChannel chan)
Method Detail

sync

public void sync()
          throws java.io.IOException
Called to write out any saved buffers. The strategy may write directly to the file in which case this would have nothing or little to do.

Specified by:
sync in interface ImgFileWriter
Throws:
java.io.IOException - If there is an error writing.

position

public int position()
Get the position. Needed because may not be reflected in the underlying file if being buffered.

Specified by:
position in interface ImgFileWriter
Returns:
The logical position within the file.

position

public void position(long pos)
Set the position of the file.

Specified by:
position in interface ImgFileWriter
Parameters:
pos - The new position in the file.

close

public void close()
           throws java.io.IOException
Called when the stream is closed. Any resources can be freed.

Specified by:
close in interface java.io.Closeable
Throws:
java.io.IOException

put

public void put(byte b)
Write out a single byte.

Specified by:
put in interface ImgFileWriter
Parameters:
b - The byte to write.

putChar

public void putChar(char c)
Write out two bytes. Done in the correct byte order.

Specified by:
putChar in interface ImgFileWriter
Parameters:
c - The value to write.

put3

public void put3(int val)
Write out a 3 byte value in the correct byte order etc.

Specified by:
put3 in interface ImgFileWriter
Parameters:
val - The value to write.

putInt

public void putInt(int val)
Write out 4 byte value.

Specified by:
putInt in interface ImgFileWriter
Parameters:
val - The value to write.

put

public void put(byte[] val)
Write out an arbitrary length sequence of bytes.

Specified by:
put in interface ImgFileWriter
Parameters:
val - The values to write.

put

public void put(byte[] src,
                int start,
                int length)
Write out part of a byte array.

Specified by:
put in interface ImgFileWriter
Parameters:
src - The array to take bytes from.
start - The start position.
length - The number of bytes to write.

getSize

public long getSize()
Get the size of the file as written. NOTE: that calling this is only valid at certain times.

Specified by:
getSize in interface ImgFileWriter
Returns:
The size of the file, if it is available.

getBuffer

public java.nio.ByteBuffer getBuffer()

setMaxSize

public void setMaxSize(long maxSize)