Java Code Examples for java.io.ObjectOutputStream
The following examples show how to use
java.io.ObjectOutputStream.
These examples are extracted from open source projects.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: dragonwell8_jdk Author: alibaba File: ObjArrays.java License: GNU General Public License v2.0 | 6 votes |
/** * Run benchmark for given number of batches, with given number of cycles * for each batch. */ void doReps(ObjectOutputStream oout, ObjectInputStream oin, StreamBuffer sbuf, Node[][] arrays, int nbatches) throws Exception { int ncycles = arrays.length; for (int i = 0; i < nbatches; i++) { sbuf.reset(); oout.reset(); for (int j = 0; j < ncycles; j++) { oout.writeObject(arrays[j]); } oout.flush(); for (int j = 0; j < ncycles; j++) { oin.readObject(); } } }
Example #2
Source Project: openjdk-jdk9 Author: AdoptOpenJDK File: ModelMBeanInfoSupport.java License: GNU General Public License v2.0 | 6 votes |
/** * Serializes a {@link ModelMBeanInfoSupport} to an {@link ObjectOutputStream}. */ private void writeObject(ObjectOutputStream out) throws IOException { if (compat) { // Serializes this instance in the old serial form // ObjectOutputStream.PutField fields = out.putFields(); fields.put("modelMBeanDescriptor", modelMBeanDescriptor); fields.put("mmbAttributes", modelMBeanAttributes); fields.put("mmbConstructors", modelMBeanConstructors); fields.put("mmbNotifications", modelMBeanNotifications); fields.put("mmbOperations", modelMBeanOperations); fields.put("currClass", currClass); out.writeFields(); } else { // Serializes this instance in the new serial form // out.defaultWriteObject(); } }
Example #3
Source Project: openjdk-8 Author: bpupadhyaya File: NumericValueExp.java License: GNU General Public License v2.0 | 6 votes |
/** * Serializes a {@link NumericValueExp} to an {@link ObjectOutputStream}. */ private void writeObject(ObjectOutputStream out) throws IOException { if (compat) { // Serializes this instance in the old serial form // ObjectOutputStream.PutField fields = out.putFields(); fields.put("doubleVal", doubleValue()); fields.put("longVal", longValue()); fields.put("valIsLong", isLong()); out.writeFields(); } else { // Serializes this instance in the new serial form // out.defaultWriteObject(); } }
Example #4
Source Project: frog Author: drinkjava2 File: EggTool.java License: Apache License 2.0 | 6 votes |
/** * Frogs which have higher energy lay eggs * * 利用Java串行机制存盘。 能量多(也就是吃的更多)的Frog下蛋并存盘, 以进行下一轮测试,能量少的Frog被淘汰,没有下蛋的资格。 * 用能量的多少来简化生存竟争模拟,每次下蛋数量固定为EGG_QTY个 */ public static void layEggs() { sortFrogsOrderByEnergyDesc(); Frog first = Env.frogs.get(0); Frog last = Env.frogs.get(Env.frogs.size() - 1); try { Env.eggs.clear(); for (int i = 0; i < Env.EGG_QTY; i++) Env.eggs.add(new Egg(Env.frogs.get(i))); FileOutputStream fo = new FileOutputStream(Application.CLASSPATH + "eggs.ser"); ObjectOutputStream so = new ObjectOutputStream(fo); so.writeObject(Env.eggs); so.close(); System.out.print("\r1st frog has " + first.organs.size() + " organs, energy=" + first.energy ); System.out.println(", Last frog has " + last.organs.size() + " organs, energy=" + last.energy); System.out.println("Saved "+Env.eggs.size() +" eggs to file '" + Application.CLASSPATH + "eggs.ser'"); } catch (IOException e) { System.out.println(e); } }
Example #5
Source Project: openjdk-jdk9 Author: AdoptOpenJDK File: ModelMBeanOperationInfo.java License: GNU General Public License v2.0 | 6 votes |
/** * Serializes a {@link ModelMBeanOperationInfo} to an {@link ObjectOutputStream}. */ private void writeObject(ObjectOutputStream out) throws IOException { if (compat) { // Serializes this instance in the old serial form // ObjectOutputStream.PutField fields = out.putFields(); fields.put("operationDescriptor", operationDescriptor); fields.put("currClass", currClass); out.writeFields(); } else { // Serializes this instance in the new serial form // out.defaultWriteObject(); } }
Example #6
Source Project: Flink-CEPplus Author: ljygz File: MemoryCheckpointStorageTest.java License: Apache License 2.0 | 6 votes |
@Test public void testTaskOwnedStateStream() throws Exception { final List<String> state = Arrays.asList("Flopsy", "Mopsy", "Cotton Tail", "Peter"); final MemoryBackendCheckpointStorage storage = new MemoryBackendCheckpointStorage( new JobID(), null, null, DEFAULT_MAX_STATE_SIZE); StreamStateHandle stateHandle; try (CheckpointStateOutputStream stream = storage.createTaskOwnedStateStream()) { assertTrue(stream instanceof MemoryCheckpointOutputStream); new ObjectOutputStream(stream).writeObject(state); stateHandle = stream.closeAndGetHandle(); } try (ObjectInputStream in = new ObjectInputStream(stateHandle.openInputStream())) { assertEquals(state, in.readObject()); } }
Example #7
Source Project: netbeans Author: apache File: EventBufferDumpedCommand.java License: Apache License 2.0 | 6 votes |
void writeObject(ObjectOutputStream out) throws IOException { out.writeInt(bufSize); out.writeBoolean(buffer != null); if (buffer != null) { Deflater compressor = new Deflater(); // for small buffers, the compressed size can be somewhat larger than the original byte[] compressedBytes = new byte[bufSize + 32]; int compressedSize; compressor.setInput(buffer,startPos,bufSize); compressor.finish(); compressedSize = compressor.deflate(compressedBytes); out.writeInt(compressedSize); out.write(compressedBytes,0,compressedSize); } else { out.writeUTF(eventBufferFileName); } }
Example #8
Source Project: j2objc Author: google File: DateFormatSymbolsTest.java License: Apache License 2.0 | 6 votes |
public void test_serialization() throws Exception { DateFormatSymbols symbols = new DateFormatSymbols(Locale.FRANCE); String[][] zoneStrings = symbols.getZoneStrings(); assertNotNull(zoneStrings); // serialize ByteArrayOutputStream byteOStream = new ByteArrayOutputStream(); ObjectOutputStream objectOStream = new ObjectOutputStream(byteOStream); objectOStream.writeObject(symbols); // and deserialize ObjectInputStream objectIStream = new ObjectInputStream( new ByteArrayInputStream(byteOStream.toByteArray())); DateFormatSymbols symbolsD = (DateFormatSymbols) objectIStream .readObject(); String[][] zoneStringsD = symbolsD.getZoneStrings(); assertNotNull(zoneStringsD); assertEquals(symbols, symbolsD); }
Example #9
Source Project: dragonwell8_jdk Author: alibaba File: Permissions.java License: GNU General Public License v2.0 | 6 votes |
/** * @serialData Default fields. */ /* * Writes the contents of the permsMap field out as a Hashtable for * serialization compatibility with earlier releases. */ private void writeObject(ObjectOutputStream out) throws IOException { // Don't call out.defaultWriteObject() // Copy perms into a Hashtable Hashtable<Permission, Permission> perms = new Hashtable<>(permsMap.size()*2); synchronized (this) { perms.putAll(permsMap); } // Write out serializable fields ObjectOutputStream.PutField pfields = out.putFields(); pfields.put("perms", perms); out.writeFields(); }
Example #10
Source Project: openjdk-jdk8u-backup Author: AdoptOpenJDK File: ClassDesc.java License: GNU General Public License v2.0 | 6 votes |
/** * Write and read class descriptors to/from a stream. * Arguments: <# cycles> */ public long run(String[] args) throws Exception { int ncycles = Integer.parseInt(args[0]); StreamBuffer sbuf = new StreamBuffer(); ObjectOutputStream oout = new ObjectOutputStream(sbuf.getOutputStream()); ObjectInputStream oin = new ObjectInputStream(sbuf.getInputStream()); ObjectStreamClass desc = ObjectStreamClass.lookup(Dummy50.class); doReps(oout, oin, sbuf, desc, 1); // warmup long start = System.currentTimeMillis(); doReps(oout, oin, sbuf, desc, ncycles); return System.currentTimeMillis() - start; }
Example #11
Source Project: datawave Author: NationalSecurityAgency File: EdgeQueryLogic.java License: Apache License 2.0 | 6 votes |
protected String serializePrefilter() { String retVal = null; if (prefilterValues != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(prefilterValues); oos.close(); } catch (IOException ex) { log.error("Some error encoding the prefilters...", ex); } retVal = new String(Base64.encodeBase64(baos.toByteArray())); } return retVal; }
Example #12
Source Project: openjdk-8-source Author: keerath File: FloatArrays.java License: GNU General Public License v2.0 | 6 votes |
/** * Run benchmark for given number of batches, with given number of cycles * for each batch. */ void doReps(ObjectOutputStream oout, ObjectInputStream oin, StreamBuffer sbuf, float[][] arrays, int nbatches) throws Exception { int ncycles = arrays.length; for (int i = 0; i < nbatches; i++) { sbuf.reset(); oout.reset(); for (int j = 0; j < ncycles; j++) { oout.writeObject(arrays[j]); } oout.flush(); for (int j = 0; j < ncycles; j++) { oin.readObject(); } } }
Example #13
Source Project: ThriftBook Author: RandyAbernethy File: FileTrans.java License: Apache License 2.0 | 6 votes |
public static void main(String[] args) throws IOException, TTransportException, ClassNotFoundException { Trade trade = new Trade(); trade.symbol = "F"; trade.price = 13.10; trade.size = 2500; TSimpleFileTransport trans_out = new TSimpleFileTransport("data",false,true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(trade); trans_out.write(baos.toByteArray()); trans_out.close(); TSimpleFileTransport trans_in = new TSimpleFileTransport("data",true,false); byte[] buf = new byte[128]; int iBytesRead = trans_in.read(buf, 0, buf.length); ByteArrayInputStream bais = new ByteArrayInputStream(buf); ObjectInputStream ois = new ObjectInputStream(bais); trade = (Trade) ois.readObject(); System.out.println("Trade(" + iBytesRead + "): " + trade.symbol + " " + trade.size + " @ " + trade.price); }
Example #14
Source Project: astor Author: SpoonLabs File: StackedXYAreaRendererTests.java License: GNU General Public License v2.0 | 6 votes |
/** * 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); }
Example #15
Source Project: astor Author: SpoonLabs File: StrokeMapTests.java License: GNU General Public License v2.0 | 6 votes |
/** * A check for serialization. */ public void testSerialization2() { StrokeMap m1 = new StrokeMap(); m1.put("K1", new BasicStroke(1.1f)); m1.put("K2", new BasicStroke(2.2f)); StrokeMap m2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(m1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream( buffer.toByteArray())); m2 = (StrokeMap) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(m1, m2); }
Example #16
Source Project: astor Author: SpoonLabs File: MeanAndStandardDeviationTests.java License: GNU General Public License v2.0 | 6 votes |
/** * Serialize an instance, restore it, and check for equality. */ public void testSerialization() { MeanAndStandardDeviation m1 = new MeanAndStandardDeviation(1.2, 3.4); MeanAndStandardDeviation m2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(m1); out.close(); ObjectInput in = new ObjectInputStream( new ByteArrayInputStream(buffer.toByteArray()) ); m2 = (MeanAndStandardDeviation) in.readObject(); in.close(); } catch (Exception e) { System.out.println(e.toString()); } assertEquals(m1, m2); }
Example #17
Source Project: hottub Author: dsrg-uoft File: DocumentImpl.java License: GNU General Public License v2.0 | 6 votes |
/** * @serialData Serialized fields. Convert Maps to Hashtables and Lists * to Vectors for backward compatibility. */ private void writeObject(ObjectOutputStream out) throws IOException { // Convert Maps to Hashtables, Lists to Vectors Vector<NodeIterator> it = (iterators == null)? null : new Vector<>(iterators); Vector<Range> r = (ranges == null)? null : new Vector<>(ranges); Hashtable<NodeImpl, Vector<LEntry>> el = null; if (eventListeners != null) { el = new Hashtable<>(); for (Map.Entry<NodeImpl, List<LEntry>> e : eventListeners.entrySet()) { el.put(e.getKey(), new Vector<>(e.getValue())); } } // Write serialized fields ObjectOutputStream.PutField pf = out.putFields(); pf.put("iterators", it); pf.put("ranges", r); pf.put("eventListeners", el); pf.put("mutationEvents", mutationEvents); out.writeFields(); }
Example #18
Source Project: openjdk-jdk8u Author: AdoptOpenJDK File: SmallObjTrees.java License: GNU General Public License v2.0 | 6 votes |
/** * Run benchmark for given number of batches, with each batch containing the * given number of cycles. */ void doReps(ObjectOutputStream oout, ObjectInputStream oin, StreamBuffer sbuf, Node[] trees, int nbatches) throws Exception { int ncycles = trees.length; for (int i = 0; i < nbatches; i++) { sbuf.reset(); oout.reset(); for (int j = 0; j < ncycles; j++) { oout.writeObject(trees[j]); } oout.flush(); for (int j = 0; j < ncycles; j++) { oin.readObject(); } } }
Example #19
Source Project: ratis Author: hortonworks File: ArithmeticStateMachine.java License: Apache License 2.0 | 6 votes |
@Override public long takeSnapshot() { final Map<String, Double> copy; final TermIndex last; try(final AutoCloseableLock readLock = readLock()) { copy = new HashMap<>(variables); last = getLastAppliedTermIndex(); } final File snapshotFile = storage.getSnapshotFile(last.getTerm(), last.getIndex()); LOG.info("Taking a snapshot to file {}", snapshotFile); try(final ObjectOutputStream out = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream(snapshotFile)))) { out.writeObject(copy); } catch(IOException ioe) { LOG.warn("Failed to write snapshot file \"" + snapshotFile + "\", last applied index=" + last); } return last.getIndex(); }
Example #20
Source Project: openjdk-8 Author: bpupadhyaya File: Ints.java License: GNU General Public License v2.0 | 6 votes |
/** * Write and read int values to/from a stream. The benchmark is run in * batches: each "batch" consists of a fixed number of read/write cycles, * and the stream is flushed (and underlying stream buffer cleared) in * between each batch. * Arguments: <# batches> <# cycles per batch> */ public long run(String[] args) throws Exception { int nbatches = Integer.parseInt(args[0]); int ncycles = Integer.parseInt(args[1]); StreamBuffer sbuf = new StreamBuffer(); ObjectOutputStream oout = new ObjectOutputStream(sbuf.getOutputStream()); ObjectInputStream oin = new ObjectInputStream(sbuf.getInputStream()); doReps(oout, oin, sbuf, 1, ncycles); // warmup long start = System.currentTimeMillis(); doReps(oout, oin, sbuf, nbatches, ncycles); return System.currentTimeMillis() - start; }
Example #21
Source Project: jdk8u_jdk Author: JetBrains File: ShortArrays.java License: GNU General Public License v2.0 | 6 votes |
/** * Write and read short arrays to/from a stream. The benchmark is run in * batches, with each batch consisting of a fixed number of read/write * cycles. The ObjectOutputStream is reset after each batch of cycles has * completed. * Arguments: <array size> <# batches> <# cycles per batch> */ public long run(String[] args) throws Exception { int size = Integer.parseInt(args[0]); int nbatches = Integer.parseInt(args[1]); int ncycles = Integer.parseInt(args[2]); short[][] arrays = new short[ncycles][size]; StreamBuffer sbuf = new StreamBuffer(); ObjectOutputStream oout = new ObjectOutputStream(sbuf.getOutputStream()); ObjectInputStream oin = new ObjectInputStream(sbuf.getInputStream()); doReps(oout, oin, sbuf, arrays, 1); // warmup long start = System.currentTimeMillis(); doReps(oout, oin, sbuf, arrays, nbatches); return System.currentTimeMillis() - start; }
Example #22
Source Project: openjdk-8-source Author: keerath File: SerializationTest.java License: GNU General Public License v2.0 | 6 votes |
public static void test(Object a) { try { File objectbin = new File("object.bin"); FileOutputStream fos = new FileOutputStream(objectbin); ObjectOutputStream out = new ObjectOutputStream(fos); out.writeObject(a); fos.close(); FileInputStream fis = new FileInputStream(objectbin); ObjectInputStream in = new ObjectInputStream(fis); Object o = in.readObject(); fis.close(); System.err.println(o); } catch (Throwable ex) { ex.printStackTrace(); failed = true; } }
Example #23
Source Project: joshua Author: apache File: BloomFilterLanguageModel.java License: Apache License 2.0 | 6 votes |
/** * Builds a language model and stores it in a file. * * @param argv command-line arguments */ public static void main(String[] argv) { if (argv.length < 5) { String msg = "usage: BloomFilterLanguageModel <statistics file> <order> <size>" + " <quantization base> <output file>"; System.err.println(msg); LOG.error(msg); return; } int order = Integer.parseInt(argv[1]); int size = (int) (Integer.parseInt(argv[2]) * Math.pow(2, 23)); double base = Double.parseDouble(argv[3]); try { BloomFilterLanguageModel lm = new BloomFilterLanguageModel(argv[0], order, size, base); ObjectOutputStream out = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(argv[4]))); lm.writeExternal(out); out.close(); //TODO: try-with-resources } catch (IOException e) { LOG.error(e.getMessage(), e); } }
Example #24
Source Project: astor Author: SpoonLabs File: XYCoordinateTests.java License: GNU General Public License v2.0 | 6 votes |
/** * 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); }
Example #25
Source Project: astor Author: SpoonLabs File: SymbolicXYItemLabelGeneratorTests.java License: GNU General Public License v2.0 | 6 votes |
/** * 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); }
Example #26
Source Project: jdk8u_jdk Author: JetBrains File: JScrollPane.java License: GNU General Public License v2.0 | 5 votes |
/** * See <code>readObject</code> and <code>writeObject</code> in * <code>JComponent</code> for more * information about serialization in Swing. */ private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); if (getUIClassID().equals(uiClassID)) { byte count = JComponent.getWriteObjCounter(this); JComponent.setWriteObjCounter(this, --count); if (count == 0 && ui != null) { ui.installUI(this); } } }
Example #27
Source Project: dremio-oss Author: dremio File: IcebergSerDe.java License: Apache License 2.0 | 5 votes |
private static byte[] serializeToByteArray(Object object) throws IOException { try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos)) { out.writeObject(object); return bos.toByteArray(); } }
Example #28
Source Project: letv Author: JackChan1999 File: ax.java License: Apache License 2.0 | 5 votes |
private void a(ObjectOutputStream objectOutputStream) throws IOException { try { b(new cs(new dk((OutputStream) objectOutputStream))); } catch (cf e) { throw new IOException(e.getMessage()); } }
Example #29
Source Project: dragonwell8_jdk Author: alibaba File: Cons.java License: GNU General Public License v2.0 | 5 votes |
/** * Run benchmark for given number of cycles. */ void doReps(StreamBuffer sbuf, Dummy dummy, int reps) throws Exception { OutputStream out = sbuf.getOutputStream(); InputStream in = sbuf.getInputStream(); for (int i = 0; i < reps; i++) { sbuf.reset(); ObjectOutputStream oout = new ObjectOutputStream(out); oout.writeObject(dummy); oout.flush(); ObjectInputStream oin = new ObjectInputStream(in); oin.readObject(); } }
Example #30
Source Project: camunda-bpm-platform Author: camunda File: JavaSerializationTest.java License: Apache License 2.0 | 5 votes |
@Test @Deployment public void testJavaObjectNotDeserializedIfNotRequested() throws Exception { // this test makes sure that if a serialized value is set, it is not automatically deserialized if deserialization is not requested // given // a serialized Java Object FailingJavaSerializable javaSerializable = new FailingJavaSerializable("foo"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); new ObjectOutputStream(baos).writeObject(javaSerializable); byte[] serializedObjectBytes = baos.toByteArray(); String serializedObject = StringUtil.fromBytes(Base64.encodeBase64(serializedObjectBytes), engineRule.getProcessEngine()); thrown.expect(RuntimeException.class); thrown.expectMessage("Exception while deserializing object"); // which cannot be deserialized ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(serializedObjectBytes)); objectInputStream.readObject(); // if // I start a process instance in which a Java Delegate reads the value in its serialized form runtimeService.startProcessInstanceByKey("oneTaskProcess", Variables.createVariables() .putValue("varName", serializedObjectValue(serializedObject) .serializationDataFormat(JAVA_DATA_FORMAT) .objectTypeName(JavaSerializable.class.getName()) .create())); // then // it does not fail }