return encodeBytes(source, 0, source.length, options);
/** * Decodes data from Base64 notation, automatically detecting * gzip-compressed data and decompressing it. * * @param s * the string to decode * @return the decoded data * @throws java.io.IOException * If there is a problem * @since 1.4 */ public static byte[] decode(String s) throws java.io.IOException { return decode(s, NO_OPTIONS); }
/** * 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); }
/** * Encodes up to the first three bytes of array <var>threeBytes</var> and * returns a four-byte array in Base64 notation. The actual number of * significant bytes in your array is given by <var>numSigBytes</var>. The * array <var>threeBytes</var> needs only be as big as * <var>numSigBytes</var>. Code can reuse a byte array by passing a * four-byte array as <var>b4</var>. * * @param b4 * A reusable byte array to reduce array instantiation * @param threeBytes * the array to convert * @param numSigBytes * the number of significant bytes in your array * @return four byte array in Base64 notation. * @since 1.5.1 */ private static byte[] encode3to4(byte[] b4, byte[] threeBytes, int numSigBytes, int options) { encode3to4(threeBytes, 0, numSigBytes, b4, 0, options); return b4; } // end encode3to4
byte[] DECODABET = getDecodabet(options); outBuffPosn += decode4to3(b4, 0, outBuff, outBuffPosn, options); b4Posn = 0;
/** * Similar to {@link #encodeBytes(byte[])} but returns a byte array instead * of instantiating a String. This is more efficient if you're working with * I/O streams and have large data sets to encode. * * * @param source * The data to convert * @return The Base64-encoded data as a byte[] (of ASCII characters) * @throws NullPointerException * if source array is null * @since 2.3.1 */ public static byte[] encodeBytesToBytes(byte[] source) { byte[] encoded = null; try { encoded = encodeBytesToBytes(source, 0, source.length, Base64.NO_OPTIONS); } catch (java.io.IOException ex) { assert false : "IOExceptions only come from GZipping, which is turned off: " + ex.getMessage(); } return encoded; }
/** * 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 { IOUtils.closeQuietly(out); } // 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 { IOUtils.closeQuietly(out); } // end finally } // end decodeFileToFile
/** * Serializes an object and returns the Base64-encoded version of that * serialized object. * * <p> * As of v 2.3, if the object cannot be serialized or there is another * error, the method will throw an java.io.IOException. <b>This is new to * v2.3!</b> In earlier versions, it just returned a null value, but in * retrospect that's a pretty poor way to handle it. * </p> * * The object is not GZip-compressed before being encoded. * * @param serializableObject * The object to encode * @return The Base64-encoded object * @throws java.io.IOException * if there is an error * @throws NullPointerException * if serializedObject is null * @since 1.4 */ public static String encodeObject(java.io.Serializable serializableObject) throws java.io.IOException { return encodeObject(serializableObject, NO_OPTIONS); } // end encodeObject
int options) { byte[] ALPHABET = getAlphabet(options);
/** * Encodes up to the first three bytes of array <var>threeBytes</var> and * returns a four-byte array in Base64 notation. The actual number of * significant bytes in your array is given by <var>numSigBytes</var>. The * array <var>threeBytes</var> needs only be as big as * <var>numSigBytes</var>. Code can reuse a byte array by passing a * four-byte array as <var>b4</var>. * * @param b4 * A reusable byte array to reduce array instantiation * @param threeBytes * the array to convert * @param numSigBytes * the number of significant bytes in your array * @return four byte array in Base64 notation. * @since 1.5.1 */ private static byte[] encode3to4(byte[] b4, byte[] threeBytes, int numSigBytes, int options) { encode3to4(threeBytes, 0, numSigBytes, b4, 0, options); return b4; } // end encode3to4
byte[] DECODABET = getDecodabet(options); outBuffPosn += decode4to3(b4, 0, outBuff, outBuffPosn, options); b4Posn = 0;
/** * Similar to {@link #encodeBytes(byte[])} but returns a byte array instead * of instantiating a String. This is more efficient if you're working with * I/O streams and have large data sets to encode. * * * @param source * The data to convert * @return The Base64-encoded data as a byte[] (of ASCII characters) * @throws NullPointerException * if source array is null * @since 2.3.1 */ public static byte[] encodeBytesToBytes(byte[] source) { byte[] encoded = null; try { encoded = encodeBytesToBytes(source, 0, source.length, Base64.NO_OPTIONS); } catch (java.io.IOException ex) { assert false : "IOExceptions only come from GZipping, which is turned off: " + ex.getMessage(); } return encoded; }
/** * 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 { IOUtils.closeQuietly(out); } // 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 { IOUtils.closeQuietly(out); } // end finally } // end decodeFileToFile
/** * Serializes an object and returns the Base64-encoded version of that * serialized object. * * <p> * As of v 2.3, if the object cannot be serialized or there is another * error, the method will throw an java.io.IOException. <b>This is new to * v2.3!</b> In earlier versions, it just returned a null value, but in * retrospect that's a pretty poor way to handle it. * </p> * * The object is not GZip-compressed before being encoded. * * @param serializableObject * The object to encode * @return The Base64-encoded object * @throws java.io.IOException * if there is an error * @throws NullPointerException * if serializedObject is null * @since 1.4 */ public static String encodeObject(java.io.Serializable serializableObject) throws java.io.IOException { return encodeObject(serializableObject, NO_OPTIONS); } // end encodeObject
int options) { byte[] ALPHABET = getAlphabet(options);
return encodeBytes(source, 0, source.length, options);
/** * Decodes data from Base64 notation, automatically detecting * gzip-compressed data and decompressing it. * * @param s * the string to decode * @return the decoded data * @throws java.io.IOException * If there is a problem * @since 1.4 */ public static byte[] decode(String s) throws java.io.IOException { return decode(s, NO_OPTIONS); }
/** * Performs Base64 encoding on the <code>raw</code> ByteBuffer, writing it * to the <code>encoded</code> ByteBuffer. This is an experimental feature. * Currently it does not pass along any options (such as * {@link #DO_BREAK_LINES} or {@link #GZIP}. * * @param raw * input buffer * @param encoded * output buffer * @since 2.3 */ public static void encode(java.nio.ByteBuffer raw, java.nio.ByteBuffer encoded) { byte[] raw3 = new byte[3]; byte[] enc4 = new byte[4]; while (raw.hasRemaining()) { int rem = Math.min(3, raw.remaining()); raw.get(raw3, 0, rem); Base64.encode3to4(enc4, raw3, rem, Base64.NO_OPTIONS); encoded.put(enc4); } // end input remaining }