cz.cuni.jagrlib
Class DefaultFileFormat

java.lang.Object
  extended by cz.cuni.jagrlib.DefaultProperty
      extended by cz.cuni.jagrlib.Piece
          extended by cz.cuni.jagrlib.DefaultFileFormat
All Implemented Interfaces:
Breakable, DataFileFormat, Property, Template
Direct Known Subclasses:
DefaultBrepFileFormat, DefaultRasterFileFormat, WAVFileFormatMapped

public abstract class DefaultFileFormat
extends Piece
implements DataFileFormat

File format default implementation.

Since:
0.24
See Also:
DefaultFileFormat.java

Field Summary
 
Fields inherited from class cz.cuni.jagrlib.Piece
channels, info, pl, plugs, reg, TEMPLATE_TYPE, userBreak
 
Fields inherited from interface cz.cuni.jagrlib.iface.DataFileFormat
DOUBLE_STREAM
 
Fields inherited from interface cz.cuni.jagrlib.iface.Property
LOGGING, STATISTICS, TEXT_DESCRIPTION
 
Fields inherited from interface cz.cuni.jagrlib.Template
ALL_PLUGS, C_1D, C_2D, C_3D, C_ALPHA, C_BINARY, C_BREP, C_CAMERA, C_CIRCLE, C_CLIP, C_COMPRESSION, C_CURVE, C_DATA, C_DRAW, C_EDITOR, C_ELLIPSE, C_FILL, C_FILTER, C_FLOAT, C_FUNCTION, C_IMAGE, C_INTEGER, C_IO, C_LIGHT, C_LINE, C_METRIC, C_POLYGON, C_PROJECTION, C_RADIOSITY, C_RASTER, C_RENDER, C_SAMPLE, C_SCENE, C_SET, C_SOLID, C_TEXT, C_TRANSFORM, C_VECTOR, C_VIEWER, C_WORKER, CAT_EMPTY, EMPTY, IFACE, JAGRLIB, JAGRLIB2, MANIPULATOR_COMBO, MANIPULATOR_CUSTOM, MANIPULATOR_DEFAULT, MANIPULATOR_MULTILINE, PL_ALPHAMASK, PL_BITMASK, PL_CAUSTIC, PL_CODEC, PL_COLORMAP, PL_COMPARE, PL_DATA, PL_DIRECT, PL_EDITOR, PL_FILTER, PL_FUNCTION, PL_IMAGE, PL_IMPORT, PL_INPUT, PL_INTERSECTION, PL_LIGHTSOURCE, PL_ORDER, PL_OUTPUT, PL_PALETTE, PL_PHOTON, PL_PROPERTY, PL_QUANTIZER, PL_RASTER, PL_RENDER, PL_SHADOW, PL_STREAM, PL_TRANSFORM, PL_TRIGGER, PL_VOLUME, PL_WINDOW, TYPE_BOOLEAN, TYPE_DOUBLE, TYPE_FLOAT, TYPE_INTEGER, TYPE_LONG, TYPE_OBJECT, TYPE_STRING
 
Constructor Summary
DefaultFileFormat()
           
 
Method Summary
 void closeFile()
          Closes the file (used in file-mapping mode).
abstract  java.lang.String[] fileNameMasks()
          Returns file-name masks associated with the file-format type.
abstract  int headerLength()
          Length of file-format header (number of bytes which is necessary for file-format matching).
 int init(int phase)
          Module initialization - called before any Runnable.run().
abstract  boolean loadFile(BitStream stream, java.lang.Object support)
          Retrieves data from the given BitStream.
abstract  boolean loadFile(java.lang.String fileName, java.lang.String detail)
          Retrieves data from the given file (stream).
abstract  double match(byte[] header, java.lang.String fileName)
          File-format match test.
static float readFloat(java.io.InputStream is, boolean littleEndian)
          Read one binary float number.
static java.lang.String readLine(java.io.InputStream is)
          Read next nonempty line.
static boolean readParameters(EntropyCodec codec, java.lang.Object instance)
          Reads and sets binary parameters to the given module instance.
static java.lang.String readToken(java.io.InputStream is)
          Read character token delimited by an white-space.
static int readUnsigned16(BitStream s, boolean littleEndian)
          Reads 16-bit unsigned integer from the given bit-stream.
static int readUnsigned16(EntropyCodec codec)
          Reads 16-bit unsigned integer from the given codec stream (big endian).
static long readUnsigned32(BitStream s, boolean littleEndian)
          Reads 32-bit unsigned integer from the given bit-stream.
abstract  boolean saveFile(BitStream stream, java.lang.Object support)
          Saves data to the given BitStream.
abstract  boolean saveFile(java.lang.String fileName, java.lang.String detail)
          Saves data to the given file (stream).
static void writeFloat(java.io.OutputStream os, float fl)
          Writes binary float number in little endian format.
static void writeLine(java.io.OutputStream os, java.lang.String s)
          Write the given string followed by system newline character.
static void writeParameters(EntropyCodec codec, java.lang.Object instance)
          Writes binary parameters of the given module instance.
static void writeUnsigned16(BitStream s, int data, boolean littleEndian)
          Writes 16-bit unsigned integer to the given bit-stream.
static void writeUnsigned16(EntropyCodec codec, int data)
          Stores the given 16-bit unsigned integer in big-endian order.
static void writeUnsigned32(BitStream s, long data, boolean littleEndian)
          Writes 32-bit unsigned integer to the given bit-stream.
 
Methods inherited from class cz.cuni.jagrlib.Piece
className, connect, findPlug, findPlug, getInfo, getInterface, getInterface, getPlug, getReg, isCompatible, isCompatible, isConnected, logError, logWarning, myInterface, newInputPlug, newOptOutputPlug, newOutputPlug, newPlug, noTemplates, propBegin, propBounds, propDefault, propEnd, propEnum, propManipulator, propManipulator, setProgressListener, setRegStrings, setTemplate, setTemplateDynamic, stop
 
Methods inherited from class cz.cuni.jagrlib.DefaultProperty
booleanProperty, booleanProperty, colorDoubleProperty, colorProperty, commit, doubleProperty, doubleProperty, doubleProperty, enumProperty, floatProperty, floatProperty, floatProperty, get, intProperty, intProperty, intProperty, longProperty, longProperty, longProperty, set, set, stringProperty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface cz.cuni.jagrlib.iface.Property
commit, get, set
 
Methods inherited from interface cz.cuni.jagrlib.Breakable
setProgressListener, stop
 

Constructor Detail

DefaultFileFormat

public DefaultFileFormat()
Method Detail

writeLine

public static void writeLine(java.io.OutputStream os,
                             java.lang.String s)
                      throws java.io.IOException
Write the given string followed by system newline character.

Throws:
java.io.IOException

readLine

public static java.lang.String readLine(java.io.InputStream is)
                                 throws java.io.IOException
Read next nonempty line. Returns null if no line is available.

Throws:
java.io.IOException

readToken

public static java.lang.String readToken(java.io.InputStream is)
                                  throws java.io.IOException
Read character token delimited by an white-space. Skips initial white-spaces.

Throws:
java.io.IOException

readFloat

public static float readFloat(java.io.InputStream is,
                              boolean littleEndian)
                       throws java.io.IOException
Read one binary float number.

Parameters:
is - Stream to read from.
littleEndian - true for little endian.
Throws:
java.io.IOException

writeFloat

public static void writeFloat(java.io.OutputStream os,
                              float fl)
                       throws java.io.IOException
Writes binary float number in little endian format.

Throws:
java.io.IOException

readUnsigned16

public static final int readUnsigned16(BitStream s,
                                       boolean littleEndian)
                                throws java.io.IOException
Reads 16-bit unsigned integer from the given bit-stream.

Throws:
java.io.IOException

writeUnsigned16

public static final void writeUnsigned16(BitStream s,
                                         int data,
                                         boolean littleEndian)
                                  throws java.io.IOException
Writes 16-bit unsigned integer to the given bit-stream.

Throws:
java.io.IOException

readUnsigned32

public static final long readUnsigned32(BitStream s,
                                        boolean littleEndian)
                                 throws java.io.IOException
Reads 32-bit unsigned integer from the given bit-stream.

Throws:
java.io.IOException

writeUnsigned32

public static final void writeUnsigned32(BitStream s,
                                         long data,
                                         boolean littleEndian)
                                  throws java.io.IOException
Writes 32-bit unsigned integer to the given bit-stream.

Throws:
java.io.IOException

readUnsigned16

public static final int readUnsigned16(EntropyCodec codec)
                                throws java.io.IOException
Reads 16-bit unsigned integer from the given codec stream (big endian).

Throws:
java.io.IOException

writeUnsigned16

public static final void writeUnsigned16(EntropyCodec codec,
                                         int data)
                                  throws java.io.IOException
Stores the given 16-bit unsigned integer in big-endian order.

Throws:
java.io.IOException

readParameters

public static final boolean readParameters(EntropyCodec codec,
                                           java.lang.Object instance)
                                    throws java.io.IOException
Reads and sets binary parameters to the given module instance.

Returns:
true if object was successfully set up.
Throws:
java.io.IOException

writeParameters

public static final void writeParameters(EntropyCodec codec,
                                         java.lang.Object instance)
                                  throws java.io.IOException
Writes binary parameters of the given module instance.

Throws:
java.io.IOException

headerLength

public abstract int headerLength()
Length of file-format header (number of bytes which is necessary for file-format matching).

Specified by:
headerLength in interface DataFileFormat
Returns:
Number of bytes from beginning of a file which are enough for file-format match test.
See Also:
match(byte[], java.lang.String)

match

public abstract double match(byte[] header,
                             java.lang.String fileName)
File-format match test. Guesses whether the binary file-header (and file-name) can belong to this file-format type.

Specified by:
match in interface DataFileFormat
Parameters:
header - Bytes from beginning of the file.
fileName - Optional file-name string.
Returns:
Probability of being my file-format type (0.0 .. no way, 0.5 .. maybe, 0.9 .. almost sure, 1.0 .. absolutely - no need to check another formats).
See Also:
headerLength(), fileNameMasks()

fileNameMasks

public abstract java.lang.String[] fileNameMasks()
Returns file-name masks associated with the file-format type. Needs not be implemented in systems where "file-name -> file-type" mapping is irrelevant.

Specified by:
fileNameMasks in interface DataFileFormat
Returns:
Array of wild-card file-name masks (e.g. ["*.png"]) or null.
See Also:
match(byte[], java.lang.String)

loadFile

public abstract boolean loadFile(java.lang.String fileName,
                                 java.lang.String detail)
                          throws java.io.IOException
Retrieves data from the given file (stream). Can be used for "file opening" in virtual-mapped mode.

Specified by:
loadFile in interface DataFileFormat
Parameters:
fileName - File-name (stream-name) to read from (can be null).
detail - Further implementation-specific information ("r" or "rw" for mapped mode).
Returns:
true if the operation was successful.
Throws:
java.io.IOException
See Also:
loadFile(BitStream,Object), saveFile(String,String), closeFile()

loadFile

public abstract boolean loadFile(BitStream stream,
                                 java.lang.Object support)
                          throws java.io.IOException
Retrieves data from the given BitStream. This metod is optional.

The stream should be opened before - only the reOpen(false,null) call can be used.

Specified by:
loadFile in interface DataFileFormat
Parameters:
stream - Input bit-stream (opened - call reOpen(false,null) first).
support - Implementation-specific data (e.g. second BitStream for file-formats with DataFileFormat.DOUBLE_STREAM property).
Returns:
true if the operation was successful.
Throws:
java.io.IOException
See Also:
loadFile(String,String), DataFileFormat.DOUBLE_STREAM

saveFile

public abstract boolean saveFile(java.lang.String fileName,
                                 java.lang.String detail)
                          throws java.io.IOException
Saves data to the given file (stream).

Specified by:
saveFile in interface DataFileFormat
Parameters:
fileName - File-name (stream-name) to write to (can be null).
detail - Further implementation-specific information.
Returns:
true if the operation was successful.
Throws:
java.io.IOException
See Also:
saveFile(BitStream,Object), loadFile(String,String)

saveFile

public abstract boolean saveFile(BitStream stream,
                                 java.lang.Object support)
                          throws java.io.IOException
Saves data to the given BitStream. This metod is optional.

The stream should be opened and empty - the reOpen(true,null) call is not required.

Specified by:
saveFile in interface DataFileFormat
Parameters:
stream - Output bit-stream (opened and empty).
support - Implementation-specific data (e.g. second BitStream for file-formats with DataFileFormat.DOUBLE_STREAM property).
Returns:
true if the operation was successful.
Throws:
java.io.IOException
See Also:
saveFile(String,String), DataFileFormat.DOUBLE_STREAM

closeFile

public void closeFile()
               throws java.io.IOException
Closes the file (used in file-mapping mode).

Specified by:
closeFile in interface DataFileFormat
Throws:
java.io.IOException
See Also:
loadFile(String,String)

init

public int init(int phase)
Module initialization - called before any Runnable.run(). Individual phases (starting at 0) will be called until maximum requested phase number is reached.

Overrides:
init in class Piece
Parameters:
phase - Current phase number.
Returns:
Number of phases needed to initialize this module.