APIdia Launch Partner
Top Description Fields Constructors Methods
java.beans

public Class XMLDecoder

extends Object
implements AutoCloseable
Class Inheritance
All Implemented Interfaces
java.lang.AutoCloseable
Imports
com.sun.beans.decoder.DocumentHandler, java.io.Closeable, .InputStream, .IOException, org.xml.sax.InputSource, org.xml.sax.helpers.DefaultHandler

The XMLDecoder class is used to read XML documents created using the XMLEncoder and is used just like the ObjectInputStream. For example, one can use the following fragment to read the first object defined in an XML document written by the XMLEncoder class:
       XMLDecoder d = new XMLDecoder(
                          new BufferedInputStream(
                              new FileInputStream("Test.xml")));
       Object result = d.readObject();
       d.close();
 

For more information you might also want to check out Long Term Persistence of JavaBeans Components: XML Schema, an article in The Swing Connection.

Author
Philip Milne
Since
1.4
See Also
XMLEncoder, java.io.ObjectInputStream

Field Summary

Modifier and TypeField and Description
private Object[]
private final DocumentHandler
private int
private final InputSource
private Object

Constructor Summary

AccessConstructor and Description
public
XMLDecoder(InputStream
The underlying stream.
in
)

Creates a new input stream for reading archives created by the XMLEncoder class.

public
XMLDecoder(InputStream
The underlying stream.
in
,
Object
The owner of this stream.
owner
)

Creates a new input stream for reading archives created by the XMLEncoder class.

public
XMLDecoder(InputStream
the underlying stream.
in
,
Object
the owner of this stream.
owner
,
ExceptionListener
the exception handler for the stream; if null the default exception listener will be used.
exceptionListener
)

Creates a new input stream for reading archives created by the XMLEncoder class.

public
XMLDecoder(InputStream
the underlying stream. null may be passed without error, though the resulting XMLDecoder will be useless
in
,
Object
the owner of this stream. null is a legal value
owner
,
ExceptionListener
the exception handler for the stream, or null to use the default
exceptionListener
,
ClassLoader
the class loader used for instantiating objects. null indicates that the default class loader should be used
cl
)

Creates a new input stream for reading archives created by the XMLEncoder class.

public
XMLDecoder(InputSource
the input source to parse
is
)

Creates a new decoder to parse XML archives created by the XMLEncoder class.

private
XMLDecoder(InputSource
the input source to parse
is
,
Object
the owner of this decoder
owner
,
ExceptionListener
the exception handler for the parser, or null to use the default exception handler
el
,
ClassLoader
the class loader used for instantiating objects, or null to use the default class loader
cl
)

Creates a new decoder to parse XML archives created by the XMLEncoder class.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.lang.AutoCloseable.close.

This method closes the input stream associated with this stream.

private void
public static DefaultHandler

Returns:

an instance of DefaultHandler for SAX parser
createHandler
(Object
the owner of the default handler that can be used as a value of <java> element
owner
,
ExceptionListener
the exception handler for the parser, or null to use the default exception handler
el
,
ClassLoader
the class loader used for instantiating objects, or null to use the default class loader
cl
)

Creates a new handler for SAX parser that can be used to parse embedded XML archives created by the XMLEncoder class.

public ExceptionListener

Returns:

The exception handler for this stream. Will return the default exception listener if this has not explicitly been set.
getExceptionListener
()

Gets the exception handler for this stream.

public Object

Returns:

The owner of this decoder.
getOwner
()

Gets the owner of this decoder.

private boolean
public Object

Returns:

the next object read
readObject
()

Reads the next object from the underlying input stream.

public void
setExceptionListener(ExceptionListener
The exception handler for this stream; if null the default exception listener will be used.
exceptionListener
)

Sets the exception handler for this stream to exceptionListener.

public void
setOwner(Object
The owner of this decoder.
owner
)

Sets the owner of this decoder to owner.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

arrayback to summary
private Object[] array
handlerback to summary
private final DocumentHandler handler
indexback to summary
private int index
inputback to summary
private final InputSource input
ownerback to summary
private Object owner

Constructor Detail

XMLDecoderback to summary
public XMLDecoder(InputStream in)

Creates a new input stream for reading archives created by the XMLEncoder class.

Parameters
in:InputStream

The underlying stream.

See Also
XMLEncoder#XMLEncoder(java.io.OutputStream)
XMLDecoderback to summary
public XMLDecoder(InputStream in, Object owner)

Creates a new input stream for reading archives created by the XMLEncoder class.

Parameters
in:InputStream

The underlying stream.

owner:Object

The owner of this stream.

XMLDecoderback to summary
public XMLDecoder(InputStream in, Object owner, ExceptionListener exceptionListener)

Creates a new input stream for reading archives created by the XMLEncoder class.

Parameters
in:InputStream

the underlying stream.

owner:Object

the owner of this stream.

exceptionListener:ExceptionListener

the exception handler for the stream; if null the default exception listener will be used.

XMLDecoderback to summary
public XMLDecoder(InputStream in, Object owner, ExceptionListener exceptionListener, ClassLoader cl)

Creates a new input stream for reading archives created by the XMLEncoder class.

Parameters
in:InputStream

the underlying stream. null may be passed without error, though the resulting XMLDecoder will be useless

owner:Object

the owner of this stream. null is a legal value

exceptionListener:ExceptionListener

the exception handler for the stream, or null to use the default

cl:ClassLoader

the class loader used for instantiating objects. null indicates that the default class loader should be used

Since
1.5
XMLDecoderback to summary
public XMLDecoder(InputSource is)

Creates a new decoder to parse XML archives created by the XMLEncoder class. If the input source is is null, no exception is thrown and no parsing is performed. This behavior is similar to behavior of other constructors that use InputStream as a parameter.

Parameters
is:InputSource

the input source to parse

Since
1.7
XMLDecoderback to summary
private XMLDecoder(InputSource is, Object owner, ExceptionListener el, ClassLoader cl)

Creates a new decoder to parse XML archives created by the XMLEncoder class.

Parameters
is:InputSource

the input source to parse

owner:Object

the owner of this decoder

el:ExceptionListener

the exception handler for the parser, or null to use the default exception handler

cl:ClassLoader

the class loader used for instantiating objects, or null to use the default class loader

Since
1.7

Method Detail

closeback to summary
public void close()

Implements java.lang.AutoCloseable.close.

This method closes the input stream associated with this stream.

Annotations
@Override
closeback to summary
private void close(Closeable in)
createHandlerback to summary
public static DefaultHandler createHandler(Object owner, ExceptionListener el, ClassLoader cl)

Creates a new handler for SAX parser that can be used to parse embedded XML archives created by the XMLEncoder class. The owner should be used if parsed XML document contains the method call within context of the <java> element. The null value may cause illegal parsing in such case. The same problem may occur, if the owner class does not contain expected method to call. See details here.

Parameters
owner:Object

the owner of the default handler that can be used as a value of <java> element

el:ExceptionListener

the exception handler for the parser, or null to use the default exception handler

cl:ClassLoader

the class loader used for instantiating objects, or null to use the default class loader

Returns:DefaultHandler

an instance of DefaultHandler for SAX parser

Since
1.7
getExceptionListenerback to summary
public ExceptionListener getExceptionListener()

Gets the exception handler for this stream.

Returns:ExceptionListener

The exception handler for this stream. Will return the default exception listener if this has not explicitly been set.

See Also
setExceptionListener
getOwnerback to summary
public Object getOwner()

Gets the owner of this decoder.

Returns:Object

The owner of this decoder.

See Also
setOwner
parsingCompleteback to summary
private boolean parsingComplete()
readObjectback to summary
public Object readObject()

Reads the next object from the underlying input stream.

Returns:Object

the next object read

Exceptions
ArrayIndexOutOfBoundsException:
if the stream contains no objects (or no more objects)
See Also
XMLEncoder#writeObject
setExceptionListenerback to summary
public void setExceptionListener(ExceptionListener exceptionListener)

Sets the exception handler for this stream to exceptionListener. The exception handler is notified when this stream catches recoverable exceptions.

Parameters
exceptionListener:ExceptionListener

The exception handler for this stream; if null the default exception listener will be used.

See Also
getExceptionListener
setOwnerback to summary
public void setOwner(Object owner)

Sets the owner of this decoder to owner.

Parameters
owner:Object

The owner of this decoder.

See Also
getOwner