b64os = new Base64.OutputStream(baos, ENCODE | options); gzos = new java.util.zip.GZIPOutputStream(b64os);
/** * Flushes and closes (I think, in the superclass) the stream. * * {@inheritDoc} * * @since 1.3 */ @Override public void close() throws IOException { // 1. Ensure that pending characters are written flushBase64(); // 2. Actually close the stream (Base class both flushes and closes) super.close(); // this.buffer = null; this.out = null; }
/** * 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); } }
b64os = new Base64.OutputStream(baos, ENCODE | options); if ((options & GZIP) != 0) {
/** * 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); } }
/** * Calls {@link #write(int)} repeatedly until <var>len</var> bytes are written. * * {@inheritDoc} * * @param theBytes array from which to read bytes * @param off offset for array * @param len max number of bytes to read into array * @since 1.3 */ @Override public void write(byte[] theBytes, int off, int len) throws IOException { // Encoding suspended? if (this.suspendEncoding) { this.out.write(theBytes, off, len); return; } for (int i = 0; i < len; i++) { write(theBytes[ off + i ]); } }
/** * Suspends encoding of the stream. * * <p>May be helpful if you need to embed a piece of base64-encoded data in a stream. * * @throws java.io.IOException if there's an error flushing * @since 1.5.1 */ public void suspendEncoding() throws IOException { flushBase64(); this.suspendEncoding = true; }