@Override public void write(byte[] b, int off, int len) throws IOException { if (closed) { throw new IOException("Stream has been closed"); } for (int inputIndex = off; inputIndex < len + off; inputIndex++) { encode(b[inputIndex]); } }
@Override public void close() throws IOException { if (closed) { return; } try { flush(); } finally { closed = true; } }
private void writeToBuffer(byte next) throws IOException { outBuffer[outputIndex++] = next; if (outputIndex >= outBuffer.length) { flushOutput(); } }
private void writeSignSafeQuotedPrintable(OutputStream out, byte[] bytes) throws IOException { SignSafeOutputStream signSafeOutputStream = new SignSafeOutputStream(out); try { QuotedPrintableOutputStream signSafeQuotedPrintableOutputStream = new QuotedPrintableOutputStream(signSafeOutputStream, false); try { signSafeQuotedPrintableOutputStream.write(bytes); } finally { signSafeQuotedPrintableOutputStream.close(); } } finally { signSafeOutputStream.close(); } }
@Test public void testSignSafeOutputStream() throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); OutputStream output = new SignSafeOutputStream(byteArrayOutputStream); output.write(INPUT_STRING.getBytes("US-ASCII")); output.close(); assertEquals(EXPECTED_SIGNSAFE, new String(byteArrayOutputStream.toByteArray(), "US-ASCII")); }
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); } }
@Test public void testSignSafeQuotedPrintableOutputStream() throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); OutputStream signSafeOutputStream = new SignSafeOutputStream(byteArrayOutputStream); OutputStream quotedPrintableOutputStream = new QuotedPrintableOutputStream(signSafeOutputStream, false); quotedPrintableOutputStream.write(INPUT_STRING.getBytes("US-ASCII")); quotedPrintableOutputStream.close(); signSafeOutputStream.close(); assertEquals(EXPECTED_QUOTED_PRINTABLE_SIGNSAFE, new String(byteArrayOutputStream.toByteArray(), "US-ASCII")); }
@Override public void flush() throws IOException { flushOutput(); out.flush(); }
@Override public void write(int b) throws IOException { if (closed) { throw new IOException("Stream has been closed"); } encode((byte) b); }