/** * 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 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); } catch (IOException ex) { throw ex; } finally { closeQuietly(out); } }
/** * 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 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 IOException, java.lang.ClassNotFoundException { return decodeToObject(encodedObject, NO_OPTIONS, null); }
/** * 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 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. } catch (IOException ex) { throw ex; } finally { closeQuietly(out); } }
byte[] objBytes = decode(encodedObject, options); throw ex; } finally { closeQuietly(bais); closeQuietly(ois);
"Base64-encoded string must have at least four characters, but length specified was " + len); byte[] decodabet = getDecodabet(options); outBuffPosn += decode4to3(b4, 0, outBuff, outBuffPosn, options); b4Posn = 0;
throw ex; } finally { closeQuietly(gzos); closeQuietly(b64os); closeQuietly(baos); int lineLength = 0; for ( ; d < len2; d += 3, e += 4) { encode3to4(source, d+off, 3, outBuff, e, options); lineLength += 4; if (breakLines && lineLength >= MAX_LINE_LENGTH) { encode3to4(source, d+off, len - d, outBuff, e, options); e += 4;
closeQuietly(oos); closeQuietly(gzos); closeQuietly(b64os); closeQuietly(baos);
/** * 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; }
/** * Encodes a byte array into Base64 notation. * * Does not GZip-compress data. * * @param source The data to convert * @return The data in Base64-encoded form * * @throws NullPointerException if source array is <code>null</code> * @since 1.4 */ public static String encodeBytes(byte[] source) { String encoded = null; try { encoded = encodeBytes(source, 0, source.length, NO_OPTIONS); } catch (IOException ex) { // Since we're not going to have the GZIP encoding turned on, // we're not going to have an IOException thrown, so // we should not force the user to have to catch it. encoded = ""; } return encoded; }
/** * 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 IOException { return encodeFromFile(new File(filename)); }
/** * Similar to {@link #encodeBytes(byte[])} but returns a byte array instead of instantiating a String. * * <p>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 <code>null</code> * @since 2.3.1 * */ public static byte[] encodeBytesToBytes(byte[] source) { byte[] encoded = null; try { encoded = encodeBytesToBytes(source, 0, source.length, Base64.NO_OPTIONS); } catch (IOException ex) { // IOExceptions only come from GZipping, which is turned off encoded = new byte[]{}; } return encoded; }
/** * 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 IOException. <b>This is new to v2.3!</b>. * * <p>In earlier versions, it just returned a <code>null</code> 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 <code>null</code> * * @deprecated Will be removed in 0.12.x release * * @since 1.4 */ @Deprecated public static String encodeObject(Serializable serializableObject) throws IOException { return encodeObject(serializableObject, NO_OPTIONS); }
bytes = decode(bytes, 0, bytes.length, options); closeQuietly(baos); closeQuietly(gzis); closeQuietly(bais);
/** * 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 IOException { Base64.OutputStream bos = null; try { bos = new Base64.OutputStream(new java.io.FileOutputStream(filename), Base64.DECODE); bos.write(dataToDecode.getBytes(PREFERRED_ENCODING)); } catch (IOException ex) { throw ex; } finally { closeQuietly(bos); } }
/** * 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 * * @deprecated Will be removed in 0.12.x release */ @Deprecated 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); } }
return encodeBytes(source, 0, source.length, options);
location.append(Base64.encodeFromFile(ftarget)); } else { String csource = ftarget.getCanonicalPath();
byte[] encoded = encodeBytesToBytes(source, off, len, options);
/** * 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 IOException if there is an error * @throws NullPointerException if dataToEncode is null * @since 2.1 */ public static void encodeToFile(byte[] dataToEncode, String filename) throws IOException { Objects.requireNonNull(dataToEncode, "Data to encode was null."); Base64.OutputStream bos = null; try { bos = new Base64.OutputStream(new java.io.FileOutputStream(filename), Base64.ENCODE); bos.write(dataToEncode); } catch (IOException ex) { throw ex; } finally { closeQuietly(bos); } }