public void TestWriteUTF() { //ByteArrayOutputStream baos = new ByteArrayOutputStream(); var baos = new MemoryStream(); DataOutputStream dos = new DataOutputStream(baos); dos.WriteUTF("Hello, World!"); // 15 dos.Flush(); if (baos.Length != dos.Length) { fail("Miscounted bytes in DataOutputStream."); } }
private static void WriteAndReadAString() { // Write out a string whose UTF-8 encoding is quite possibly // longer than 65535 bytes int length = Random.Next(A_NUMBER_NEAR_65535) + 1; MemoryStream baos = new MemoryStream(); StringBuilder testBuffer = new StringBuilder(); for (int i = 0; i < length; i++) { testBuffer.Append((char)Random.Next()); } string testString = testBuffer.ToString(); DataOutputStream dos = new DataOutputStream(baos); dos.WriteUTF(testString); // Corrupt the data to produce malformed characters byte[] testBytes = baos.ToArray(); int dataLength = testBytes.Length; int corruptions = Random.Next(MAX_CORRUPTIONS_PER_CYCLE); for (int i = 0; i < corruptions; i++) { int index = Random.Next(dataLength); testBytes[index] = (byte)Random.Next(); } // Pay special attention to mangling the end to produce // partial characters at end testBytes[dataLength - 1] = (byte)Random.Next(); testBytes[dataLength - 2] = (byte)Random.Next(); // Attempt to decode the bytes back into a String MemoryStream bais = new MemoryStream(testBytes); DataInputStream dis = new DataInputStream(bais); dis.ReadUTF(); }