org.omnaest.utils.structure.container
Class ByteArrayContainer

java.lang.Object
  extended by org.omnaest.utils.structure.container.ByteArrayContainer

public class ByteArrayContainer
extends Object

This class is a simple container to hold a byte array.
It additionally offers some simple methods to load the byte array from different ways.

Author:
Omnaest

Field Summary
static String DEFAULTENCODING
          UTF-8
static String DEFAULTZIPFILENAME
           
static String ENCODING_UTF8
           
static Class<? extends ByteArrayContainer> implementationForByteArrayContainerClass
          Used by the factory method.
 
Constructor Summary
ByteArrayContainer()
          Default ByteArrayContainer creating an instance with no content.
ByteArrayContainer(byte[] content)
          Creates an ByteArrayContainer with a copied byte array content.
ByteArrayContainer(CharSequence charsequence)
           
ByteArrayContainer(CharSequence charsequence, String encoding)
           
ByteArrayContainer(InputStream sourceInputStream)
           
ByteArrayContainer(Readable readable)
           
ByteArrayContainer(String content)
           
ByteArrayContainer(String content, String encoding)
           
 
Method Summary
 void clear()
          Clears the content.
 ByteArrayContainer copyFrom(byte[] source)
          Copies the content from a byte array into the container.
 ByteArrayContainer copyFrom(ByteArrayContainer sourceByteArrayContainer)
          Copies the content from another container into this one.
 ByteArrayContainer copyFrom(CharSequence charSequence)
          Copies the content of a CharSequence into the ByteArrayContainer using the "utf-8".
 ByteArrayContainer copyFrom(CharSequence charSequence, String encoding)
          Copies the content of a CharSequence into the ByteArrayContainer.
 ByteArrayContainer copyFrom(File file)
          Copies the content of the given File into the ByteArrayContainer.
 ByteArrayContainer copyFrom(InputStream sourceInputStream)
          Copies the content from an InputStream into the ByteArrayContainer without closing the InputStream

If the copy operation fails the content of the ByteArrayContainer is set to invalid.
 ByteArrayContainer copyFrom(Readable readable)
          Copies the content from a Readable using the "utf-8" encoding
 ByteArrayContainer copyFrom(Readable readable, String encoding)
          Copies the content from a Readable using the given encoding
 ByteArrayContainer copyFrom(String string)
          Copies the content of a String into the container.
 ByteArrayContainer copyFrom(String string, String encoding)
          Copies the content of a String into the ByteArrayContainer.
 ByteArrayContainer copyFrom(URL url)
          Uses the given URL.openStream() to retrieve data which will be available within the ByteArrayContainer

If the data cannot be resolved completely the isContentInvalid() will return true afterwards.
 ByteArrayContainer copyFrom(URLConnection urlConnection)
          Similar to copyFrom(URL)
<S extends Serializable>
ByteArrayContainer
copyFromAsSerialized(S element)
          Serializes any given Serializable element and stores it
static ByteArrayContainer createNewInstance()
          Creates a new instance of this class.
 void download(String urlStr)
          Downloads the content from the given url resource.
 void download(URI uri)
           
 void download(URL url)
          Downloads the content from the given url resource.
 byte[] getContent()
           
 InputStream getInputStream()
           
 OutputStream getOutputStream()
          Returns an outputstream, that allows to write the byte array content directly.
 OutputStreamWriter getOutputStreamWriter()
          Like getOutputStreamWriter(String) using the DEFAULTENCODING
 OutputStreamWriter getOutputStreamWriter(String encoding)
          Returns an OutputStreamWriter using the given encoding
 PrintStream getPrintStreamWriter()
          Returns a new PrintStream using the "utf-8" encoding
 PrintStream getPrintStreamWriter(String encoding)
          Returns a new PrintStream
 PrintStream getPrintStreamWriter(String encoding, boolean autoFlush)
          Returns a new PrintStream
 Reader getReader()
          Returns a Reader with the default encoding "utf-8"
 Reader getReader(String encoding)
          Returns a Reader using the given encoing
 boolean isContentInvalid()
          Returns true if a previous operation on the ByteArrayContainer has put the content into a malformed state.
 boolean isEmpty()
           
 boolean isNotEmpty()
           
 void load(File file)
          Deprecated. use copyFrom(File) instead
 void save(File file)
          Deprecated. use writeTo(File) instead
 ByteArrayContainer setContent(byte[] content)
           
 void setContentInvalid(boolean isContentInvalid)
          Sets the content of the ByteArrayContainer to be marked as invalid
 ByteArrayContainer setExceptionHandler(ExceptionHandler exceptionHandler)
           
<S extends Serializable>
S
toDeserializedElement()
          Deserializes the content into an element
 String toString()
          Transforms the content into a String.
 String toString(String encoding)
          Transforms the content into a String.
 List<String> toStringList()
          Returns the content as a list of strings, separated by line feed and/or carriage return.
 List<String> toStringList(String encoding)
          Returns the content as a list of strings, separated by line feed and/or carriage return.
 List<String> toStringList(String encoding, String regExDelimiter)
          Returns the content as a list of strings, separated by an arbitrary regular expression.
 Map<String,ByteArrayContainer> unzip()
          Converts a zip file content into unzipped content.
static Map<String,ByteArrayContainer> unzipIntoFilenameByteArrayContainerMap(ByteArrayContainer byteArrayContainer)
          Unzipps the given ByteArrayContainer object into a map containing the filenames and unzipped ByteArrayContainer objects for each file.
static Map<String,ByteArrayContainer> unzipIntoFilenameByteArrayContainerMap(ByteArrayContainer byteArrayContainer, ExceptionHandler exceptionHandler)
          Unzipps the given ByteArrayContainer object into a map containing the filenames and unzipped ByteArrayContainer objects for each file.
static ByteArrayContainer valueOf(ByteArrayContainer byteArrayContainer)
          Returns a new ByteArrayContainer instance mapping the same byte[] array as the given ByteArrayContainer
 boolean writeTo(Appendable appendable, String encoding)
          Writes the content of the ByteArrayContainer to an Appendable e.g. a StringBuilder or StringBuffer
 void writeTo(File file)
          Writes the content of the ByteArrayContainer to the given File
 boolean writeTo(OutputStream outputStream)
          Writes the content of the ByteArrayContainer to a given OutputStream without closing the OutputStream but flushing the content to it.
 boolean writeTo(Writer writer)
          Similar to writeTo(Writer, String) using the default encoding "utf-8"
 boolean writeTo(Writer writer, String encoding)
          Writes the content of the ByteArrayContainer to the given Writer instance.
 void zip()
           
 void zip(String zipFileName)
          Converts the content into a zip file content.
static ByteArrayContainer zipFilenameByteArrayContainerMap(Map<String,ByteArrayContainer> byteArrayContainerMap)
          Zipps all ByteArrayContainers of a given map, which contains filenames with corresponding unzipped ByteArrayContainer objects.
static ByteArrayContainer zipFilenameByteArrayContainerMap(Map<String,ByteArrayContainer> byteArrayContainerMap, ExceptionHandler exceptionHandler)
          Zipps all ByteArrayContainers of a given map, which contains filenames with corresponding unzipped ByteArrayContainer objects.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ENCODING_UTF8

public static final String ENCODING_UTF8
See Also:
Constant Field Values

DEFAULTENCODING

public static final String DEFAULTENCODING
UTF-8

See Also:
Constant Field Values

DEFAULTZIPFILENAME

public static final String DEFAULTZIPFILENAME
See Also:
Constant Field Values

implementationForByteArrayContainerClass

public static Class<? extends ByteArrayContainer> implementationForByteArrayContainerClass
Used by the factory method.

See Also:
createNewInstance()
Constructor Detail

ByteArrayContainer

public ByteArrayContainer()
Default ByteArrayContainer creating an instance with no content.


ByteArrayContainer

public ByteArrayContainer(byte[] content)
Creates an ByteArrayContainer with a copied byte array content.

Parameters:
content -
See Also:
copyFrom(byte[])

ByteArrayContainer

public ByteArrayContainer(String content)
Parameters:
content -
See Also:
copyFrom(String)

ByteArrayContainer

public ByteArrayContainer(String content,
                          String encoding)
Parameters:
content -
See Also:
copyFrom(String, String)

ByteArrayContainer

public ByteArrayContainer(CharSequence charsequence,
                          String encoding)
Parameters:
charsequence -
See Also:
copyFrom(CharSequence)

ByteArrayContainer

public ByteArrayContainer(InputStream sourceInputStream)
Parameters:
sourceInputStream -
See Also:
copyFrom(InputStream)

ByteArrayContainer

public ByteArrayContainer(CharSequence charsequence)
Parameters:
charsequence -
See Also:
copyFrom(CharSequence)

ByteArrayContainer

public ByteArrayContainer(Readable readable)
Parameters:
readable -
See Also:
ByteArrayContainer, copyFrom(Readable)
Method Detail

isEmpty

public boolean isEmpty()
Returns:
true: content is empty, false: container has content with size > 0
See Also:
isNotEmpty()

isNotEmpty

public boolean isNotEmpty()
Returns:
false: content is empty, true: container has content with size > 0
See Also:
isEmpty()

download

public void download(URI uri)
Parameters:
uri -
See Also:
download(URL)

download

public void download(URL url)
Downloads the content from the given url resource.

Parameters:
url -
See Also:
download(URI), download(String)

download

public void download(String urlStr)
Downloads the content from the given url resource. Ensure the urlStr is encoded correctly and is valid at all.

Parameters:
urlStr -
See Also:
download(URI), download(URL)

load

@Deprecated
public void load(File file)
Deprecated. use copyFrom(File) instead

Loads the content of the given file into the container.

Parameters:
file -

copyFrom

public ByteArrayContainer copyFrom(File file)
Copies the content of the given File into the ByteArrayContainer.

If the operation fails the isContentInvalid() will return true

Parameters:
file - File
Returns:
this
See Also:
isContentInvalid()

save

@Deprecated
public void save(File file)
Deprecated. use writeTo(File) instead

Saves the content of the ByteArrayContainer to a given File

Parameters:
file - File

writeTo

public void writeTo(File file)
Writes the content of the ByteArrayContainer to the given File

Parameters:
file - File

copyFrom

public ByteArrayContainer copyFrom(ByteArrayContainer sourceByteArrayContainer)
Copies the content from another container into this one.

Parameters:
sourceByteArrayContainer -
Returns:
this

copyFrom

public ByteArrayContainer copyFrom(InputStream sourceInputStream)
Copies the content from an InputStream into the ByteArrayContainer without closing the InputStream

If the copy operation fails the content of the ByteArrayContainer is set to invalid.

Parameters:
sourceInputStream -
Returns:
this
See Also:
isContentInvalid()

copyFrom

public ByteArrayContainer copyFrom(URL url)
Uses the given URL.openStream() to retrieve data which will be available within the ByteArrayContainer

If the data cannot be resolved completely the isContentInvalid() will return true afterwards.

Parameters:
url - URL
Returns:
this
See Also:
isContentInvalid(), copyFrom(InputStream)

copyFrom

public ByteArrayContainer copyFrom(URLConnection urlConnection)
Similar to copyFrom(URL)

Parameters:
urlConnection - URLConnection
Returns:
this

copyFrom

public ByteArrayContainer copyFrom(Readable readable)
Copies the content from a Readable using the "utf-8" encoding

Parameters:
readable -
Returns:
this

copyFrom

public ByteArrayContainer copyFrom(Readable readable,
                                   String encoding)
Copies the content from a Readable using the given encoding

Parameters:
readable -
encoding -
Returns:
this

copyFrom

public ByteArrayContainer copyFrom(String string,
                                   String encoding)
Copies the content of a String into the ByteArrayContainer.

If the copy operation fails the content of the ByteArrayContainer is set to invalid.

Parameters:
string -
encoding -
Returns:
this
See Also:
isContentInvalid()

copyFrom

public ByteArrayContainer copyFrom(String string)
Copies the content of a String into the container.

Parameters:
string -
Returns:
this

copyFrom

public ByteArrayContainer copyFrom(CharSequence charSequence)
Copies the content of a CharSequence into the ByteArrayContainer using the "utf-8".

Parameters:
charSequence -
Returns:
this

copyFrom

public ByteArrayContainer copyFrom(CharSequence charSequence,
                                   String encoding)
Copies the content of a CharSequence into the ByteArrayContainer.

Parameters:
charSequence -
encoding -
Returns:
this

copyFrom

public ByteArrayContainer copyFrom(byte[] source)
Copies the content from a byte array into the container. The data is copied and not the given byte array is used.

Parameters:
source -
Returns:
this

copyFromAsSerialized

public <S extends Serializable> ByteArrayContainer copyFromAsSerialized(S element)
Serializes any given Serializable element and stores it

Parameters:
element -
Returns:
this
See Also:
toDeserializedElement()

toDeserializedElement

public <S extends Serializable> S toDeserializedElement()
Deserializes the content into an element

Returns:
new element instance
See Also:
copyFromAsSerialized(Serializable)

toString

public String toString()
Transforms the content into a String. As default utf-8 is used.

Overrides:
toString in class Object
See Also:
toString(String)

toString

public String toString(String encoding)
Transforms the content into a String.

Parameters:
encoding - specifies the encoding of the binary data. Example: "utf-8"
Returns:
String, null if error happens
See Also:
toString()

toStringList

public List<String> toStringList()
Returns the content as a list of strings, separated by line feed and/or carriage return. The default encoding "utf-8" is used.

Returns:

toStringList

public List<String> toStringList(String encoding)
Returns the content as a list of strings, separated by line feed and/or carriage return.

Parameters:
encoding - : for example = "utf-8"
Returns:

toStringList

public List<String> toStringList(String encoding,
                                 String regExDelimiter)
Returns the content as a list of strings, separated by an arbitrary regular expression.

Parameters:
encoding - : for example = "utf-8"
Returns:
See Also:
ENCODING_UTF8

writeTo

public boolean writeTo(Writer writer,
                       String encoding)
Writes the content of the ByteArrayContainer to the given Writer instance. The Writer.flush() method will be invoked afterwards but it will not be closed.

Parameters:
writer -
encoding -
Returns:
true, if no error occurs
See Also:
writeTo(Writer)

writeTo

public boolean writeTo(Writer writer)
Similar to writeTo(Writer, String) using the default encoding "utf-8"

Parameters:
writer -
Returns:
true, if no error occurs
See Also:
writeTo(Writer, String)

writeTo

public boolean writeTo(Appendable appendable,
                       String encoding)
Writes the content of the ByteArrayContainer to an Appendable e.g. a StringBuilder or StringBuffer

Parameters:
appendable -
encoding -
Returns:
true : transfer successful

writeTo

public boolean writeTo(OutputStream outputStream)
Writes the content of the ByteArrayContainer to a given OutputStream without closing the OutputStream but flushing the content to it.

Parameters:
outputStream -
Returns:
true : transfer was successful

clear

public void clear()
Clears the content.


getContent

public byte[] getContent()
Returns:

setContent

public ByteArrayContainer setContent(byte[] content)
Parameters:
content -
Returns:

getInputStream

public InputStream getInputStream()
Returns:

getReader

public Reader getReader()
Returns a Reader with the default encoding "utf-8"

Returns:

getReader

public Reader getReader(String encoding)
Returns a Reader using the given encoing

Parameters:
encoding -
Returns:

getOutputStream

public OutputStream getOutputStream()
Returns an outputstream, that allows to write the byte array content directly.
Be aware of the fact, that the content is written only if the outputstream is closed correctly!

Returns:

getOutputStreamWriter

public OutputStreamWriter getOutputStreamWriter(String encoding)
Returns an OutputStreamWriter using the given encoding

Parameters:
encoding -
Returns:

getOutputStreamWriter

public OutputStreamWriter getOutputStreamWriter()
Like getOutputStreamWriter(String) using the DEFAULTENCODING

Returns:
See Also:
getOutputStreamWriter(String)

zip

public void zip(String zipFileName)
Converts the content into a zip file content. For example, load a file content, call this method, and save the content back to the same file, to make the file zipped.


zip

public void zip()
See Also:
zip(String)

zipFilenameByteArrayContainerMap

public static ByteArrayContainer zipFilenameByteArrayContainerMap(Map<String,ByteArrayContainer> byteArrayContainerMap)
Zipps all ByteArrayContainers of a given map, which contains filenames with corresponding unzipped ByteArrayContainer objects.

Parameters:
byteArrayContainerMap -

zipFilenameByteArrayContainerMap

public static ByteArrayContainer zipFilenameByteArrayContainerMap(Map<String,ByteArrayContainer> byteArrayContainerMap,
                                                                  ExceptionHandler exceptionHandler)
Zipps all ByteArrayContainers of a given map, which contains filenames with corresponding unzipped ByteArrayContainer objects.

Parameters:
byteArrayContainerMap -
exceptionHandler - ExceptionHandler

unzip

public Map<String,ByteArrayContainer> unzip()
Converts a zip file content into unzipped content. For example load a zip file, and call this method, to get the unzipped content of the file. If an error occurs, the content will be set to null, and null will be returned.

Ensure the zip file contains only one file, if you wanna use the current ByteArrayContainer obejct. Only the first file is read for the current container, all others are only saved within the returned map.

Returns:
Map of the zip file names with unzipped ByteArrayContainer objects, or null, if unzipping was unsuccesful.
See Also:
unzipIntoFilenameByteArrayContainerMap(ByteArrayContainer)

unzipIntoFilenameByteArrayContainerMap

public static Map<String,ByteArrayContainer> unzipIntoFilenameByteArrayContainerMap(ByteArrayContainer byteArrayContainer)
Unzipps the given ByteArrayContainer object into a map containing the filenames and unzipped ByteArrayContainer objects for each file.

Parameters:
byteArrayContainer -
Returns:

unzipIntoFilenameByteArrayContainerMap

public static Map<String,ByteArrayContainer> unzipIntoFilenameByteArrayContainerMap(ByteArrayContainer byteArrayContainer,
                                                                                    ExceptionHandler exceptionHandler)
Unzipps the given ByteArrayContainer object into a map containing the filenames and unzipped ByteArrayContainer objects for each file.

Parameters:
byteArrayContainer -
exceptionHandler - ExceptionHandler
Returns:

createNewInstance

public static ByteArrayContainer createNewInstance()
Creates a new instance of this class.

See Also:
implementationForByteArrayContainerClass

isContentInvalid

public boolean isContentInvalid()
Returns true if a previous operation on the ByteArrayContainer has put the content into a malformed state.

Returns:
See Also:
copyFrom(InputStream), copyFrom(String)

setContentInvalid

public void setContentInvalid(boolean isContentInvalid)
Sets the content of the ByteArrayContainer to be marked as invalid

Parameters:
isContentInvalid -

getPrintStreamWriter

public PrintStream getPrintStreamWriter()
Returns a new PrintStream using the "utf-8" encoding

Returns:
See Also:
PrintStream

getPrintStreamWriter

public PrintStream getPrintStreamWriter(String encoding)
Returns a new PrintStream

Parameters:
encoding -
Returns:
See Also:
PrintStream

getPrintStreamWriter

public PrintStream getPrintStreamWriter(String encoding,
                                        boolean autoFlush)
Returns a new PrintStream

Parameters:
encoding -
autoFlush -
Returns:
See Also:
PrintStream

setExceptionHandler

public ByteArrayContainer setExceptionHandler(ExceptionHandler exceptionHandler)
Parameters:
exceptionHandler - ExceptionHandler
Returns:
this

valueOf

public static ByteArrayContainer valueOf(ByteArrayContainer byteArrayContainer)
Returns a new ByteArrayContainer instance mapping the same byte[] array as the given ByteArrayContainer

Parameters:
byteArrayContainer -
Returns:
new instance


Copyright © 2013. All Rights Reserved.