下面列出了怎么用java.io.ObjectInput的API类实例代码及写法,或者点击链接到github查看源代码。
public void readExternal(ObjectInput in)
throws IOException, ClassNotFoundException {
// VERSION
byte version = in.readByte();
// NOTE: super was not written in version 0
if (version != 0) {
super.readExternal(in);
}
// NUMBER OF ENTRIES
int size = in.readInt();
setUp(size);
// ENTRIES
while (size-- > 0) {
// noinspection unchecked
K key = (K) in.readObject();
// noinspection unchecked
V val = (V) in.readObject();
put(key, val);
}
}
/** {@inheritDoc} */
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
interpolator = (StepInterpolator) in.readObject();
final int n = in.readInt();
final int k = in.readInt();
y = new double[n];
dydy0 = new double[n][n];
dydp = new double[n][k];
yDot = new double[n];
dydy0Dot = new double[n][n];
dydpDot = new double[n][k];
readArray(in, y);
readArray(in, dydy0);
readArray(in, dydp);
readArray(in, yDot);
readArray(in, dydy0Dot);
readArray(in, dydpDot);
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
SimpleHistogramBin b1 = new SimpleHistogramBin(1.0, 2.0, false, true);
b1.setItemCount(123);
SimpleHistogramBin b2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(b1);
out.close();
ObjectInput in = new ObjectInputStream(
new ByteArrayInputStream(buffer.toByteArray())
);
b2 = (SimpleHistogramBin) in.readObject();
in.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
assertEquals(b1, b2);
}
public void readExternal(ObjectInput in)
throws IOException, ClassNotFoundException
{
z = in.readBoolean();
b = in.readByte();
c = in.readChar();
s = in.readShort();
i = in.readInt();
f = in.readFloat();
j = in.readLong();
d = in.readDouble();
str = (String) in.readObject();
parent = in.readObject();
left = in.readObject();
right = in.readObject();
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
OHLCItem item1 = new OHLCItem(new Year(2006), 2.0, 4.0, 1.0, 3.0);
OHLCItem item2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(item1);
out.close();
ObjectInput in = new ObjectInputStream(
new ByteArrayInputStream(buffer.toByteArray()));
item2 = (OHLCItem) in.readObject();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
assertEquals(item1, item2);
}
@Override
public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
final QName qname = QName.readFrom(in);
final int size = in.readInt();
switch (size) {
case 0:
nip = NodeIdentifierWithPredicates.of(qname);
break;
case 1:
nip = NodeIdentifierWithPredicates.of(qname, QName.readFrom(in), in.readObject());
break;
default:
final Builder<QName, Object> keys = ImmutableMap.builderWithExpectedSize(size);
for (int i = 0; i < size; ++i) {
keys.put(QName.readFrom(in), in.readObject());
}
nip = NodeIdentifierWithPredicates.of(qname, keys.build());
}
}
public void readExternal(ObjectInput in)
throws IOException, ClassNotFoundException {
// VERSION
in.readByte();
// LOAD FACTOR
float old_factor = _loadFactor;
_loadFactor = Math.abs(in.readFloat());
// AUTO COMPACTION LOAD FACTOR
_autoCompactionFactor = in.readFloat();
// If we change the laod factor from the default, re-setup
if (old_factor != _loadFactor) {
setUp(
saturatedCast(
(long) Math.ceil(
DEFAULT_CAPACITY / (double) _loadFactor)));
}
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
OHLC i1 = new OHLC(2.0, 4.0, 1.0, 3.0);
OHLC i2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(i1);
out.close();
ObjectInput in = new ObjectInputStream(
new ByteArrayInputStream(buffer.toByteArray()));
i2 = (OHLC) in.readObject();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
assertEquals(i1, i2);
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
YIntervalRenderer r1 = new YIntervalRenderer();
YIntervalRenderer r2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(r1);
out.close();
ObjectInput in = new ObjectInputStream(
new ByteArrayInputStream(buffer.toByteArray()));
r2 = (YIntervalRenderer) in.readObject();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
assertEquals(r1, r2);
}
/**
* Serialize an instance, restore it, and check for identity.
*/
public void testSerialization() {
final RectangleAnchor a1 = RectangleAnchor.RIGHT;
RectangleAnchor a2 = null;
try {
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
final ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(a1);
out.close();
final ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
a2 = (RectangleAnchor) in.readObject();
in.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
assertTrue(a1 == a2);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
boolean hasAttributes = in.readBoolean();
if (hasAttributes)
{
attributeNames = new ArrayList<>();
int count = in.readInt();
for (int i = 0; i < count; i++)
{
attributeNames.add(in.readUTF());
}
}
xCol = in.readInt();
yCol = in.readInt();
geometryCol = in.readInt();
delimiter = in.readChar();
encapsulator = in.readChar();
skipFirstLine = in.readBoolean();
}
public void receive(byte code, ObjectInput in)
throws IOException, ClassNotFoundException {
JClientRequestInfo info = new JRMPClientRequestInfoImpl();
int len = in.readShort();
for (int i = 0; i < len; i++) {
info.add_request_service_context((JServiceContext) in.readObject());
}
for (int i = 0; i < cis.length; i++) {
switch (code) {
case METHOD_RESULT:
cis[i].receive_reply(info);
break;
case METHOD_ERROR:
case SYSTEM_ERROR:
cis[i].receive_exception(info);
break;
}
}
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
XYCoordinate v1 = new XYCoordinate(1.0, 2.0);
XYCoordinate v2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(v1);
out.close();
ObjectInput in = new ObjectInputStream(
new ByteArrayInputStream(buffer.toByteArray()));
v2 = (XYCoordinate) in.readObject();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
assertEquals(v1, v2);
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
EmptyBlock b1 = new EmptyBlock(1.0, 2.0);
EmptyBlock b2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(b1);
out.close();
ObjectInput in = new ObjectInputStream(
new ByteArrayInputStream(buffer.toByteArray()));
b2 = (EmptyBlock) in.readObject();
in.close();
}
catch (Exception e) {
fail(e.toString());
}
assertEquals(b1, b2);
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
SymbolicXYItemLabelGenerator g1 = new SymbolicXYItemLabelGenerator();
SymbolicXYItemLabelGenerator g2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(g1);
out.close();
ObjectInput in = new ObjectInputStream(
new ByteArrayInputStream(buffer.toByteArray())
);
g2 = (SymbolicXYItemLabelGenerator) in.readObject();
in.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
assertEquals(g1, g2);
}
ModuleData(ObjectInput dis) throws IOException {
try {
this.codeName = dis.readUTF();
this.codeNameBase = dis.readUTF();
this.codeNameRelease = dis.readInt();
this.coveredPackages = readStrings(dis, new HashSet<String>(), true);
this.dependencies = (Dependency[]) dis.readObject();
this.implVersion = dis.readUTF();
this.buildVersion = dis.readUTF();
this.provides = readStrings(dis);
this.friendNames = readStrings(dis, new HashSet<String>(), false);
this.specVers = new SpecificationVersion(dis.readUTF());
this.publicPackages = Module.PackageExport.read(dis);
this.agentClass = dis.readUTF();
String s = dis.readUTF();
if (s != null) {
s = s.trim();
}
this.fragmentHostCodeName = s == null || s.isEmpty() ? null : s;
} catch (ClassNotFoundException cnfe) {
throw new IOException(cnfe);
}
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
OHLCItem item1 = new OHLCItem(new Year(2006), 2.0, 4.0, 1.0, 3.0);
OHLCItem item2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(item1);
out.close();
ObjectInput in = new ObjectInputStream(
new ByteArrayInputStream(buffer.toByteArray()));
item2 = (OHLCItem) in.readObject();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
assertEquals(item1, item2);
}
/**
* Sets a filter for invocation arguments, if a filter has been set.
* Called by dispatch before the arguments are read.
*/
protected void unmarshalCustomCallData(ObjectInput in)
throws IOException, ClassNotFoundException {
if (filter != null &&
in instanceof ObjectInputStream) {
// Set the filter on the stream
ObjectInputStream ois = (ObjectInputStream) in;
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
ObjectInputFilter.Config.setObjectInputFilter(ois, filter);
return null;
}
});
}
}
@Override
public RoleUpdatedEvent readObject(ObjectInput input) throws IOException, ClassNotFoundException {
switch (input.readByte()) {
case VERSION_1:
return readObjectVersion1(input);
default:
throw new IOException("Unknown version");
}
}
static HijrahDate readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
HijrahChronology chrono = (HijrahChronology) in.readObject();
int year = in.readInt();
int month = in.readByte();
int dayOfMonth = in.readByte();
return chrono.date(year, month, dayOfMonth);
}
/**
* @see java.io.Externalizable#readExternal
*
* @exception IOException on error
*/
public void readExternal(ObjectInput in)
throws IOException, ClassNotFoundException
{
aggregator = (SumAggregator)in.readObject(); //TODO -sf- is this the most efficient? perhaps better to get the sum direct
eliminatedNulls = in.readBoolean();
count = in.readLong();
scale = in.readInt();
}
@Override
public void readExternal(final ObjectInput in)
throws IOException, ClassNotFoundException
{
_type = in.readByte();
switch (_type) {
case OBJECT_STORE: _object = ObjectStore.read(in); break;
case ARRAY: _object = Array.read(in); break;
case CHAR_STORE: _object = CharStore.read(in); break;
default:
throw new StreamCorruptedException("Unknown serialized type.");
}
}
/**
* delegated to bit
*
* @exception IOException io exception
* @exception ClassNotFoundException class not found
*/
public final void readExternal(ObjectInput in) throws IOException
{
// need to clear stream first, in case this object is reused, and
// stream is set by previous use. Track 3794.
stream = null;
streamValueLength = -1;
_blobValue = null;
if (in instanceof FormatIdInputStream) {
} else {
isNull = in.readBoolean();
if (isNull) {
return;
}
}
int len = SQLBinary.readBinaryLength(in);
if (len != 0)
{
dataValue = new byte[len];
in.readFully(dataValue);
}
else
{
readFromStream((InputStream) in);
}
isNull = evaluateNull();
}
@Override
public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
final byte version = in.readByte(); // future use
responseCode = in.readByte();
switch (responseCode) {
case ResponseCodes.AUTH_GRANTED:
identity = new ClientMetaData();
identity.setMetaData(metaData);
identity.readExternal(in);
break;
case ResponseCodes.AUTH_REDIRECT:
identity = new ClientMetaData();
identity.setMetaData(metaData);
identity.readExternal(in);
server = new ServerMetaData();
server.setMetaData(metaData);
server.readExternal(in);
break;
case ResponseCodes.AUTH_DENIED:
final ThrowableArtifact ta = new ThrowableArtifact();
ta.setMetaData(metaData);
ta.readExternal(in);
deniedCause = ta.getThrowable();
break;
}
}
@Override
public void readExternal(ObjectInput objectInput) throws IOException, ClassNotFoundException {
fLhsPatternExpr = (IExpr) objectInput.readObject();
fPatternCondition = (IExpr) objectInput.readObject();
if (fLhsPatternExpr != null) {
int[] priority = new int[] { IPatternMap.DEFAULT_RULE_PRIORITY };
this.fPatternMap = IPatternMap.determinePatterns(fLhsPatternExpr, priority);
fLHSPriority = priority[0];
}
}
static ChronoZonedDateTime<?> readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
ChronoLocalDateTime<?> dateTime = (ChronoLocalDateTime<?>) in.readObject();
ZoneOffset offset = (ZoneOffset) in.readObject();
ZoneId zone = (ZoneId) in.readObject();
return dateTime.atZone(offset).withZoneSameLocal(zone);
// TODO: ZDT uses ofLenient()
}
/** Read the instance from an input channel.
* @param in input channel
* @exception IOException if the instance cannot be read
*/
@Override
public void readExternal(final ObjectInput in)
throws IOException {
// read the base class
final double t = readBaseExternal(in);
// we can now set the interpolated time and state
setInterpolatedTime(t);
}
/**
* Read this object from a stream of stored objects.
*
* @param in read this.
*
* @exception IOException thrown on error
* @exception ClassNotFoundException thrown on error
*/
public void readExternal( ObjectInput in )
throws IOException, ClassNotFoundException
{
super.readExternal(in);
specificName = (String) in.readObject();
dynamicResultSets = in.readInt();
parameterCount = in.readInt();
parameterStyle = in.readShort();
sqlOptions = in.readShort();
returnType = getStoredType(in.readObject());
calledOnNullInput = in.readBoolean();
// expansionNum is used for adding more fields in the future.
// It is an indicator for whether extra fields exist and need
// to be written
expansionNum = in.readInt();
if (parameterCount != 0) {
parameterNames = new String[parameterCount];
parameterTypes = new TypeDescriptor[parameterCount];
ArrayUtil.readArrayItems(in, parameterNames);
for (int p = 0; p < parameterTypes.length; p++)
{
parameterTypes[p] = getStoredType(in.readObject());
}
parameterModes = ArrayUtil.readIntArray(in);
} else {
parameterNames = null;
parameterTypes = null;
parameterModes = null;
}
if(expansionNum == 1){
language = (String) in.readObject();
compiledPyCode = (byte[]) in.readObject();
}
}
public PolicyUpdatedEvent readObjectVersion1(ObjectInput input) throws IOException, ClassNotFoundException {
PolicyUpdatedEvent res = new PolicyUpdatedEvent();
res.id = MarshallUtil.unmarshallString(input);
res.name = MarshallUtil.unmarshallString(input);
res.resources = KeycloakMarshallUtil.readCollection(input, KeycloakMarshallUtil.STRING_EXT, HashSet::new);
res.resourceTypes = KeycloakMarshallUtil.readCollection(input, KeycloakMarshallUtil.STRING_EXT, HashSet::new);
res.scopes = KeycloakMarshallUtil.readCollection(input, KeycloakMarshallUtil.STRING_EXT, HashSet::new);
res.serverId = MarshallUtil.unmarshallString(input);
return res;
}
/**
* Read {@link JointModel} object from a file.
*/
public static <DI extends ISituatedDataItem<?, ?>, MR, ESTEP> JointModel<DI, MR, ESTEP> readJointModel(
File file) throws ClassNotFoundException, IOException {
LOG.info("Reading joint model from file...");
final long start = System.currentTimeMillis();
try (final ObjectInput input = new ObjectInputStream(
new BufferedInputStream(new FileInputStream(file)))) {
@SuppressWarnings("unchecked")
final JointModel<DI, MR, ESTEP> model = (JointModel<DI, MR, ESTEP>) input
.readObject();
LOG.info("Model loaded. Reading time: %.4f",
(System.currentTimeMillis() - start) / 1000.0);
return model;
}
}