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 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(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 DEFAULTENCODING.
 ByteArrayContainer copyFrom(CharSequence charSequence, String encoding)
          Copies the content of a CharSequence 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(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.
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.
 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)
          Loads the content of the given file into the container.
 void save(File file)
          Saves the content of the ByteArrayContainer to a given File
 ByteArrayContainer setContent(byte[] content)
           
 void setContentInvalid(boolean isContentInvalid)
          Sets the content of the ByteArrayContainer to be marked as invalid
 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.
 boolean writeTo(Appendable appendable, String encoding)
          Writes the content of the ByteArrayContainer to an Appendable e.g. a StringBuilder or StringBuffer
 boolean writeTo(OutputStream outputStream)
          Writes the content of the ByteArrayContainer to a given OutputStream without closing the OutputStream but flushing the content to it.
 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.
 
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

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(CharSequence charsequence)
Parameters:
charsequence -
See Also:
copyFrom(CharSequence)
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

public void load(File file)
          throws IOException
Loads the content of the given file into the container.

Parameters:
file -
Throws:
IOException

save

public void save(File file)
          throws IOException
Saves the content of the ByteArrayContainer to a given File

Parameters:
file -
Throws:
IOException

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(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 DEFAULTENCODING.

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

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(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()

setContent

public ByteArrayContainer setContent(byte[] content)

getInputStream

public InputStream getInputStream()

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:

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 -

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:

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 -


Copyright © 2011. All Rights Reserved.