com.miginfocom.util.io
Class IOUtil

java.lang.Object
  extended by com.miginfocom.util.io.IOUtil

public class IOUtil
extends java.lang.Object

Utility methods for I/O.
Manages delegates for objects that should be able to be serialized to XML with XMLEncoder.

See Also:
setDelegate(java.lang.Class, java.beans.PersistenceDelegate), getDelegate(Class)

Constructor Summary
IOUtil()
           
 
Method Summary
static void addDelegates(java.beans.XMLEncoder enc)
          Adds the custom delegates used for persisting (saving) a number of classes to XML including a lot of JDK classes that don't have default delegates.
static void addPersistanceDelegates(java.beans.XMLEncoder enc)
          Deprecated. Spelling error. Use addDelegates
static java.io.BufferedInputStream bufferInputStream(java.io.InputStream in, int bufSize)
          Buffers the stream if not already buffered.
static java.io.BufferedOutputStream bufferOutputStream(java.io.OutputStream in, int bufSize)
          Buffers the stream if not already buffered.
static java.beans.PersistenceDelegate getDelegate(java.lang.Class c)
          Returns the persistence delagates currently used.
static java.util.HashMap getDelegates()
          Returns a copy of the current delegate map.
static java.lang.Object getSerializedObject(java.lang.Object caller)
          Returns the serialized object that are associated with caller.
static java.util.zip.ZipInputStream getZipEntryStream(java.io.InputStream in, java.lang.String entryName)
          Wraps in in a ZipInputStream and forwards to the entry entryName.
static boolean isProbableBase64(java.lang.String s)
          Returns if the string is a base64 encoded binary string.
static boolean isProbableURL(java.lang.String s)
          Returns if the string, trimmed, start with "sftp:", "ftp:", "http:", "https:" or "file:" ignoring case.
static boolean isZippedStream(java.io.InputStream in, boolean defaultAns)
          Returns if the InputStream points to a ziped stream.
static java.lang.Object readAsXML(java.io.ObjectInput in)
          Reads an object from in using the
static byte[] readByteArray(java.io.File file)
          Reads the file and returns it as a byte array.
static void registerDelegatesGlobally()
          Registers the delegates in the global Introspector.
static void setDelegate(java.lang.Class c, java.beans.PersistenceDelegate del)
          Sets/add the persistence delagates to be used for a class.
static void setSerializedObject(java.lang.Object caller, java.lang.Object o)
          Sets the serialized object and associates it with caller.
static byte[] toByteArray(java.io.InputStream is)
          Reads the input stream into a byte array.
static void writeAsXML(java.io.ObjectOutput out, java.lang.Object o)
          Writes an object to XML.
static void writeXMLObject(java.io.OutputStream os, java.lang.Object o)
          Writes the objet and CLOSES the stream.
static void writeXMLObject(java.io.OutputStream os, java.lang.Object o, java.beans.ExceptionListener listener)
          Writes the objet and CLOSES the stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IOUtil

public IOUtil()
Method Detail

setSerializedObject

public static void setSerializedObject(java.lang.Object caller,
                                       java.lang.Object o)
Sets the serialized object and associates it with caller.

Parameters:
caller - The objec created o
o - The just serialized object.

getSerializedObject

public static java.lang.Object getSerializedObject(java.lang.Object caller)
Returns the serialized object that are associated with caller. It also removes it from the list.

Parameters:
caller - The original creator of the object.
Returns:
The object.

isProbableBase64

public static boolean isProbableBase64(java.lang.String s)
Returns if the string is a base64 encoded binary string. Note! This is only as a comparison to beeing a URL or filename, it's not a generic test.

Parameters:
s - The string to test.
Returns:
If the string is a base64 encoded binary string as opposed to a filename or an URL.

isProbableURL

public static boolean isProbableURL(java.lang.String s)
Returns if the string, trimmed, start with "sftp:", "ftp:", "http:", "https:" or "file:" ignoring case.

Parameters:
s - A string to check, not null
Returns:
true if probable url

isZippedStream

public static boolean isZippedStream(java.io.InputStream in,
                                     boolean defaultAns)
                              throws java.io.IOException
Returns if the InputStream points to a ziped stream. (not the type)

Parameters:
in - The stream to peek at. Must support marking. Will not be changed
defaultAns - Will be returned if in doesn't support mark/reset.
Returns:
true if the stream is a ZIPPED-stream
Throws:
java.io.IOException
See Also:
InputStream.markSupported()

bufferInputStream

public static java.io.BufferedInputStream bufferInputStream(java.io.InputStream in,
                                                            int bufSize)
Buffers the stream if not already buffered.

Parameters:
in - The stream that may or may not be buffered.
Returns:
The stream as a buffered stream.

bufferOutputStream

public static java.io.BufferedOutputStream bufferOutputStream(java.io.OutputStream in,
                                                              int bufSize)
Buffers the stream if not already buffered.

Parameters:
in - The stream that may or may not be buffered.
Returns:
The stream as a buffered stream.

getZipEntryStream

public static java.util.zip.ZipInputStream getZipEntryStream(java.io.InputStream in,
                                                             java.lang.String entryName)
                                                      throws java.io.IOException
Wraps in in a ZipInputStream and forwards to the entry entryName.

Parameters:
in - The zipped input stream.
entryName - The name of the entry to forward to.
Returns:
The stream at the correct position or null if not found.
Throws:
java.io.IOException

toByteArray

public static byte[] toByteArray(java.io.InputStream is)
                          throws java.io.IOException
Reads the input stream into a byte array. If not buffered already a BufferedInputStream is wrapped around it.

Parameters:
is - The stream. Will be NOT closed after reading is done.
Returns:
A byte array. Never null
Throws:
java.io.IOException

readByteArray

public static byte[] readByteArray(java.io.File file)
                            throws java.io.IOException
Reads the file and returns it as a byte array.

Parameters:
file - The file. Not null.
Returns:
The byte array. Never null.
Throws:
java.io.IOException

writeXMLObject

public static void writeXMLObject(java.io.OutputStream os,
                                  java.lang.Object o)
Writes the objet and CLOSES the stream. Uses the persistence delegate registered in this class.

Parameters:
os - The stream to write to. Will be closed.
o - The object to be serialized.

writeXMLObject

public static void writeXMLObject(java.io.OutputStream os,
                                  java.lang.Object o,
                                  java.beans.ExceptionListener listener)
Writes the objet and CLOSES the stream. Uses the persistence delegate registered in this class.

Parameters:
os - The stream to write to. Will be closed.
o - The object to be serialized.
listener - The listener to recieve the exeptions if there are any. If null not used.

writeAsXML

public static void writeAsXML(java.io.ObjectOutput out,
                              java.lang.Object o)
                       throws java.io.IOException
Writes an object to XML.

Parameters:
out - The boject out to write to. Will not be closed.
o - The object to write.
Throws:
java.io.IOException

readAsXML

public static java.lang.Object readAsXML(java.io.ObjectInput in)
                                  throws java.io.IOException
Reads an object from in using the

Parameters:
in - The object input to read from.
Returns:
The object. Never null.
Throws:
java.io.IOException

addPersistanceDelegates

public static void addPersistanceDelegates(java.beans.XMLEncoder enc)
Deprecated. Spelling error. Use addDelegates

Deprecated. Spelling error.


addDelegates

public static void addDelegates(java.beans.XMLEncoder enc)
Adds the custom delegates used for persisting (saving) a number of classes to XML including a lot of JDK classes that don't have default delegates. The most common object types already have a delegate (see source code for MetaData), including all JavaBeans. This method is only called when saving the com.miginfocom.theme.
For a great primer on how to write Persistence delegates (normally just a couple of lines) see http://java.sun.com/products/jfc/tsc/articles/persistence4

Parameters:
enc - The decoder to add the delegates to. Not null.
See Also:
Encoder.setPersistenceDelegate(java.lang.Class, java.beans.PersistenceDelegate)

getDelegates

public static java.util.HashMap getDelegates()
Returns a copy of the current delegate map. The Class is the key and the PersistenceDelegate is the value.

Returns:
A copy of the current delegate map.
See Also:
IdentityHashMap.entrySet()

registerDelegatesGlobally

public static void registerDelegatesGlobally()
Registers the delegates in the global Introspector. Only does this once no matter how many times it's called.


getDelegate

public static java.beans.PersistenceDelegate getDelegate(java.lang.Class c)
Returns the persistence delagates currently used.

Parameters:
c - The class to return the registered deligate for.
Returns:
The deligate of null if none.

setDelegate

public static void setDelegate(java.lang.Class c,
                               java.beans.PersistenceDelegate del)
Sets/add the persistence delagates to be used for a class.

Parameters:
c - The class to set the registered deligate for.
del - The new deligate or null to erase to old one.


Copyright © 2009 MiG InfoCom AB. All Rights Reserved.