privatevoid testDeserialization() {
TextField textFieldToSerialize = new TextField(testStrEOL);
textFieldToSerialize.setSize(200, 100);
mainFrame.add(textFieldToSerialize); try { // Serialize TextField object "textFieldToSerialize".
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutput outStream = new ObjectOutputStream(baos);
outStream.writeObject(textFieldToSerialize);
// Search the text variable data through serialized object stream. byte[] streamedBytes = baos.toByteArray(); int foundLoc = 0; for (int i = 0; i < streamedBytes.length; ++i) { if (streamedBytes[i] == expectedString.charAt(0)) {
foundLoc = i; int j = 1; for (; j < expectedString.length(); ++j) { if (streamedBytes[i+j] != expectedString.charAt(j)) { break;
}
} if (j == expectedString.length()) { break;
}
}
foundLoc = -1;
}
if (foundLoc == -1) { // Could not find text data in serialized object stream. thrownew Exception("Could not find text data in serialized "
+ "object stream.");
} // Replace space character from serialized stream with // EOL character for testing de-serialization.
String EOLChar = System.lineSeparator();
String newExpectedString = ""; for (int i = foundLoc, j = 0; j < expectedString.length(); ++i, ++j) {
newExpectedString += (char)(streamedBytes[i]); if (streamedBytes[i] == ' ') { int k = 0; for (; k < EOLChar.length(); ++k) {
streamedBytes[i + k] = (byte) EOLChar.charAt(k);
}
i += k-1;
j += k-1;
}
} // New line character varies with platform, // ex. For windows '\r\n', for linux '\n'. // While replacing space from serialized object stream, the length // of EOL character will affect the expected string as well.
expectedString = newExpectedString;
// De-serialize TextField object stream.
ByteArrayInputStream bais = new ByteArrayInputStream(streamedBytes);
ObjectInput inStream = new ObjectInputStream(bais);
textField = (TextField) inStream.readObject();
} catch (Exception ex) { // Serialization or De-serialization failed. // Create textField with empty string to show failure.
ex.printStackTrace();
textField = new TextField();
}
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.