### Question:
BinaryInputArchive implements InputArchive { public String readString(String tag) throws IOException { int len = in.readInt(); if (len == -1) { return null; } checkLength(len); byte[] b = new byte[len]; in.readFully(b); return new String(b, StandardCharsets.UTF_8); } BinaryInputArchive(DataInput in);  BinaryInputArchive(DataInput in, int maxBufferSize, int extraMaxBufferSize); static BinaryInputArchive getArchive(InputStream strm); byte readByte(String tag); boolean readBool(String tag); int readInt(String tag); long readLong(String tag); float readFloat(String tag); double readDouble(String tag); String readString(String tag); byte[] readBuffer(String tag); void readRecord(Record r, String tag); void startRecord(String tag); void endRecord(String tag); Index startVector(String tag); void endVector(String tag); Index startMap(String tag); void endMap(String tag); static final String UNREASONBLE_LENGTH; static final int maxBuffer; }### Answer:
@Test public void testString() { final String expected = "hello"; final String tag = "tag1"; checkWriterAndReader( (oa) -> oa.writeString(expected, tag), (ia) -> { String actual = ia.readString(tag); assertEquals(expected, actual); } ); }
@Test public void testReadStringForRecordsHavingLengthMoreThanMaxAllowedSize() { int maxBufferSize = 2000; int extraMaxBufferSize = 1025; int recordSize = maxBufferSize + extraMaxBufferSize + 100; BinaryInputArchive ia = getBinaryInputArchive(recordSize, maxBufferSize, extraMaxBufferSize); try { ia.readString(""); fail("Should have thrown an IOException"); } catch (IOException e) { assertTrue(e.getMessage().startsWith(BinaryInputArchive.UNREASONBLE_LENGTH), () -> "Not 'Unreasonable length' exception: " + e); } }
@Test public void testReadStringForRecordsHavingLengthLessThanMaxAllowedSize() throws IOException { int maxBufferSize = 2000; int extraMaxBufferSize = 1025; int recordSize = maxBufferSize + extraMaxBufferSize - 100; BinaryInputArchive ia = getBinaryInputArchive(recordSize, maxBufferSize, extraMaxBufferSize); String s = ia.readString(""); assertNotNull(s); assertEquals(recordSize, s.getBytes().length); }