Top Description Inners Fields Constructors Methods
org.jvnet.staxex

public Class Base64Data

extends Object
implements CharSequence, Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable, java.lang.CharSequence
Imports
jakarta.activation.DataHandler, .DataSource, java.io.ByteArrayInputStream, .ByteArrayOutputStream, .File, .FileOutputStream, .IOException, .InputStream, .OutputStream, javax.xml.stream.XMLStreamException, .XMLStreamWriter, java.util.logging.Level, .Logger

Binary data represented as base64-encoded string in XML.

Used in conjunction with XMLStreamReaderEx and XMLStreamWriterEx.

Author
Kohsuke Kawaguchi, Martin Grebac

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private class
private static class

Field Summary

Modifier and TypeField and Description
private static final int
private byte[]
private boolean
dataCloneByRef

True if data can be cloned by reference if Base64Data instance is cloned.

private DataHandler
private int
dataLen

Length of the valid data in data.

private String
private static final Logger
private String
mimeType

Optional MIME type of data.

Constructor Summary

AccessConstructor and Description
public
Base64Data()

Default constructor

public
Base64Data(Base64Data
needs to be cloned
that
)

Clone constructor

Method Summary

Modifier and TypeMethod and Description
public char
charAt(int index)

Implements java.lang.CharSequence.charAt.

Encode this binary data in the base64 encoding and returns the character at the specified position.

public Base64Data
clone()

Overrides java.lang.Object.clone.

Creates and returns a copy of this object.

public byte[]

Returns:

data as byte[], the array may be larger
get
()

Gets the raw data.

public DataHandler

Returns:

DataHandler for the data
getDataHandler
()

Gets the raw data.

public int

Returns:

no of bytes
getDataLen
()

Gets the length of the binary data counted in bytes.

public byte[]

Returns:

byte[] for data
getExact
()

Gets the byte[] of the exact length.

public String
public InputStream

Returns:

data as InputStream
getInputStream
()

Gets the data as an InputStream.

public String
pack-priv static String
getProperty(final String propName)

public boolean

Returns:

false if it has only DataHandler
hasData
()

Returns false if this object only has DataHandler and therefore get() operation is likely going to be expensive.

public int
length()

Implements java.lang.CharSequence.length.

Gets the number of characters needed to represent this binary data in the base64 encoding.

public void
set(byte[]
actual data
data
,
int
data[0] to data[len-1] are treated as the data.
len
,
String
MIME type
mimeType
,
boolean
true if data[] can be cloned by reference
cloneByRef
)

Fills in the data object by a portion of the byte[].

public void
set(byte[]
actual data bytes
data
,
int
data[0] to data[len-1] are treated as the data.
len
,
String
MIME type
mimeType
)

Fills in the data object by a portion of the byte[].

public void
set(byte[]
this buffer may be owned directly by the unmarshaleld JAXB object.
data
,
String
MIME type
mimeType
)

Fills in the data object by the byte[] of the exact length.

public void
set(DataHandler
DataHandler for the data
data
)

Fills in the data object by a DataHandler.

public void
setHrefCid(final String cid)

public CharSequence
subSequence(int start, int end)

Implements java.lang.CharSequence.subSequence.

Internally this is only used to split a text to a list, which doesn't happen that much for base64.

public String
toString()

Overrides java.lang.Object.toString.

Implements java.lang.CharSequence.toString.

Returns the base64 encoded string of this data.

public void
writeTo(char[] buf, int start)

public void
Inherited from java.lang.Object:
equalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

CHUNK_SIZEback to summary
private static final int CHUNK_SIZE
databack to summary
private byte[] data
dataCloneByRefback to summary
private boolean dataCloneByRef

True if data can be cloned by reference if Base64Data instance is cloned.

dataHandlerback to summary
private DataHandler dataHandler
dataLenback to summary
private int dataLen

Length of the valid data in data.

hrefCidback to summary
private String hrefCid
loggerback to summary
private static final Logger logger
mimeTypeback to summary
private String mimeType

Optional MIME type of data. Unused when dataHandler is set. Use DataHandler#getContentType() in that case.

Constructor Detail

Base64Databack to summary
public Base64Data()

Default constructor

Base64Databack to summary
public Base64Data(Base64Data that)

Clone constructor

Parameters
that:Base64Data

needs to be cloned

Method Detail

charAtback to summary
public char charAt(int index)

Implements java.lang.CharSequence.charAt.

Encode this binary data in the base64 encoding and returns the character at the specified position.

Annotations
@Override
cloneback to summary
public Base64Data clone()

Overrides java.lang.Object.clone.

Doc from java.lang.Object.clone.

Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:

x.clone() != x
will be true, and that the expression:
x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that:
x.clone().equals(x)
will be 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.

Annotations
@Override
getback to summary
public byte[] get()

Gets the raw data. The size of the byte array maybe larger than the actual length.

Returns:byte[]

data as byte[], the array may be larger

getDataHandlerback to summary
public DataHandler getDataHandler()

Gets the raw data. If the returned DataHandler is StreamingDataHandler, callees may need to downcast to take advantage of its capabilities.

Returns:DataHandler

DataHandler for the data

See Also
StreamingDataHandler
getDataLenback to summary
public int getDataLen()

Gets the length of the binary data counted in bytes. Note that if this object encapsulates DataHandler, this method would have to read the whole thing into byte[] just to count the length, because DataHandler doesn't easily expose the length.

Returns:int

no of bytes

getExactback to summary
public byte[] getExact()

Gets the byte[] of the exact length.

Returns:byte[]

byte[] for data

getHrefCidback to summary
public String getHrefCid()
getInputStreamback to summary
public InputStream getInputStream() throws IOException

Gets the data as an InputStream.

Returns:InputStream

data as InputStream

Exceptions
IOException:
if i/o error occurs
getMimeTypeback to summary
public String getMimeType()
getPropertyback to summary
pack-priv static String getProperty(final String propName)
hasDataback to summary
public boolean hasData()

Returns false if this object only has DataHandler and therefore get() operation is likely going to be expensive.

Returns:boolean

false if it has only DataHandler

lengthback to summary
public int length()

Implements java.lang.CharSequence.length.

Gets the number of characters needed to represent this binary data in the base64 encoding.

Annotations
@Override
setback to summary
public void set(byte[] data, int len, String mimeType, boolean cloneByRef)

Fills in the data object by a portion of the byte[].

Parameters
data:byte[]

actual data

len:int

data[0] to data[len-1] are treated as the data.

mimeType:String

MIME type

cloneByRef:boolean

true if data[] can be cloned by reference

setback to summary
public void set(byte[] data, int len, String mimeType)

Fills in the data object by a portion of the byte[].

Parameters
data:byte[]

actual data bytes

len:int

data[0] to data[len-1] are treated as the data.

mimeType:String

MIME type

setback to summary
public void set(byte[] data, String mimeType)

Fills in the data object by the byte[] of the exact length.

Parameters
data:byte[]

this buffer may be owned directly by the unmarshaleld JAXB object.

mimeType:String

MIME type

setback to summary
public void set(DataHandler data)

Fills in the data object by a DataHandler.

Parameters
data:DataHandler

DataHandler for the data

setHrefCidback to summary
public void setHrefCid(final String cid)
subSequenceback to summary
public CharSequence subSequence(int start, int end)

Implements java.lang.CharSequence.subSequence.

Internally this is only used to split a text to a list, which doesn't happen that much for base64. So this method should be smaller than faster.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Implements java.lang.CharSequence.toString.

Returns the base64 encoded string of this data.

Annotations
@Override
writeToback to summary
public void writeTo(char[] buf, int start)
writeToback to summary
public void writeTo(XMLStreamWriter output) throws IOException, XMLStreamException
org.jvnet.staxex back to summary

private final Class Base64Data.Base64DataSource

extends Object
implements DataSource
Class Inheritance
All Implemented Interfaces
jakarta.activation.DataSource

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public String
getContentType()

Implements jakarta.activation.DataSource.getContentType.

This method returns the MIME type of the data in the form of a string.

public InputStream
getInputStream()

Implements jakarta.activation.DataSource.getInputStream.

This method returns an InputStream representing the data and throws the appropriate exception if it can not do so.

public String
getName()

Implements jakarta.activation.DataSource.getName.

Return the name of this object where the name of the object is dependant on the nature of the underlying objects.

public OutputStream
getOutputStream()

Implements jakarta.activation.DataSource.getOutputStream.

This method returns an OutputStream where the data can be written and throws the appropriate exception if it can not do so.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Base64DataSourceback to summary
private Base64DataSource()

Method Detail

getContentTypeback to summary
public String getContentType()

Implements jakarta.activation.DataSource.getContentType.

Doc from jakarta.activation.DataSource.getContentType.

This method returns the MIME type of the data in the form of a string. It should always return a valid type. It is suggested that getContentType return "application/octet-stream" if the DataSource implementation can not determine the data type.

getInputStreamback to summary
public InputStream getInputStream()

Implements jakarta.activation.DataSource.getInputStream.

Doc from jakarta.activation.DataSource.getInputStream.

This method returns an InputStream representing the data and throws the appropriate exception if it can not do so. Note that a new InputStream object must be returned each time this method is called, and the stream must be positioned at the beginning of the data.

getNameback to summary
public String getName()

Implements jakarta.activation.DataSource.getName.

Doc from jakarta.activation.DataSource.getName.

Return the name of this object where the name of the object is dependant on the nature of the underlying objects. DataSources encapsulating files may choose to return the filename of the object. (Typically this would be the last component of the filename, not an entire pathname.)

getOutputStreamback to summary
public OutputStream getOutputStream()

Implements jakarta.activation.DataSource.getOutputStream.

Doc from jakarta.activation.DataSource.getOutputStream.

This method returns an OutputStream where the data can be written and throws the appropriate exception if it can not do so. Note that a new OutputStream object must be returned each time this method is called, and the stream must be positioned at the location the data is to be written.

org.jvnet.staxex back to summary

private final Class Base64Data.Base64StreamingDataHandler

extends StreamingDataHandler
Class Inheritance

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements abstract org.jvnet.staxex.StreamingDataHandler.close.

Implements java.io.Closeable.close.

Releases any resources associated with this DataHandler.

public void
moveTo(File dst)

Implements abstract org.jvnet.staxex.StreamingDataHandler.moveTo.

Obtains the BLOB into a specified file.

public InputStream
readOnce()

Implements abstract org.jvnet.staxex.StreamingDataHandler.readOnce.

Works like getInputStream() except that this method can be invoked only once.

Inherited from org.jvnet.staxex.StreamingDataHandler:
getHrefCidsetHrefCid

Constructor Detail

Base64StreamingDataHandlerback to summary
pack-priv Base64StreamingDataHandler(DataSource source)

Method Detail

closeback to summary
public void close() throws IOException

Implements abstract org.jvnet.staxex.StreamingDataHandler.close.

Implements java.io.Closeable.close.

Doc from org.jvnet.staxex.StreamingDataHandler.close.

Releases any resources associated with this DataHandler. (such as an attachment in a web service read from a temp file will be deleted.) After calling this method, it is illegal to call any other methods.

Annotations
@Override
Exceptions
IOException:
for errors
moveToback to summary
public void moveTo(File dst) throws IOException

Implements abstract org.jvnet.staxex.StreamingDataHandler.moveTo.

Doc from org.jvnet.staxex.StreamingDataHandler.moveTo.

Obtains the BLOB into a specified file.

Semantically, this method is roughly equivalent to the following code, except that the actual implementation is likely to be a lot faster.

InputStream i = getInputStream();
OutputStream o = new FileOutputStream(dst);
int ch;
while((ch=i.read())!=-1)  o.write(ch);
i.close();
o.close();

The main motivation behind this method is that often DataHandler that reads data from a streaming source will use a temporary file as a data store to hold data (think of commons-fileupload.) In such case this method can be as fast as calling File#renameTo(File).

This method shouldn't be called when there are any open streams.

After this method is invoked, readOnce() and getInputStream() will simply open the destination file you've specified as an argument. So if you further move the file or delete this file, those methods will behave in undefined fashion. For a simliar reason, calling this method multiple times will cause undefined behavior.

Annotations
@Override
Exceptions
IOException:
for errors
readOnceback to summary
public InputStream readOnce() throws IOException

Implements abstract org.jvnet.staxex.StreamingDataHandler.readOnce.

Doc from org.jvnet.staxex.StreamingDataHandler.readOnce.

Works like getInputStream() except that this method can be invoked only once.

This is used as a signal from the caller that there will be no further getInputStream() invocation nor readOnce() invocation on this object (which would result in IOException.)

When DataHandler is backed by a streaming BLOB (such as an attachment in a web service read from the network), this allows the callee to avoid unnecessary buffering.

Note that it is legal to call getInputStream() multiple times and then call readOnce() afterward. Streams created such a way can be read in any order — there's no requirement that streams created earlier must be read first.

Annotations
@Override
Exceptions
IOException:
if any i/o error
org.jvnet.staxex back to summary

private final Class Base64Data.FilterDataHandler

extends StreamingDataHandler
Class Inheritance

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements abstract org.jvnet.staxex.StreamingDataHandler.close.

Implements java.io.Closeable.close.

Releases any resources associated with this DataHandler.

public void
moveTo(File dst)

Implements abstract org.jvnet.staxex.StreamingDataHandler.moveTo.

Obtains the BLOB into a specified file.

public InputStream
readOnce()

Implements abstract org.jvnet.staxex.StreamingDataHandler.readOnce.

Works like getInputStream() except that this method can be invoked only once.

Inherited from org.jvnet.staxex.StreamingDataHandler:
getHrefCidsetHrefCid

Constructor Detail

FilterDataHandlerback to summary
pack-priv FilterDataHandler(DataHandler dh)

Method Detail

closeback to summary
public void close() throws IOException

Implements abstract org.jvnet.staxex.StreamingDataHandler.close.

Implements java.io.Closeable.close.

Doc from org.jvnet.staxex.StreamingDataHandler.close.

Releases any resources associated with this DataHandler. (such as an attachment in a web service read from a temp file will be deleted.) After calling this method, it is illegal to call any other methods.

Annotations
@Override
Exceptions
IOException:
for errors
moveToback to summary
public void moveTo(File dst) throws IOException

Implements abstract org.jvnet.staxex.StreamingDataHandler.moveTo.

Doc from org.jvnet.staxex.StreamingDataHandler.moveTo.

Obtains the BLOB into a specified file.

Semantically, this method is roughly equivalent to the following code, except that the actual implementation is likely to be a lot faster.

InputStream i = getInputStream();
OutputStream o = new FileOutputStream(dst);
int ch;
while((ch=i.read())!=-1)  o.write(ch);
i.close();
o.close();

The main motivation behind this method is that often DataHandler that reads data from a streaming source will use a temporary file as a data store to hold data (think of commons-fileupload.) In such case this method can be as fast as calling File#renameTo(File).

This method shouldn't be called when there are any open streams.

After this method is invoked, readOnce() and getInputStream() will simply open the destination file you've specified as an argument. So if you further move the file or delete this file, those methods will behave in undefined fashion. For a simliar reason, calling this method multiple times will cause undefined behavior.

Annotations
@Override
Exceptions
IOException:
for errors
readOnceback to summary
public InputStream readOnce() throws IOException

Implements abstract org.jvnet.staxex.StreamingDataHandler.readOnce.

Doc from org.jvnet.staxex.StreamingDataHandler.readOnce.

Works like getInputStream() except that this method can be invoked only once.

This is used as a signal from the caller that there will be no further getInputStream() invocation nor readOnce() invocation on this object (which would result in IOException.)

When DataHandler is backed by a streaming BLOB (such as an attachment in a web service read from the network), this allows the callee to avoid unnecessary buffering.

Note that it is legal to call getInputStream() multiple times and then call readOnce() afterward. Streams created such a way can be read in any order — there's no requirement that streams created earlier must be read first.

Annotations
@Override
Exceptions
IOException:
if any i/o error