public void encode(byte next) throws IOException { State nextState = state.nextState(next); if (nextState == State.SPACE_FROM) { state = State.INIT; writeToBuffer(ESCAPED_SPACE[0]); writeToBuffer(ESCAPED_SPACE[1]); writeToBuffer(ESCAPED_SPACE[2]); } else { state = nextState; writeToBuffer(next); } }
/** * Closes this output stream, flushing any remaining bytes that must be encoded. The * underlying stream is flushed but not closed. */ @Override public void close() throws IOException { // Notify encoder of EOF (-1). if (doEncode) { base64.encode(singleByte, 0, -1); } else { base64.decode(singleByte, 0, -1); } flush(); }
/** * Flushes this output stream and forces any buffered output bytes * to be written out to the stream. * * @throws IOException if an I/O error occurs. */ @Override public void flush() throws IOException { flush(true); }
@Test public void skipRemaining_shouldExhaustStream() throws IOException { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 5); fixedLengthInputStream.skipRemaining(); assertInputStreamExhausted(fixedLengthInputStream); }
@Test public void read_withOverSizedByteArray_shouldNotConsumeMoreThanLimitFromUnderlyingStream() throws Exception { InputStream inputStream = inputStream("Hello World"); FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream, 6); //noinspection ResultOfMethodCallIgnored fixedLengthInputStream.read(new byte[100]); assertRemainingInputStreamEquals("World", inputStream); }
@Test public void skipRemaining_shouldNotConsumeMoreThanLimitFromUnderlyingInputStream() throws IOException { InputStream inputStream = inputStream("Hello World"); FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream, 6); fixedLengthInputStream.skipRemaining(); assertRemainingInputStreamEquals("World", inputStream); }
/** * Encodes binary data using the base64 algorithm but does not chunk the output. * * @param binaryData * binary data to encode * @return Base64 characters */ public static byte[] encodeBase64(byte[] binaryData) { return encodeBase64(binaryData, false); }
@Test public void readingStream_shouldNotConsumeMoreThanLimitFromUnderlyingInputStream() throws Exception { InputStream inputStream = inputStream("Hello world"); FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream, 5); exhaustStream(fixedLengthInputStream); assertRemainingInputStreamEquals(" world", inputStream); }
/** * Decodes an Object using the base64 algorithm. This method is provided in order to satisfy the requirements of the * Decoder interface, and will throw a DecoderException if the supplied object is not of type byte[]. * * @param pObject * Object to decode * @return An object (of type byte[]) containing the binary data which corresponds to the byte[] supplied. * @throws DecoderException * if the parameter supplied is not of type byte[] */ public Object decode(Object pObject) throws DecoderException { if (!(pObject instanceof byte[])) { throw new DecoderException("Parameter supplied to Base64 decode is not a byte[]"); } return decode((byte[]) pObject); }
@Override public int read(byte[] b) throws IOException { return read(b, 0, b.length); }
private void completeCrLf() throws IOException { if (lastByte == CR) { writeByte(LF); // We have to ignore the next character if it is <LF>. Otherwise it // will be expanded to an additional <CR><LF> sequence although it // belongs to the one just completed. ignoreLf = true; } }
/** * Decodes a byte[] containing containing characters in the Base64 alphabet. * * @param pArray * A byte array containing Base64 character data * @return a byte array containing binary data */ public byte[] decode(byte[] pArray) { return decodeBase64(pArray); }
private void writeToBuffer(byte next) throws IOException { outBuffer[outputIndex++] = next; if (outputIndex >= outBuffer.length) { flushOutput(); } }
/** * Writes the specified <code>byte</code> to this output stream. */ @Override public void write(int i) throws IOException { singleByte[0] = (byte) i; write(singleByte, 0, 1); }
private static boolean containsBase64Byte(byte[] arrayOctet) { for (byte element : arrayOctet) { if (isBase64(element)) { return true; } } return false; }
@Override public void close() throws IOException { if (closed) { return; } try { flush(); } finally { closed = true; } }
/** * Creates a Base64OutputStream such that all data written is either * Base64-encoded or Base64-decoded to the original provided OutputStream. * * @param out OutputStream to wrap. * @param doEncode true if we should encode all data written to us, * false if we should decode. */ public Base64OutputStream(OutputStream out, boolean doEncode) { super(out); this.doEncode = doEncode; this.base64 = new Base64(); }
/** * Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 alphabet. * * @param pArray * a byte array containing binary data * @return A byte array containing only Base64 character data */ public byte[] encode(byte[] pArray) { return encodeBase64(pArray, false); }
/** * Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks * * @param binaryData * binary data to encode * @return Base64 characters chunked in 76 character blocks */ public static byte[] encodeBase64Chunked(byte[] binaryData) { return encodeBase64(binaryData, true); }