java.io.Serializable
, java.lang.Cloneable
public class FileToBytesExporter
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable
Files to bytes exporter provides a custom, fast class for generating binary files from plain (or not) text files.
Binary files are present at every computer and the have some interesting advantages facing regular text files:
This class allows you to easily generate binary files from regular text files with some simple methods and attributes. In addition, it supports glob syntax, so it is even easier to find and convert lots of files with just one line.
Modifier and Type | Class | Description |
---|---|---|
private static class |
FileToBytesExporter.Glob
|
Class for searching and obtaining files that
corresponds to a custom glob.
|
private static class |
FileToBytesExporter.Lock
|
Private custom class for synchronizing
monitor locks.
|
Modifier and Type | Field | Description |
---|---|---|
private java.lang.Object |
lock
|
|
private java.lang.String |
mFilename
|
|
private java.lang.String |
mFileSeparator
|
|
private boolean |
mMustOpenSourcePath
|
|
private java.util.ArrayList<java.lang.String>
|
mPath
|
|
private java.lang.String |
mReadData
|
Modifier | Constructor | Description |
---|---|---|
|
FileToBytesExporter()
|
|
|
FileToBytesExporter(java.lang.String filename)
|
|
|
FileToBytesExporter(java.lang.String filename,
boolean mustOpenSourcePath,
java.lang.String... paths) |
Constructor that uses an extra attribute for
declaring whether or not the source path will be
used also for searching the file.
|
|
FileToBytesExporter(java.lang.String filename,
java.lang.String... paths) |
|
private |
FileToBytesExporter(java.lang.String filename,
java.util.ArrayList<java.lang.String> paths,
java.lang.String readData,
java.lang.String fileSeparator,
boolean mustOpenSourcePath) |
Private constructor for cloning or generating
a new instance - only visible for this class.
|
Modifier and Type | Method | Description |
---|---|---|
void |
addPath(java.lang.String newPath)
|
Includes a new path inside the stored paths -
if
path
is not created, it
generates a new instance by using setPaths(String...)
method.
|
protected java.lang.Object |
clone()
|
Creates and returns a copy of this object.
|
boolean |
equals(java.lang.Object o)
|
Indicates whether some other object is "equal
to" this one.
|
java.lang.String |
getFileSeparator()
|
Obtains the file separator used when
writing/reading the files - can be null if only one file
was read/written
|
protected static java.lang.String
|
getHash(java.lang.String source)
|
Obtains the hash of the specified
source
by using MessageDigest.digest(byte[]) method,
generating a SHA-256 hash .
|
java.lang.String |
getReadData()
|
Obtains all the read data obtained after
executing
readSource()
methods
|
int |
hashCode()
|
Returns a hash code value for the object.
|
void |
readObject(java.io.File source)
|
Reads the data contained at
source obtaining its file
separator (used if more than one file was
read) and the file
data .
|
void |
readObject(java.io.InputStream source)
|
Reads the data contained at
source obtaining its file
separator (used if more than one file was
read) and the file
data .
|
void |
readSource()
|
Reads the source file and saves its contents
inside a
String object.
|
void |
readSource(boolean mustOpenAllFiles)
|
Reads all the sources file that were found at
the current paths and inside all the provided
paths if
mustOpenAllFiles is true ,
appending every file at the end with a
double blank line.
|
void |
readSource(boolean mustOpenAllFiles,
java.lang.String fileSeparator) |
Reads all the sources file that were found at
the current paths and inside all the provided
paths if
mustOpenAllFiles is true ,
appending every file at the end the value
passed at fileSeparator .
|
static java.lang.String |
readSource(java.io.File source)
|
Reads the provided file and returns the
String that contains its content.
|
void |
setFilename(java.lang.String filename)
|
Updates the filename once the
class
is created
|
void |
setMustOpenSourcePath(boolean mustOpenSourcePath)
|
Updates the policy for reading also the
source directory for the provided filename.
|
void |
setPaths(java.lang.String... paths)
|
Updates the paths once the
class
is created
|
java.lang.String |
toString()
|
Returns a string representation of the
object.
|
void |
writeObject(java.io.File destination)
|
Writes the read object to the specified
destination given at
destination .
|
void |
writeObject(java.io.OutputStream destination)
|
Writes the read object to the specified
destination given at
destination .
|
static void |
writeObject(java.lang.String source,
java.io.File destination) |
Writes the read object to the specified
destination given at
destination .
|
static void |
writeObject(java.lang.String source,
java.io.OutputStream destination) |
Writes the read object to the specified
destination given at
destination .
|
private java.lang.String mFilename
private java.util.ArrayList<java.lang.String> mPath
private java.lang.String mReadData
private java.lang.String mFileSeparator
private boolean mMustOpenSourcePath
private final java.lang.Object lock
public FileToBytesExporter()
String
public FileToBytesExporter(java.lang.String filename)
filename
- source file.String
public FileToBytesExporter(java.lang.String filename, java.lang.String... paths)
filename
- source file.paths
- list of paths where to search for the
source file.
String
public FileToBytesExporter(java.lang.String filename, boolean mustOpenSourcePath, java.lang.String... paths)
filename
- source file.mustOpenSourcePath
- true
if the
source path is included while reading the
file.
paths
- list of paths where to search for the
source file.
private FileToBytesExporter(java.lang.String filename, java.util.ArrayList<java.lang.String> paths, java.lang.String readData, java.lang.String fileSeparator, boolean mustOpenSourcePath)
filename
- source file.paths
- list of paths where search for the
source file.
readData
- current read data.fileSeparator
- current used file separator.
mustOpenSourcePath
- whether the source path
should be used for searching files.
public void setFilename(java.lang.String filename)
class
is created
filename
- new filenamepublic void setPaths(java.lang.String... paths)
class
is created
paths
- new pathspublic void setMustOpenSourcePath(boolean mustOpenSourcePath)
mustOpenSourcePath
- whether the source
directory is used or not.
public void addPath(java.lang.String newPath)
path
is not created, it
generates a new instance by using setPaths(String...)
method.
newPath
- new path to includepublic void readSource() throws java.io.IOException
String
object.
MultipleFilesFoundError
- if multiple files were found and
mustOpenAllFiles
is
false
.
InvalidPathException
- when one of the provided paths does not exists or any
other
error happened (like having not enough permissions).
java.io.FileNotFoundException
- when the
filename is not found at any of the directories.
java.io.IOException
public void readSource(boolean mustOpenAllFiles) throws java.io.IOException
mustOpenAllFiles
is true
,
appending every file at the end with a
double blank line.
mustOpenAllFiles
- whether if more than one
source file was found it must be also read
and appended at the end of the current file.
MultipleFilesFoundError
- if multiple files were found and
mustOpenAllFiles
is
false
.
InvalidPathException
- when one of the provided paths does not exists or any
other
error happened (like having not enough permissions).
java.io.FileNotFoundException
- when the
filename is not found at any of the directories.
java.io.IOException
public void readSource(boolean mustOpenAllFiles, java.lang.String fileSeparator) throws java.io.IOException
mustOpenAllFiles
is true
,
appending every file at the end the value
passed at fileSeparator
.
mustOpenAllFiles
- whether if more than one
source file was found it must be also read
and appended at the end of the current file. In addition, if
is set
to true
, it will navigate through all the child
directories
for file matches with the same name - pattern specified at
FileToBytesExporter(String)
or
setFilename(String)
.
fileSeparator
- the String
that
will be added at the end of the just read
file if any other file was found.
MultipleFilesFoundError
- if multiple files were found and
mustOpenAllFiles
is false
.
InvalidFileSeparatorException
- when multiple files were found and
fileSeparator
is null.
InvalidPathException
- when one of the provided paths does not exists or any
other error happened (like having not enough
permissions).
java.io.FileNotFoundException
- when the
filename is not found at any of the
directories.
java.io.IOException
- when looking for a file
using glob and any error
occurred
FileToBytesExporter.Glob.match(File,
String, boolean)
public static java.lang.String readSource(java.io.File source) throws java.io.IOException
String
that contains its content.
source
- the file that will be read.String
with the file data.java.io.IOException
- if the file does not
exists or there is any error while reading it.
public java.lang.String getReadData()
readSource()
methods
String
with the datapublic java.lang.String getFileSeparator()
String
with the file separatorpublic void writeObject(java.io.File destination) throws java.io.IOException
destination
. If it does
not exists, com.github.javinator9889.exporter.FileToBytesExporter
will create all the
necessary directories in order to work as expected.
destination
- relative or complete path to the
output file - cannot be only dir.
java.io.IOException
- when there is an error by
creating necessary directories or by writing
the file.
public void writeObject(java.io.OutputStream destination) throws java.io.IOException
destination
. If it does
not exists, com.github.javinator9889.exporter.FileToBytesExporter
will create all the
necessary directories in order to work as expected.
destination
- subclass of
OutputStream
which contains the output file
that can
be used at ObjectOutputStream
constructor
(e.g.: FileOutputStream
).
java.io.IOException
- when there is an error
while writing the file.
public static void writeObject(java.lang.String source, java.io.File destination) throws java.io.IOException
destination
. If it does
not exists, com.github.javinator9889.exporter.FileToBytesExporter
will create all the
necessary directories in order to work as expected.
source
- the source that will be wrote to the
destination - use readSource(File)
for obtaining the data.
destination
- relative or complete path to the
output file - cannot be only dir.
java.io.IOException
- when there is an error by
creating necessary directories or by writing
the file.
public static void writeObject(java.lang.String source, java.io.OutputStream destination) throws java.io.IOException
destination
. If it does
not exists, com.github.javinator9889.exporter.FileToBytesExporter
will create all the
necessary directories in order to work as expected.
source
- the source that will be wrote to the
destination - use readSource(File)
for obtaining the data.
destination
- subclass of
OutputStream
which contains the output file
that can
be used at ObjectOutputStream
constructor
(e.g.: FileOutputStream
).
java.io.IOException
- when there is an error
while writing the file.
public void readObject(java.io.File source) throws java.io.IOException, java.lang.ClassCastException
source
obtaining its file
separator
(used if more than one file was read)
and the file
data
.
Both file separator and data can be obtained by using getFileSeparator()
and
getReadData()
source
- relative or complete path to the file
- cannot be only a directory
java.io.IOException
- if the file was not
found
, there was
an error recovering the data or it is a
directory
.
java.lang.ClassCastException
- if the retrieved
data is not a String[]
FileError
- if the obtained hash from file is not the same as the
generated
one from the data extracted from the file.
public void readObject(java.io.InputStream source) throws java.io.IOException, java.lang.ClassCastException
source
obtaining its file
separator
(used if more than one file was read)
and the file
data
.
Both file separator and data can be obtained by using getFileSeparator()
and
getReadData()
source
- InputStream
subclass that
can be used at ObjectInputStream(InputStream)
constructor (e.g.: FileInputStream
).
java.io.IOException
- if there was an error
recovering the data.
java.lang.ClassCastException
- if the retrieved
data is not a String[]
FileError
- if the obtained hash from file is not the same as the
generated
one from the data extracted from the file.
protected static java.lang.String getHash(java.lang.String source)
source
by using
MessageDigest.digest(byte[])
method, generating a
SHA-256 hash
. If it fails because no SHA-256
hashing
algorithm
was found, it just returns the String.hashCode()
casted to
String
.
You can @Override
this method if you inherit
from FileToBytesExporter
if you
need any other StandardCharsets
charset.
source
- original String
where
obtaining bytes from.
String
with the SHA-256 hash of
source
.
public boolean equals(java.lang.Object o)
The equals
method implements an equivalence
relation on non-null object references:
x
, x.equals(x)
should return
true
.
x
and y
,
x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
x
and y
, multiple invocations
of x.equals(y)
consistently return
true
or consistently return
false
, provided no information used in
equals
comparisons on the objects is
modified.
x
,
x.equals(null)
should return
false
.
The equals
method for class Object
implements the most discriminating
possible equivalence relation on objects; that is, for any
non-null reference values
x
and y
, this method returns
true
if and only if x
and
y
refer to the same object (x == y
has the value
true
).
Note that it is generally necessary to override the hashCode
method whenever this
method is overridden, so as to maintain the general contract
for the hashCode
method,
which states that equal objects must have equal hash codes.
equals
in class java.lang.Object
o
- the reference object with which to compare.
true
if this object is the same as the obj
argument; false
otherwise.
hashCode()
,
HashMap
public int hashCode()
HashMap
.
The general contract of hashCode
is:
hashCode
method must consistently return
the
same integer, provided no information used in equals
comparisons on the object is
modified. This integer need not remain consistent from
one execution of an application to
another execution of the same application.
equals(Object)
method, then calling the hashCode
method on
each of the two objects must produce the
same integer result.
Object.equals(java.lang.Object)
method,
then calling the
hashCode
method on each of the two objects
must produce distinct integer results.
However, the programmer should be aware that producing
distinct integer results for unequal
objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method
defined by class Object
does
return distinct integers for distinct objects. (The hashCode
may or may not be implemented as
some function of an object's memory address at some point in
time.)
hashCode
in class java.lang.Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
protected java.lang.Object clone()
x
, the expression:
will be true, and that the expression:x.clone() != x
will bex.clone().getClass() == x.getClass()
true
, but these are not absolute
requirements. While it is typically the case
that:
will bex.clone().equals(x)
true
, this is not an absolute requirement.
By convention, the returned object should be obtained by
calling super.clone
. If a
class and all of its superclasses (except
Object
) obey this convention, it will be
the case that x.clone().getClass() ==
x.getClass()
.
By convention, the object returned by this method should be
independent of this object (which
is being cloned). To achieve this independence, it may be
necessary to modify one or more
fields of the object returned by super.clone
before returning it. Typically, this
means copying any mutable objects that comprise the internal
"deep structure" of the object
being cloned and replacing the references to these objects
with references to the copies. If
a class contains only primitive fields or references to
immutable objects, then it is usually
the case that no fields in the object returned by super.clone
need to be modified.
The method clone
for class Object
performs a specific cloning operation.
First, if the class of this object does not implement the
interface Cloneable
, then a
CloneNotSupportedException
is thrown. Note that
all arrays are considered to
implement the interface Cloneable
and that the
return type of the clone
method of an array type T[]
is T[]
where T is any reference or primitive
type. Otherwise, this method creates a new instance of the
class of this object and
initializes all its fields with exactly the contents of the
corresponding fields of this
object, as if by assignment; the contents of the fields are
not themselves cloned. Thus, this
method performs a "shallow copy" of this object, not a "deep
copy" operation.
The class Object
does not itself implement the
interface Cloneable
, so
calling the clone
method on an object whose
class is Object
will result in
throwing an exception at run time.
clone
in
class java.lang.Object
Cloneable
public java.lang.String toString()
toString
method
returns a string that "textually represents" this object. The
result should be a concise but
informative representation that is easy for a person to read. It
is recommended that all
subclasses override this method.
The toString
method for class
Object
returns a string consisting of the name
of the class of which the object is an instance, the at-sign
character `@
', and the
unsigned hexadecimal representation of the hash code of the
object. In other words, this
method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
toString
in class java.lang.Object