Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated to run under Java 1.4

  • Loading branch information...
commit 175cf9f6fa7af53f3cd9ed937fb3218231955600 1 parent 2133ae2
@nzakas authored
View
2  ant.properties
@@ -3,7 +3,7 @@ lib.dir = lib
doc.dir = doc
build.dir = build
product.name = datauri
-version.number = 0.1.1
+version.number = 0.2.0
jar.name = ${product.name}-${version.number}.jar
core.jar.name = ${product.name}-core-${version.number}.jar
dist.package.name = ${product.name}-${version.number}
View
2  build.xml
@@ -20,7 +20,7 @@
includes="**/*.java"
deprecation="off"
debug="on"
- source="1.5">
+ source="1.4">
<classpath>
<pathelement location="${lib.dir}/jargs-1.0.jar"/>
</classpath>
View
352 src/net/nczonline/web/datauri/Base64.java
@@ -891,7 +891,7 @@ public static String encodeBytes( byte[] source, int off, int len, int options )
if( off + len > source.length ){
throw new IllegalArgumentException(
- String.format( "Cannot have offset of %d and length of %d with array of length %d", off,len,source.length));
+ "Cannot have offset of " + off + " and length of " + len + " with array of length " + source.length);
} // end if: off < 0
@@ -1027,12 +1027,12 @@ private static int decode4to3(
throw new NullPointerException( "Destination array was null." );
} // end if
if( srcOffset < 0 || srcOffset + 3 >= source.length ){
- throw new IllegalArgumentException( String.format(
- "Source array with length %d cannot have offset of %d and still process four bytes.", source.length, srcOffset ) );
+ throw new IllegalArgumentException(
+ "Source array with length " + source.length + " cannot have offset of " + srcOffset + " and still process four bytes.");
} // end if
if( destOffset < 0 || destOffset +2 >= destination.length ){
- throw new IllegalArgumentException( String.format(
- "Destination array with length %d cannot have offset of %d and still store three bytes.", destination.length, destOffset ) );
+ throw new IllegalArgumentException(
+ "Destination array with length " + destination.length + " cannot have offset of " );
} // end if
@@ -1140,8 +1140,8 @@ else if( source[ srcOffset + 3 ] == EQUALS_SIGN ) {
throw new NullPointerException( "Cannot decode null source array." );
} // end if
if( off < 0 || off + len > source.length ){
- throw new IllegalArgumentException( String.format(
- "Source array with length %d cannot have offset of %d and process %d bytes.", source.length, off, len ) );
+ throw new IllegalArgumentException(
+ "Source array with length " + source.length + " cannot have offset of " + off + " and process " + len + " bytes." );
} // end if
if( len == 0 ){
@@ -1187,8 +1187,8 @@ else if( source[ srcOffset + 3 ] == EQUALS_SIGN ) {
} // end if: white space, equals sign or better
else {
// There's a bad input character in the Base64 stream.
- throw new java.io.IOException( String.format(
- "Bad Base64 input character '%c' in array position %d", source[i], i ) );
+ throw new java.io.IOException(
+ "Bad Base64 input character '" + source[i] + "' in array position " + i);
} // end else:
} // each input character
@@ -1285,335 +1285,6 @@ else if( source[ srcOffset + 3 ] == EQUALS_SIGN ) {
return bytes;
} // end decode
-
-
-
- /**
- * Attempts to decode Base64 data and deserialize a Java
- * Object within. Returns <tt>null</tt> if there was an error.
- *
- * @param encodedObject The Base64 data to decode
- * @return The decoded and deserialized object
- * @throws NullPointerException if encodedObject is null
- * @throws java.io.IOException if there is a general error
- * @throws ClassNotFoundException if the decoded object is of a
- * class that cannot be found by the JVM
- * @since 1.5
- */
- public static Object decodeToObject( String encodedObject )
- throws java.io.IOException, java.lang.ClassNotFoundException {
- return decodeToObject(encodedObject,NO_OPTIONS,null);
- }
-
-
- /**
- * Attempts to decode Base64 data and deserialize a Java
- * Object within. Returns <tt>null</tt> if there was an error.
- * If <tt>loader</tt> is not null, it will be the class loader
- * used when deserializing.
- *
- * @param encodedObject The Base64 data to decode
- * @param options Various parameters related to decoding
- * @param loader Optional class loader to use in deserializing classes.
- * @return The decoded and deserialized object
- * @throws NullPointerException if encodedObject is null
- * @throws java.io.IOException if there is a general error
- * @throws ClassNotFoundException if the decoded object is of a
- * class that cannot be found by the JVM
- * @since 2.3.4
- */
- public static Object decodeToObject(
- String encodedObject, int options, final ClassLoader loader )
- throws java.io.IOException, java.lang.ClassNotFoundException {
-
- // Decode and gunzip if necessary
- byte[] objBytes = decode( encodedObject, options );
-
- java.io.ByteArrayInputStream bais = null;
- java.io.ObjectInputStream ois = null;
- Object obj = null;
-
- try {
- bais = new java.io.ByteArrayInputStream( objBytes );
-
- // If no custom class loader is provided, use Java's builtin OIS.
- if( loader == null ){
- ois = new java.io.ObjectInputStream( bais );
- } // end if: no loader provided
-
- // Else make a customized object input stream that uses
- // the provided class loader.
- else {
- ois = new java.io.ObjectInputStream(bais){
- @Override
- public Class<?> resolveClass(java.io.ObjectStreamClass streamClass)
- throws java.io.IOException, ClassNotFoundException {
- Class c = Class.forName(streamClass.getName(), false, loader);
- if( c == null ){
- return super.resolveClass(streamClass);
- } else {
- return c; // Class loader knows of this class.
- } // end else: not null
- } // end resolveClass
- }; // end ois
- } // end else: no custom class loader
-
- obj = ois.readObject();
- } // end try
- catch( java.io.IOException e ) {
- throw e; // Catch and throw in order to execute finally{}
- } // end catch
- catch( java.lang.ClassNotFoundException e ) {
- throw e; // Catch and throw in order to execute finally{}
- } // end catch
- finally {
- try{ bais.close(); } catch( Exception e ){}
- try{ ois.close(); } catch( Exception e ){}
- } // end finally
-
- return obj;
- } // end decodeObject
-
-
-
- /**
- * Convenience method for encoding data to a file.
- *
- * <p>As of v 2.3, if there is a error,
- * the method will throw an java.io.IOException. <b>This is new to v2.3!</b>
- * In earlier versions, it just returned false, but
- * in retrospect that's a pretty poor way to handle it.</p>
- *
- * @param dataToEncode byte array of data to encode in base64 form
- * @param filename Filename for saving encoded data
- * @throws java.io.IOException if there is an error
- * @throws NullPointerException if dataToEncode is null
- * @since 2.1
- */
- public static void encodeToFile( byte[] dataToEncode, String filename )
- throws java.io.IOException {
-
- if( dataToEncode == null ){
- throw new NullPointerException( "Data to encode was null." );
- } // end iff
-
- Base64.OutputStream bos = null;
- try {
- bos = new Base64.OutputStream(
- new java.io.FileOutputStream( filename ), Base64.ENCODE );
- bos.write( dataToEncode );
- } // end try
- catch( java.io.IOException e ) {
- throw e; // Catch and throw to execute finally{} block
- } // end catch: java.io.IOException
- finally {
- try{ bos.close(); } catch( Exception e ){}
- } // end finally
-
- } // end encodeToFile
-
-
- /**
- * Convenience method for decoding data to a file.
- *
- * <p>As of v 2.3, if there is a error,
- * the method will throw an java.io.IOException. <b>This is new to v2.3!</b>
- * In earlier versions, it just returned false, but
- * in retrospect that's a pretty poor way to handle it.</p>
- *
- * @param dataToDecode Base64-encoded data as a string
- * @param filename Filename for saving decoded data
- * @throws java.io.IOException if there is an error
- * @since 2.1
- */
- public static void decodeToFile( String dataToDecode, String filename )
- throws java.io.IOException {
-
- Base64.OutputStream bos = null;
- try{
- bos = new Base64.OutputStream(
- new java.io.FileOutputStream( filename ), Base64.DECODE );
- bos.write( dataToDecode.getBytes( PREFERRED_ENCODING ) );
- } // end try
- catch( java.io.IOException e ) {
- throw e; // Catch and throw to execute finally{} block
- } // end catch: java.io.IOException
- finally {
- try{ bos.close(); } catch( Exception e ){}
- } // end finally
-
- } // end decodeToFile
-
-
-
-
- /**
- * Convenience method for reading a base64-encoded
- * file and decoding it.
- *
- * <p>As of v 2.3, if there is a error,
- * the method will throw an java.io.IOException. <b>This is new to v2.3!</b>
- * In earlier versions, it just returned false, but
- * in retrospect that's a pretty poor way to handle it.</p>
- *
- * @param filename Filename for reading encoded data
- * @return decoded byte array
- * @throws java.io.IOException if there is an error
- * @since 2.1
- */
- public static byte[] decodeFromFile( String filename )
- throws java.io.IOException {
-
- byte[] decodedData = null;
- Base64.InputStream bis = null;
- try
- {
- // Set up some useful variables
- java.io.File file = new java.io.File( filename );
- byte[] buffer = null;
- int length = 0;
- int numBytes = 0;
-
- // Check for size of file
- if( file.length() > Integer.MAX_VALUE )
- {
- throw new java.io.IOException( "File is too big for this convenience method (" + file.length() + " bytes)." );
- } // end if: file too big for int index
- buffer = new byte[ (int)file.length() ];
-
- // Open a stream
- bis = new Base64.InputStream(
- new java.io.BufferedInputStream(
- new java.io.FileInputStream( file ) ), Base64.DECODE );
-
- // Read until done
- while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 ) {
- length += numBytes;
- } // end while
-
- // Save in a variable to return
- decodedData = new byte[ length ];
- System.arraycopy( buffer, 0, decodedData, 0, length );
-
- } // end try
- catch( java.io.IOException e ) {
- throw e; // Catch and release to execute finally{}
- } // end catch: java.io.IOException
- finally {
- try{ bis.close(); } catch( Exception e) {}
- } // end finally
-
- return decodedData;
- } // end decodeFromFile
-
-
-
- /**
- * Convenience method for reading a binary file
- * and base64-encoding it.
- *
- * <p>As of v 2.3, if there is a error,
- * the method will throw an java.io.IOException. <b>This is new to v2.3!</b>
- * In earlier versions, it just returned false, but
- * in retrospect that's a pretty poor way to handle it.</p>
- *
- * @param filename Filename for reading binary data
- * @return base64-encoded string
- * @throws java.io.IOException if there is an error
- * @since 2.1
- */
- public static String encodeFromFile( String filename )
- throws java.io.IOException {
-
- String encodedData = null;
- Base64.InputStream bis = null;
- try
- {
- // Set up some useful variables
- java.io.File file = new java.io.File( filename );
- byte[] buffer = new byte[ Math.max((int)(file.length() * 1.4+1),40) ]; // Need max() for math on small files (v2.2.1); Need +1 for a few corner cases (v2.3.5)
- int length = 0;
- int numBytes = 0;
-
- // Open a stream
- bis = new Base64.InputStream(
- new java.io.BufferedInputStream(
- new java.io.FileInputStream( file ) ), Base64.ENCODE );
-
- // Read until done
- while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 ) {
- length += numBytes;
- } // end while
-
- // Save in a variable to return
- encodedData = new String( buffer, 0, length, Base64.PREFERRED_ENCODING );
-
- } // end try
- catch( java.io.IOException e ) {
- throw e; // Catch and release to execute finally{}
- } // end catch: java.io.IOException
- finally {
- try{ bis.close(); } catch( Exception e) {}
- } // end finally
-
- return encodedData;
- } // end encodeFromFile
-
- /**
- * Reads <tt>infile</tt> and encodes it to <tt>outfile</tt>.
- *
- * @param infile Input file
- * @param outfile Output file
- * @throws java.io.IOException if there is an error
- * @since 2.2
- */
- public static void encodeFileToFile( String infile, String outfile )
- throws java.io.IOException {
-
- String encoded = Base64.encodeFromFile( infile );
- java.io.OutputStream out = null;
- try{
- out = new java.io.BufferedOutputStream(
- new java.io.FileOutputStream( outfile ) );
- out.write( encoded.getBytes("US-ASCII") ); // Strict, 7-bit output.
- } // end try
- catch( java.io.IOException e ) {
- throw e; // Catch and release to execute finally{}
- } // end catch
- finally {
- try { out.close(); }
- catch( Exception ex ){}
- } // end finally
- } // end encodeFileToFile
-
-
- /**
- * Reads <tt>infile</tt> and decodes it to <tt>outfile</tt>.
- *
- * @param infile Input file
- * @param outfile Output file
- * @throws java.io.IOException if there is an error
- * @since 2.2
- */
- public static void decodeFileToFile( String infile, String outfile )
- throws java.io.IOException {
-
- byte[] decoded = Base64.decodeFromFile( infile );
- java.io.OutputStream out = null;
- try{
- out = new java.io.BufferedOutputStream(
- new java.io.FileOutputStream( outfile ) );
- out.write( decoded );
- } // end try
- catch( java.io.IOException e ) {
- throw e; // Catch and release to execute finally{}
- } // end catch
- finally {
- try { out.close(); }
- catch( Exception ex ){}
- } // end finally
- } // end decodeFileToFile
-
/* ******** I N N E R C L A S S I N P U T S T R E A M ******** */
@@ -1691,7 +1362,6 @@ public InputStream( java.io.InputStream in, int options ) {
* @return next byte
* @since 1.3
*/
- @Override
public int read() throws java.io.IOException {
// Do we need to get data?
@@ -1800,7 +1470,6 @@ else if( i == 0 ){
* @return bytes read into array or -1 if end of stream is encountered.
* @since 1.3
*/
- @Override
public int read( byte[] dest, int off, int len )
throws java.io.IOException {
int i;
@@ -1910,7 +1579,6 @@ public OutputStream( java.io.OutputStream out, int options ) {
* @param theByte the byte to write
* @since 1.3
*/
- @Override
public void write(int theByte)
throws java.io.IOException {
// Encoding suspended?
@@ -1965,7 +1633,6 @@ else if( decodabet[ theByte & 0x7f ] != WHITE_SPACE_ENC ) {
* @param len max number of bytes to read into array
* @since 1.3
*/
- @Override
public void write( byte[] theBytes, int off, int len )
throws java.io.IOException {
// Encoding suspended?
@@ -2006,7 +1673,6 @@ public void flushBase64() throws java.io.IOException {
*
* @since 1.3
*/
- @Override
public void close() throws java.io.IOException {
// 1. Ensure that pending characters are written
flushBase64();
Please sign in to comment.
Something went wrong with that request. Please try again.