Constructor and Description |
---|
IOUtils() |
Modifier and Type | Method and Description |
---|---|
static boolean |
canRead(InputStreamObjectReader<?> reader,
BufferedInputStream is,
String name)
Test whether the data in the given
InputStream can be read by the
given InputStreamObjectReader . |
static <SRC> boolean |
canRead(ObjectReader<?,SRC> reader,
SRC source,
String name)
Test whether the data in the given source can be read by the given
ObjectReader . |
static <T extends ReadableBinary> |
deserialize(byte[] source,
Class<T> clazz)
Convenience function for deserializing an object from a byte array.
|
static <T extends ReadableBinary> |
deserialize(byte[] source,
long skip,
Class<T> clazz)
Convenience function for deserializing an object from a byte array.
|
static <T extends org.openimaj.io.InternalReadable> |
deserialize(byte[] source,
T instance)
Convenience function for deserializing an object from a byte array.
|
static <T extends ReadableASCII> |
fromString(String source,
Class<T> clazz)
Convenience function for deserializing an object from a String.
|
static boolean |
isBinary(BufferedInputStream bis,
byte[] header)
Checks whether a given input stream contains readable binary information
by checking for the first header.length bytes == header.
|
static <T extends ReadableBinary> |
isBinary(BufferedInputStream bis,
T obj)
Extracts the binary header from object and calls the byte[] version
|
static boolean |
isBinary(File f,
byte[] header)
Opens an input stream and calls input stream version
|
static <T extends org.openimaj.io.InternalReadable> |
newInstance(Class<T> cls)
Create a new instance of the given class.
|
static <T extends org.openimaj.io.InternalReadable> |
newInstance(String className)
Create a new instance of the given class.
|
static <T> T |
read(DataInput in)
Utility method to read any object written with
write(Object, DataOutput) . |
static <T extends org.openimaj.io.InternalReadable> |
read(File f)
Read an object from a file.
|
static <T extends org.openimaj.io.InternalReadable> |
read(File f,
Class<T> cls)
Read a new instance of type class from a file.
|
static <T extends org.openimaj.io.InternalReadable> |
read(File f,
Class<T> cls,
String charset)
Read a new instance of type class from a file.
|
static <T extends org.openimaj.io.InternalReadable> |
read(File f,
String charset)
Read an object from a file.
|
static <T extends org.openimaj.io.InternalReadable> |
read(File f,
T obj)
Open file input stream and call Readable#read(InputStream,T)
|
static <T extends org.openimaj.io.InternalReadable> |
read(File f,
T obj,
String charset)
Open file input stream and call Readable#read(InputStream,T)
|
static <T extends org.openimaj.io.InternalReadable> |
read(InputStream ios,
Class<T> cls)
Read a new instance of type class from an input stream.
|
static <T extends org.openimaj.io.InternalReadable> |
read(InputStream ios,
Class<T> cls,
String charset)
Read a new instance of type class from an input stream.
|
static <T extends org.openimaj.io.InternalReadable> |
read(InputStream fis,
T obj)
Read an instance of an object from an input stream.
|
static <T extends org.openimaj.io.InternalReadable> |
read(InputStream fis,
T obj,
String charset)
Read an instance of an object from an input stream.
|
static <T extends org.openimaj.io.InternalReadable> |
read(Reader fis,
Class<T> cls)
Read an instance of an object from a reader.
|
static <T extends org.openimaj.io.InternalReadable> |
read(Reader fis,
T obj)
Read an instance of an object from a reader.
|
static boolean |
readable(BufferedInputStream bis,
byte[] header)
Check whether a stream starts with a header.
|
static <T extends org.openimaj.io.InternalReadable> |
readable(BufferedInputStream bis,
Class<T> cls)
Check whether an InputStream can be read by an instantiated class based
on it's binary and ascii headers.
|
static boolean |
readable(BufferedInputStream bis,
String header)
Check whether an input stream starts with a header string.
|
static boolean |
readable(File f,
byte[] header)
Check readability by checking whether a file starts with a given header.
|
static <T extends org.openimaj.io.InternalReadable> |
readable(File f,
Class<T> cls)
Check whether a given file is readable by a given Writeable class.
|
static boolean |
readable(File f,
String header)
Check whether a file is readable by checking it's first bytes contains
the header.
|
static <T> T |
readFromFile(File in)
Utility method to read any object written with
writeToFile(Object, File) . |
static byte[] |
serialize(WriteableBinary object)
Convenience function for serializing a writeable object as a byte array.
|
static void |
write(Object obj,
DataOutput out)
Writes an object using the Kryo serialisation library.
|
static <T extends WriteableASCII> |
writeASCII(File f,
T obj)
Writeable object is written to the a file in ASCII format.
|
static <T extends WriteableASCII> |
writeASCII(File f,
T obj,
String charset)
Writeable object is written to the a file in ASCII format.
|
static <T extends WriteableASCII> |
writeASCII(OutputStream fos,
T obj)
Write the object in ASCII format to the output stream.
|
static <T extends WriteableASCII> |
writeASCII(OutputStream fos,
T obj,
String charset)
Write the object in ASCII format to the output stream.
|
static <T extends WriteableASCII> |
writeASCII(Writer writer,
T obj)
Write the object in ASCII format to the output stream.
|
static <T extends WriteableASCII> |
writeASCIIFull(File f,
T object)
Write an object to a file fully.
|
static <T extends WriteableASCII> |
writeASCIIFull(File f,
T object,
String charset)
Write an object to a file fully.
|
static <T extends WriteableBinary> |
writeBinary(BufferedOutputStream bos,
T obj)
Writeable object is written to the output stream in binary format.
|
static <T extends WriteableBinary> |
writeBinary(File f,
T obj)
Write a Writeable T object instance.
|
static <T extends WriteableBinary> |
writeBinary(OutputStream fos,
T obj)
Write a Writeable T object instance to the provided output stream, calls
BufferedOutputStream version
|
static <T extends WriteableBinary> |
writeBinaryFull(File f,
T object)
Write an object to a file fully.
|
static void |
writeToFile(Object obj,
File out)
Writes an object to a file using the Kryo serialisation library.
|
public IOUtils()
public static <T extends org.openimaj.io.InternalReadable> T newInstance(Class<T> cls)
T
- The type of object.cls
- The class.public static <T extends org.openimaj.io.InternalReadable> T newInstance(String className)
T
- The type of object.className
- The class name.public static <T extends org.openimaj.io.InternalReadable> T read(File f) throws IOException
T
- instance type expectedf
- the fileIOException
- problem reading filepublic static <T extends org.openimaj.io.InternalReadable> T read(File f, String charset) throws IOException
T
- instance type expectedf
- the filecharset
- the charsetName sent to the reader which reads the file IFF
the file is not binaryIOException
- problem reading filepublic static <T extends WriteableBinary> void writeBinaryFull(File f, T object) throws IOException
read(File)
without needing to know the actual type.T
- instance type expectedf
- the fileobject
- the object to writeIOException
- problem reading filepublic static <T extends WriteableASCII> void writeASCIIFull(File f, T object) throws IOException
read(File)
without needing to know the actual type.T
- instance type expectedf
- the fileobject
- the object to writeIOException
- problem reading filepublic static <T extends WriteableASCII> void writeASCIIFull(File f, T object, String charset) throws IOException
read(File)
without needing to know the actual type.T
- instance type expectedf
- the fileobject
- the object to writecharset
- the charsetName sent to the internal writerIOException
- problem reading filepublic static <T extends org.openimaj.io.InternalReadable> T read(File f, Class<T> cls) throws IOException
T
- instance type expectedf
- the filecls
- the classIOException
- problem reading filepublic static <T extends org.openimaj.io.InternalReadable> T read(File f, Class<T> cls, String charset) throws IOException
T
- instance type expectedf
- the filecls
- the classcharset
- the charsetName sent to the reader which reads the file IFF
the file is not binaryIOException
- problem reading filepublic static <T extends org.openimaj.io.InternalReadable> T read(InputStream ios, Class<T> cls) throws IOException
T
- instance type expectedios
- the input streamcls
- the classIOException
- problem reading streampublic static <T extends org.openimaj.io.InternalReadable> T read(InputStream ios, Class<T> cls, String charset) throws IOException
T
- instance type expectedios
- the input streamcls
- the classcharset
- the charsetName sent to the internal inputstreamreaderIOException
- problem reading streampublic static <T extends org.openimaj.io.InternalReadable> T read(File f, T obj) throws IOException
T
- instance type expectedf
- the fileobj
- the object of type TIOException
- an error reading the filepublic static <T extends org.openimaj.io.InternalReadable> T read(File f, T obj, String charset) throws IOException
T
- instance type expectedf
- the fileobj
- the object of type Tcharset
- the charsetName sent to the reader which reads the file IFF
the file is not binaryIOException
- an error reading the filepublic static <T extends org.openimaj.io.InternalReadable> T read(InputStream fis, T obj) throws IOException
T
- instance type expectedfis
- the input streamobj
- the object to instantiateIOException
- if there is a problem reading the stream from the fileReadableBinary.binaryHeader()
,
ReadableBinary.readBinary(java.io.DataInput)
,
ReadableASCII.readASCII(java.util.Scanner)
public static <T extends org.openimaj.io.InternalReadable> T read(InputStream fis, T obj, String charset) throws IOException
T
- instance type expectedfis
- the input streamobj
- the object to instantiatecharset
- the charsetName sent the to the inputstreamreaderIOException
- if there is a problem reading the stream from the fileReadableBinary.binaryHeader()
,
ReadableBinary.readBinary(java.io.DataInput)
,
ReadableASCII.readASCII(java.util.Scanner)
public static <T extends org.openimaj.io.InternalReadable> T read(Reader fis, T obj) throws IOException
T
- instance type expectedfis
- the input streamobj
- the object to instantiateIOException
- if there is a problem reading the stream from the fileReadableASCII.readASCII(java.util.Scanner)
public static <T extends org.openimaj.io.InternalReadable> T read(Reader fis, Class<T> cls) throws IOException
T
- instance type expectedfis
- the input streamcls
- the object to instantiateIOException
- if there is a problem reading the stream from the fileReadableASCII.readASCII(java.util.Scanner)
public static boolean isBinary(File f, byte[] header) throws IOException
f
- file containing dataheader
- expected header in binary formatIOException
- if there was a problem reading the input streamisBinary(BufferedInputStream,byte[])
public static <T extends ReadableBinary> boolean isBinary(BufferedInputStream bis, T obj) throws IOException
T
- expected data typebis
- stream containing dataobj
- instance of expected data typeIOException
- problem reading input streamisBinary(BufferedInputStream,byte[])
public static boolean isBinary(BufferedInputStream bis, byte[] header) throws IOException
bis
- stream containing dataheader
- expected binary headerIOException
- problem reading or reseting the streampublic static <T extends WriteableBinary> void writeBinary(File f, T obj) throws IOException
T
- data type to be writtenf
- file to write instance toobj
- instance to be writtenIOException
- error reading fileIOUtils#writeBinary(OutputStream,Writeable)
public static <T extends WriteableBinary> void writeBinary(OutputStream fos, T obj) throws IOException
T
- Expected data typefos
- output streamobj
- the object to writeIOException
- error writing to streamIOUtils#writeBinary(BufferedOutputStream,Writeable)
public static <T extends WriteableBinary> void writeBinary(BufferedOutputStream bos, T obj) throws IOException
T
- instance type expectedbos
- the output streamobj
- the object to writeIOException
- error writing to streamWriteableBinary.writeBinary(java.io.DataOutput)
,
WriteableBinary.binaryHeader()
public static <T extends WriteableASCII> void writeASCII(File f, T obj) throws IOException
T
- instance type expectedf
- the file to write toobj
- the object to writeIOException
- error writing to fileIOUtils#writeASCII(OutputStream, Writeable)
public static <T extends WriteableASCII> void writeASCII(File f, T obj, String charset) throws IOException
T
- instance type expectedf
- the file to write toobj
- the object to writecharset
- the charsetName sent to the internal writerIOException
- error writing to fileIOUtils#writeASCII(OutputStream, Writeable)
public static <T extends WriteableASCII> void writeASCII(OutputStream fos, T obj) throws IOException
T
- instance type expectedfos
- the output streamobj
- the objectIOException
- error writing to streamPrintWriter
,
WriteableASCII.asciiHeader()
,
WriteableASCII.writeASCII(PrintWriter)
public static <T extends WriteableASCII> void writeASCII(OutputStream fos, T obj, String charset) throws IOException
T
- instance type expectedfos
- the output streamobj
- the objectcharset
- the charsetName sent to the internal outputstreamwriterIOException
- error writing to streamPrintWriter
,
WriteableASCII.asciiHeader()
,
WriteableASCII.writeASCII(PrintWriter)
public static <T extends WriteableASCII> void writeASCII(Writer writer, T obj) throws IOException
T
- instance type expectedwriter
- the output streamobj
- the objectIOException
- error writing to streamPrintWriter
,
WriteableASCII.asciiHeader()
,
WriteableASCII.writeASCII(PrintWriter)
public static <T extends org.openimaj.io.InternalReadable> boolean readable(File f, Class<T> cls) throws IOException
T
- instance type expectedf
- the file to checkcls
- the class to instantiate the Readable objectIOException
- error reading fileReadableASCII.asciiHeader()
public static boolean readable(File f, String header) throws IOException
f
- the file to checkheader
- the header to check withIOException
- error reading filereadable(File, byte[])
public static boolean readable(File f, byte[] header) throws IOException
f
- file to checkheader
- the expected headerIOException
- error reading fileisBinary(BufferedInputStream, byte[])
public static <T extends org.openimaj.io.InternalReadable> boolean readable(BufferedInputStream bis, Class<T> cls) throws IOException
T
- instance type expectedbis
- the streamcls
- the class to instantiate and checkIOException
- error reading streampublic static boolean readable(BufferedInputStream bis, String header) throws IOException
bis
- the input streamheader
- the headerIOException
- error reading streamisBinary(BufferedInputStream, byte[])
public static boolean readable(BufferedInputStream bis, byte[] header) throws IOException
bis
- the input streamheader
- the byte[] headerIOException
- error reading streampublic static byte[] serialize(WriteableBinary object) throws IOException
writeBinary(OutputStream, WriteableBinary)
on a
ByteArrayOutputStream
then calls
ByteArrayOutputStream.toByteArray()
object
- IOException
public static <T extends ReadableASCII> T fromString(String source, Class<T> clazz) throws IOException
ReadableASCII.readASCII(Scanner)
with a Scanner initialized from
a StringReaderT
- the type to outputsource
- where to read fromclazz
- the class of the outputIOException
public static <T extends ReadableBinary> T deserialize(byte[] source, Class<T> clazz) throws IOException
read(InputStream, Class)
on a
ByteArrayInputStream
.T
- the type to outputsource
- where to read fromclazz
- the class of the outputIOException
public static <T extends ReadableBinary> T deserialize(byte[] source, long skip, Class<T> clazz) throws IOException
read(InputStream, Class)
on a
ByteArrayInputStream
.T
- the type to outputsource
- where to read fromclazz
- the class of the outputskip
- number of bytes to skipIOException
public static <T extends org.openimaj.io.InternalReadable> T deserialize(byte[] source, T instance) throws IOException
read(InputStream, Class)
on a
ByteArrayInputStream
.T
- the type to outputsource
- where to read frominstance
- a T instanceIOException
public static void writeToFile(Object obj, File out) throws IOException
obj
- the object to writeout
- the output sinkIOException
public static void write(Object obj, DataOutput out) throws IOException
obj
- the object to writeout
- the output sinkIOException
public static <T> T read(DataInput in) throws IOException
write(Object, DataOutput)
.T
- type of objectin
- inputIOException
public static <T> T readFromFile(File in) throws IOException
writeToFile(Object, File)
.T
- type of objectin
- input fileIOException
public static boolean canRead(InputStreamObjectReader<?> reader, BufferedInputStream is, String name) throws IOException
InputStream
can be read by the
given InputStreamObjectReader
. This method tries to ensure that
the stream is reset to its initial condition.reader
- the InputStreamObjectReader
.is
- the streamname
- the name of the file/object behind the stream (can be null)InputStreamObjectReader
can read from this
stream; false otherwise.IOException
- if an error occurs resetting the stream.public static <SRC> boolean canRead(ObjectReader<?,SRC> reader, SRC source, String name) throws IOException
ObjectReader
.reader
- the ObjectReader
.source
- the sourcename
- the name of the file/object behind the stream (can be null)ObjectReader
can read from this stream; false
otherwise.IOException
- if an error occurs resetting the stream.