Top Description Fields Constructors Methods
org.jvnet.staxex

public abstract Class StreamingDataHandler

extends DataHandler
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Known Direct Subclasses
org.jvnet.staxex.Base64Data.Base64StreamingDataHandler, org.jvnet.staxex.Base64Data.FilterDataHandler
Imports
jakarta.activation.DataHandler, .DataSource, java.io.BufferedInputStream, .Closeable, .File, .IOException, .InputStream, java.net.URL

DataHandler extended to offer better buffer management in a streaming environment.

DataHandler is used commonly as a data format across multiple systems (such as JAXB/WS.) Unfortunately, DataHandler has the semantics of "read as many times as you want", so this makes it difficult for involving parties to handle a BLOB in a streaming fashion.

StreamingDataHandler solves this problem by offering methods that enable faster bulk "consume once" read operation.

Author
Jitendra Kotamraju

Field Summary

Modifier and TypeField and Description
private String

Constructor Summary

AccessConstructor and Description
public
public
public

Method Summary

Modifier and TypeMethod and Description
public abstract void
close()

Redeclares java.io.Closeable.close.

Releases any resources associated with this DataHandler.

public String
public abstract void
moveTo(File dst)

Obtains the BLOB into a specified file.

public abstract InputStream

Returns:

always non-null. Represents the content of this BLOB. The returned stream is generally not buffered, so for better performance read in a big batch or wrap this into BufferedInputStream.
readOnce
()

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

public void
setHrefCid(final String cid)

Inherited from jakarta.activation.DataHandler:
getAllCommandsgetBeangetCommandgetContentgetContentTypegetDataSourcegetInputStreamgetNamegetOutputStreamgetPreferredCommandsgetTransferDatagetTransferDataFlavorsisDataFlavorSupportedsetCommandMapsetDataContentHandlerFactorywriteTo

Field Detail

hrefCidback to summary
private String hrefCid

Constructor Detail

StreamingDataHandlerback to summary
public StreamingDataHandler(Object o, String s)
StreamingDataHandlerback to summary
public StreamingDataHandler(URL url)
StreamingDataHandlerback to summary
public StreamingDataHandler(DataSource dataSource)

Method Detail

closeback to summary
public abstract void close() throws IOException

Redeclares java.io.Closeable.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
getHrefCidback to summary
public String getHrefCid()
moveToback to summary
public abstract void moveTo(File dst) throws IOException

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.

Exceptions
IOException:
for errors
readOnceback to summary
public abstract InputStream readOnce() throws IOException

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.

Returns:InputStream

always non-null. Represents the content of this BLOB. The returned stream is generally not buffered, so for better performance read in a big batch or wrap this into BufferedInputStream.

Exceptions
IOException:
if any i/o error
setHrefCidback to summary
public void setHrefCid(final String cid)