publicstaticfinalvoid main(String[] args) throws Exception { for (int i=0; i<TEST_ITERATIONS; i++) { try {
writeAndReadAString();
} catch (UTFDataFormatException utfdfe) { if (utfdfe.getMessage() == null) thrownew RuntimeException("vague exception thrown");
} catch (EOFException eofe) { // These are rare and beyond the scope of the test
}
}
}
privatestaticvoid writeAndReadAString() throws Exception { // Write out a string whose UTF-8 encoding is quite possibly // longer than 65535 bytes int length = generator.nextInt(A_NUMBER_NEAR_65535) + 1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
StringBuffer testBuffer = new StringBuffer(); for (int i=0; i<length; i++)
testBuffer.append((char)generator.nextInt());
String testString = testBuffer.toString();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(testString);
// Corrupt the data to produce malformed characters byte[] testBytes = baos.toByteArray(); int dataLength = testBytes.length; int corruptions = generator.nextInt(MAX_CORRUPTIONS_PER_CYCLE); for (int i=0; i<corruptions; i++) { int index = generator.nextInt(dataLength);
testBytes[index] = (byte)generator.nextInt();
}
// Pay special attention to mangling the end to produce // partial characters at end
testBytes[dataLength-1] = (byte)generator.nextInt();
testBytes[dataLength-2] = (byte)generator.nextInt();
// Attempt to decode the bytes back into a String
ByteArrayInputStream bais = new ByteArrayInputStream(testBytes);
DataInputStream dis = new DataInputStream(bais);
dis.readUTF();
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.