下面列出了怎么用java.io.ObjectOutput的API类实例代码及写法,或者点击链接到github查看源代码。
public static <T> void writeTo(final GOV4Function<T> gov4Function, final String path) throws NoSuchFieldException,
IllegalAccessException, IOException {
final Field dataField = gov4Function.getClass().getDeclaredField("data");
dataField.setAccessible(true);
final AbstractBitVector.LongBigListView signaturesData = (AbstractBitVector.LongBigListView) dataField.get(gov4Function);
final Field bitVectorField = signaturesData.getClass().getDeclaredField("bitVector");
bitVectorField.setAccessible(true);
final BitVector bitVector = (BitVector)bitVectorField.get(signaturesData);
try (final LittleEndianDataOutputStream outputStream = new LittleEndianDataOutputStream(
new BufferedOutputStream(new FileOutputStream(Files.buildPath(path, "signatures.bin"))))) {
for (final long value : bitVector.bits()) {
outputStream.writeLong(value);
}
}
dataField.set(gov4Function, null);
try (final OutputStream outputStream = new FileOutputStream(Files.buildPath(path, "GOV4Function.bin"));
final ObjectOutput objectOutput = new ObjectOutputStream(outputStream)) {
objectOutput.writeObject(gov4Function);
}
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
StackedXYAreaRenderer r1 = new StackedXYAreaRenderer();
r1.setShapePaint(Color.red);
r1.setShapeStroke(new BasicStroke(1.23f));
StackedXYAreaRenderer 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 = (StackedXYAreaRenderer) in.readObject();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
assertEquals(r1, r2);
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
Vector v1 = new Vector(1.0, 2.0);
Vector 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 = (Vector) in.readObject();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
assertEquals(v1, v2);
}
/** {@inheritDoc} */
@Override
public void writeExternal(final ObjectOutput out)
throws IOException {
try {
// save the local attributes
finalizeStep();
} catch (DerivativeException e) {
throw MathRuntimeException.createIOException(e);
}
final int dimension = (currentState == null) ? -1 : currentState.length;
out.writeInt(dimension);
for (int i = 0; i < dimension; ++i) {
out.writeDouble(yDotKLast[0][i]);
out.writeDouble(yDotKLast[1][i]);
out.writeDouble(yDotKLast[2][i]);
}
// save the state of the base class
super.writeExternal(out);
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
ExtendedCategoryAxis a1 = new ExtendedCategoryAxis("Test");
a1.setSubLabelPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
4.0f, Color.blue));
ExtendedCategoryAxis a2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(a1);
out.close();
ObjectInput in = new ObjectInputStream(
new ByteArrayInputStream(buffer.toByteArray()));
a2 = (ExtendedCategoryAxis) in.readObject();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
assertEquals(a1, a2);
}
/**
* 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);
}
/**
* 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);
}
/**
* Returns an output stream (may put out header information
* relating to the success of the call).
* @param success If true, indicates normal return, else indicates
* exceptional return.
* @exception StreamCorruptedException If result stream previously
* acquired
* @exception IOException For any other problem with I/O.
*/
public ObjectOutput getResultStream(boolean success) throws IOException {
/* make sure result code only marshaled once. */
if (resultStarted)
throw new StreamCorruptedException("result already in progress");
else
resultStarted = true;
// write out return header
// return header, part 1 (read by Transport)
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeByte(TransportConstants.Return);// transport op
getOutputStream(true); // creates a MarshalOutputStream
// return header, part 2 (read by client-side RemoteCall)
if (success) //
out.writeByte(TransportConstants.NormalReturn);
else
out.writeByte(TransportConstants.ExceptionalReturn);
out.writeID(); // write id for gcAck
return out;
}
/**
* 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);
}
/**
* 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() {
BubbleXYItemLabelGenerator g1 = new BubbleXYItemLabelGenerator();
BubbleXYItemLabelGenerator 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 = (BubbleXYItemLabelGenerator) in.readObject();
in.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
assertEquals(g1, g2);
}
/**
* {@inheritDoc}
*/
@Override
public void writeExternal( ObjectOutput out ) throws IOException
{
out.writeUTF( oid );
out.writeBoolean( criticality );
if ( hasValue() )
{
out.writeBoolean( true );
out.writeInt( value.length );
if ( value.length > 0 )
{
out.write( value );
}
}
else
{
out.writeBoolean( false );
}
out.flush();
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
CategoryPointerAnnotation a1 = new CategoryPointerAnnotation("Label",
"A", 20.0, Math.PI);
CategoryPointerAnnotation a2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(a1);
out.close();
ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(
buffer.toByteArray()));
a2 = (CategoryPointerAnnotation) in.readObject();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
assertEquals(a1, a2);
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
DateTick t1 = new DateTick(new Date(0L), "Label", TextAnchor.CENTER,
TextAnchor.CENTER, 10.0);
DateTick t2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(t1);
out.close();
ObjectInput in = new ObjectInputStream(
new ByteArrayInputStream(buffer.toByteArray()));
t2 = (DateTick) in.readObject();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
assertEquals(t1, t2);
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
out.writeBoolean(characterDelimiter!=null);
if (characterDelimiter!=null)
out.writeUTF(characterDelimiter);
out.writeBoolean(columnDelimiter!=null);
if (columnDelimiter!=null)
out.writeUTF(columnDelimiter);
writeNullableUTF(out, timeFormat);
writeNullableUTF(out, dateFormat);
writeNullableUTF(out,timestampFormat);
try {
ArrayUtil.writeIntArray(out, WriteReadUtils.getExecRowTypeFormatIds(execRow));
} catch (StandardException se) {
throw new IOException(se);
}
out.writeBoolean(columnIndex!=null);
if (columnIndex!=null)
ArrayUtil.writeIntArray(out,columnIndex);
}
/**
* Write this object to a stream of stored objects.
*
* @param out write bytes here.
*
* @exception IOException thrown on error
*/
public void writeExternal( ObjectOutput out )
throws IOException
{
FormatableHashtable fh = new FormatableHashtable();
fh.put("name", name);
fh.put("dataType", dataType);
fh.put("defaultValue", defaultValue);
fh.put("defaultInfo", defaultInfo);
fh.put("newDefaultUUID", newDefaultUUID);
fh.put("oldDefaultUUID", oldDefaultUUID );
fh.putInt("action", action);
if (autoincInc != 0)
{
// only write out autoinc values if its an autoinc column.
fh.putLong("autoincStart", autoincStart);
fh.putLong("autoincInc", autoincInc);
}
out.writeObject(fh);
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
PieLabelRecord p1 = new PieLabelRecord("A", 1.0, 2.0, new TextBox("B"),
3.0, 4.0, 5.0);
PieLabelRecord p2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(p1);
out.close();
ObjectInput in = new ObjectInputStream(
new ByteArrayInputStream(buffer.toByteArray()));
p2 = (PieLabelRecord) in.readObject();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
boolean b = p1.equals(p2);
assertTrue(b);
}
/** {@inheritDoc} */
@Override
public void writeExternal(final ObjectOutput out)
throws IOException {
// save the state of the base class
writeBaseExternal(out);
// save the local attributes
final int n = (currentState == null) ? -1 : currentState.length;
final int kMax = (yDotK == null) ? -1 : yDotK.length;
out.writeInt(kMax);
for (int k = 0; k < kMax; ++k) {
for (int i = 0; i < n; ++i) {
out.writeDouble(yDotK[k][i]);
}
}
// we do not save any reference to the equations
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
Stroke stroke1 = new BasicStroke(2.0f);
XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {1.0,
2.0, 3.0, 4.0, 5.0, 6.0}, stroke1, Color.red, Color.blue);
XYPolygonAnnotation a2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(a1);
out.close();
ObjectInput in = new ObjectInputStream(
new ByteArrayInputStream(buffer.toByteArray())
);
a2 = (XYPolygonAnnotation) in.readObject();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
assertEquals(a1, a2);
}
/**
* 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);
}
/** {@inheritDoc} */
@Override
public void writeExternal(final ObjectOutput out)
throws IOException {
try {
// save the local attributes
finalizeStep();
} catch (DerivativeException e) {
throw MathRuntimeException.createIOException(e);
}
final int dimension = (currentState == null) ? -1 : currentState.length;
out.writeInt(dimension);
for (int i = 0; i < dimension; ++i) {
out.writeDouble(yDotKLast[0][i]);
out.writeDouble(yDotKLast[1][i]);
out.writeDouble(yDotKLast[2][i]);
}
// save the state of the base class
super.writeExternal(out);
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(CLASS_VERSION);
out.writeInt(userId);
out.writeInt(applicationId);
out.writeBoolean(enabled);
out.writeInt(version);
}
/**
* Serializes this <code>DataFlavor</code>.
*/
public synchronized void writeExternal(ObjectOutput os) throws IOException {
if (mimeType != null) {
mimeType.setParameter("humanPresentableName", humanPresentableName);
os.writeObject(mimeType);
mimeType.removeParameter("humanPresentableName");
} else {
os.writeObject(null);
}
os.writeObject(representationClass);
}
/**
* {@inheritDoc}
*/
public void writeExternal(ObjectOutput out) throws IOException {
// VERSION
out.writeByte(0);
// SUPER
super.writeExternal(out);
// NO_ENTRY_KEY
out.writeInt(no_entry_key);
// NO_ENTRY_VALUE
out.writeDouble(no_entry_value);
}
@Override
public void writeExternal(ObjectOutput out) throws IOException{
super.writeExternal(out);
out.writeInt(sender);
out.writeInt(cid);
out.writeInt(type);
out.writeInt(regency);
out.writeInt(leader);
out.writeObject(state);
out.writeObject(view);
}
private void serializeSyncResultHeader(ObjectOutput out, int pageCountEstimate) throws IOException
{
//header:
out.writeLong(this.finder.getMithraObjectPortal().getLatestRefreshTime());
out.writeLong(getPageSize());
out.writeInt(pageCountEstimate);
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeBoolean(this.buffer != null);
if (this.buffer != null) {
out.writeInt(this.buffer.capacity());
out.writeInt(this.buffer.limit());
out.writeInt(this.buffer.position());
for (int i=0; i < this.buffer.capacity(); i++) {
out.write(this.buffer.get(i));
}
}
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(myVersion);
out.writeObject(myValue);
out.writeObject(extraObject);
out.writeObject(modVal);
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(bucketId.intValue());
out.writeUTF(primaryBucketServersStr);
if (this.secondaryBucketServersStr == null) {
out.writeShort(-1); // for null
}
else {
out.writeShort(this.secondaryBucketServersStr.length);
for (int i = 0; i < this.secondaryBucketServersStr.length; i++) {
out.writeUTF(this.secondaryBucketServersStr[i]);
}
}
}
@Override
public void writeExternal (ObjectOutput out)
throws IOException {
out.writeBoolean (bComp.isStatusLineVisible ());
out.writeBoolean (bComp.isToolbarVisible ());
out.writeObject (bComp.getDocumentURL ());
}