/** * 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); }
return encodeBytes(source, 0, source.length, options);
/** * Encodes content of the supplied InputStream into Base64 notation. Does not GZip-compress data. * * @param source The data to convert * @return the encoded bytes */ public static String encode( java.io.InputStream source ) { return encode(source, NO_OPTIONS); }
@Test public void shouldEncodeStreamableValue() throws IOException { String actualValue = "propertyValue"; byte[] actualBytes = actualValue.getBytes(); InputStream actualStream = new ByteArrayInputStream(actualBytes); String encoded = Base64.encode(actualStream); String encoded2 = Base64.encodeBytes(actualBytes); assertThat(encoded, is(encoded2)); byte[] decoded = Base64.decode(encoded); String decodedValue = new String(decoded); assertThat(decodedValue, is(actualValue)); }
@Test public void shouldEncodeStringValue() throws UnsupportedEncodingException, IOException { String actualValue = "propertyValue"; String encoded = Base64.encodeBytes(actualValue.getBytes("UTF-8")); byte[] decoded = Base64.decode(encoded); String decodedValue = new String(decoded, "UTF-8"); assertThat(decodedValue, is(actualValue)); }
/** * 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 * @param options the options * @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; }
/** * 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); }
/** * 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 { if (out != null) out.close(); } catch (Exception ex) { } } // end finally } // end decodeFileToFile
@Test public void testBasicExamples() throws IOException { // Make up some source objects byte[] originalBytes = {(byte)-2, (byte)-1, (byte)0, (byte)1, (byte)2}; // Display original array System.out.println("\n\nOriginal array: "); for (int i = 0; i < originalBytes.length; i++) System.out.print(originalBytes[i] + " "); System.out.println(); // Encode serialized bytes String encBytes = Base64.encodeBytes(originalBytes); // Print encoded bytes System.out.println("Bytes, encoded ( " + encBytes.getBytes().length + " bytes):\n" + encBytes); // Decode bytes byte[] decBytes = Base64.decode(encBytes); // Display decoded bytes System.out.println("Encoded Bytes -> decoded: "); for (int i = 0; i < decBytes.length; i++) System.out.print(decBytes[i] + " "); System.out.println(); }
/** * 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 * @param options the options * @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; }
/** * 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); }
/** * 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 { if (out != null) out.close(); } catch (Exception ex) { } } // end finally } // end decodeFileToFile
/** * 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); }
return encodeBytes(source, 0, source.length, 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 * @param options the options * @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;