@Test public void shouldFormatMultibyteUTF8CharactersAsBinary() throws Exception { Mockery context = new Mockery(); final Emitter emitter = context.mock(Emitter.class); ByteArrayWriter writer = new ByteArrayWriter(Type.READ, emitter); final byte[] bytes = new byte[] { (byte) 0xBD, (byte) 0xF3, 0x2B, 0x2C, (byte) 0x9F, 0x1B, (byte) 0xB2, 0x0B, (byte) 0xA5, 0x4A, (byte) 0x90, 0x7A, (byte) 0xA0, 0x20, (byte) 0xD4, (byte) 0xAA, 0x4A, 0x55, 0x11, (byte) 0xD2, 0x1E, 0x4A, 0x0F }; context.checking(new Expectations() { { oneOf(emitter).add("read [" + Util.getHexFromBytes(bytes) + "]"); oneOf(emitter).add("\n"); } }); writer.write(bytes); }
public void write(byte[] bytes) { this.bytes = bytes; start = 0; state = State.BINARY; for (position=0; position < bytes.length; position++) { int codePoint = bytes[position] & 0xFF; if (shouldTreatAsText(codePoint)) { switch(state) { case BINARY: transitionBinaryToCandidateText(); break; case CANDIDATE_TEXT: if (position + 1 - startCandidateText == ASSUME_TEXT_AFTER) { transitionCandidateTextToText(); } break; default: break; } } else { switch(state) { case CANDIDATE_TEXT: transitionCandidateTextToBinary(); break; case TEXT: transitionTextToBinary(); break; default: break; } } } transitionToEnd(); }
ByteArrayWriter { public void write(byte[] bytes) { this.bytes = bytes; start = 0; state = State.BINARY; for (position=0; position < bytes.length; position++) { int codePoint = bytes[position] & 0xFF; if (shouldTreatAsText(codePoint)) { switch(state) { case BINARY: transitionBinaryToCandidateText(); break; case CANDIDATE_TEXT: if (position + 1 - startCandidateText == ASSUME_TEXT_AFTER) { transitionCandidateTextToText(); } break; default: break; } } else { switch(state) { case CANDIDATE_TEXT: transitionCandidateTextToBinary(); break; case TEXT: transitionTextToBinary(); break; default: break; } } } transitionToEnd(); } }
ByteArrayWriter { public void write(byte[] bytes) { this.bytes = bytes; start = 0; state = State.BINARY; for (position=0; position < bytes.length; position++) { int codePoint = bytes[position] & 0xFF; if (shouldTreatAsText(codePoint)) { switch(state) { case BINARY: transitionBinaryToCandidateText(); break; case CANDIDATE_TEXT: if (position + 1 - startCandidateText == ASSUME_TEXT_AFTER) { transitionCandidateTextToText(); } break; default: break; } } else { switch(state) { case CANDIDATE_TEXT: transitionCandidateTextToBinary(); break; case TEXT: transitionTextToBinary(); break; default: break; } } } transitionToEnd(); } ByteArrayWriter(Type type, Emitter emitter); private  ByteArrayWriter(Emitter emitter, String textStart, String textEnd, String hexStart, String hexEnd); }
ByteArrayWriter { public void write(byte[] bytes) { this.bytes = bytes; start = 0; state = State.BINARY; for (position=0; position < bytes.length; position++) { int codePoint = bytes[position] & 0xFF; if (shouldTreatAsText(codePoint)) { switch(state) { case BINARY: transitionBinaryToCandidateText(); break; case CANDIDATE_TEXT: if (position + 1 - startCandidateText == ASSUME_TEXT_AFTER) { transitionCandidateTextToText(); } break; default: break; } } else { switch(state) { case CANDIDATE_TEXT: transitionCandidateTextToBinary(); break; case TEXT: transitionTextToBinary(); break; default: break; } } } transitionToEnd(); } ByteArrayWriter(Type type, Emitter emitter); private  ByteArrayWriter(Emitter emitter, String textStart, String textEnd, String hexStart, String hexEnd); void write(byte[] bytes); }
ByteArrayWriter { public void write(byte[] bytes) { this.bytes = bytes; start = 0; state = State.BINARY; for (position=0; position < bytes.length; position++) { int codePoint = bytes[position] & 0xFF; if (shouldTreatAsText(codePoint)) { switch(state) { case BINARY: transitionBinaryToCandidateText(); break; case CANDIDATE_TEXT: if (position + 1 - startCandidateText == ASSUME_TEXT_AFTER) { transitionCandidateTextToText(); } break; default: break; } } else { switch(state) { case CANDIDATE_TEXT: transitionCandidateTextToBinary(); break; case TEXT: transitionTextToBinary(); break; default: break; } } } transitionToEnd(); } ByteArrayWriter(Type type, Emitter emitter); private  ByteArrayWriter(Emitter emitter, String textStart, String textEnd, String hexStart, String hexEnd); void write(byte[] bytes); static final int MAX_TEXT_LINE_SIZE; }