java.io.ObjectOutputStream.writeObject()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(214)

本文整理了Java中java.io.ObjectOutputStream.writeObject()方法的一些代码示例,展示了ObjectOutputStream.writeObject()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ObjectOutputStream.writeObject()方法的具体详情如下:
包路径:java.io.ObjectOutputStream
类名称:ObjectOutputStream
方法名:writeObject

ObjectOutputStream.writeObject介绍

[英]Writes an object to the target stream.
[中]将对象写入目标流。

代码示例

代码示例来源:origin: google/guava

/**
 * The serial form currently mimics Android's java.util.HashMap version, e.g. see
 * http://omapzoom.org/?p=platform/libcore.git;a=blob;f=luni/src/main/java/java/util/HashMap.java
 */
private void writeObject(ObjectOutputStream stream) throws IOException {
 stream.defaultWriteObject();
 stream.writeInt(size);
 for (int i = 0; i < size; i++) {
  stream.writeObject(keys[i]);
  stream.writeObject(values[i]);
 }
}

代码示例来源:origin: google/guava

/** Serializes and deserializes the specified object. */
@SuppressWarnings("unchecked")
static <T> T reserialize(T object) {
 checkNotNull(object);
 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
 try {
  ObjectOutputStream out = new ObjectOutputStream(bytes);
  out.writeObject(object);
  ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
  return (T) in.readObject();
 } catch (IOException | ClassNotFoundException e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: stackoverflow.com

FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(this);
os.close();
fos.close();

代码示例来源:origin: apache/storm

public static byte[] serializeKerberosTicket(KerberosTicket tgt) throws Exception {
  ByteArrayOutputStream bao = new ByteArrayOutputStream();
  ObjectOutputStream out = new ObjectOutputStream(bao);
  out.writeObject(tgt);
  out.flush();
  out.close();
  return bao.toByteArray();
}

代码示例来源:origin: spring-projects/spring-framework

public static void testSerialization(Object o) throws IOException {
  OutputStream baos = new ByteArrayOutputStream();
  ObjectOutputStream oos = new ObjectOutputStream(baos);
  oos.writeObject(o);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Writes the source object to an output stream using Java serialization.
 * The source object must implement {@link Serializable}.
 * @see ObjectOutputStream#writeObject(Object)
 */
@Override
public void serialize(Object object, OutputStream outputStream) throws IOException {
  if (!(object instanceof Serializable)) {
    throw new IllegalArgumentException(getClass().getSimpleName() + " requires a Serializable payload " +
        "but received an object of type [" + object.getClass().getName() + "]");
  }
  ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
  objectOutputStream.writeObject(object);
  objectOutputStream.flush();
}

代码示例来源:origin: google/guava

@SuppressWarnings("unchecked")
static <T> T reserialize(T object) {
 try {
  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  ObjectOutputStream out = new ObjectOutputStream(bytes);
  out.writeObject(object);
  ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
  return (T) in.readObject();
 } catch (IOException | ClassNotFoundException e) {
  Helpers.fail(e, e.getMessage());
 }
 throw new AssertionError("not reachable");
}

代码示例来源:origin: apache/incubator-shardingsphere

@SneakyThrows
private InputStream getInputStream(final Object value) {
  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
  objectOutputStream.writeObject(value);
  objectOutputStream.flush();
  objectOutputStream.close();
  return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
}

代码示例来源:origin: google/guava

/** @serialData the ConcurrentMap of elements and their counts. */
private void writeObject(ObjectOutputStream stream) throws IOException {
 stream.defaultWriteObject();
 stream.writeObject(countMap);
}

代码示例来源:origin: junit-team/junit4

private void save() throws IOException {
  ObjectOutputStream stream = null;
  try {
    stream = new ObjectOutputStream(new FileOutputStream(fHistoryStore));
    stream.writeObject(this);
  } finally {
    if (stream != null) {
      stream.close();
    }
  }
}

代码示例来源:origin: stackoverflow.com

public static byte[] serialize(Object obj) throws IOException {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  ObjectOutputStream os = new ObjectOutputStream(out);
  os.writeObject(obj);
  return out.toByteArray();
}
public static Object deserialize(byte[] data) throws IOException, ClassNotFoundException {
  ByteArrayInputStream in = new ByteArrayInputStream(data);
  ObjectInputStream is = new ObjectInputStream(in);
  return is.readObject();
}

代码示例来源:origin: apache/storm

public static byte[] javaSerialize(Object obj) {
  try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(obj);
    oos.close();
    return bos.toByteArray();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: google/guava

/**
 * The serial form currently mimics Android's java.util.HashSet version, e.g. see
 * http://omapzoom.org/?p=platform/libcore.git;a=blob;f=luni/src/main/java/java/util/HashSet.java
 */
private void writeObject(ObjectOutputStream stream) throws IOException {
 stream.defaultWriteObject();
 stream.writeInt(size);
 for (E e : this) {
  stream.writeObject(e);
 }
}

代码示例来源:origin: gocd/gocd

public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException {
  ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  ObjectOutputStream out = new ObjectOutputStream(buffer);
  out.writeObject(o);
  ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  return in.readObject();
}

代码示例来源:origin: apache/incubator-shardingsphere

@SneakyThrows
protected InputStream getInputStream(final Object value) {
  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
  objectOutputStream.writeObject(value);
  objectOutputStream.flush();
  objectOutputStream.close();
  return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
}

代码示例来源:origin: prestodb/presto

/**
 * The serial form currently mimics Android's java.util.HashMap version, e.g. see
 * http://omapzoom.org/?p=platform/libcore.git;a=blob;f=luni/src/main/java/java/util/HashMap.java
 */
private void writeObject(ObjectOutputStream stream) throws IOException {
 stream.defaultWriteObject();
 stream.writeInt(size);
 for (int i = 0; i < size; i++) {
  stream.writeObject(keys[i]);
  stream.writeObject(values[i]);
 }
}

代码示例来源:origin: TeamNewPipe/NewPipe

@NonNull
private <T extends Serializable> T clone(@NonNull T item,
                     @NonNull final Class<T> type) throws Exception {
  final ByteArrayOutputStream bytesOutput = new ByteArrayOutputStream();
  try (final ObjectOutputStream objectOutput = new ObjectOutputStream(bytesOutput)) {
    objectOutput.writeObject(item);
    objectOutput.flush();
  }
  final Object clone = new ObjectInputStream(
      new ByteArrayInputStream(bytesOutput.toByteArray())).readObject();
  return type.cast(clone);
}

代码示例来源:origin: apache/storm

@Override
public byte[] serialize(Object object) {
  try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gos = new GZIPOutputStream(bos);
    ObjectOutputStream oos = new ObjectOutputStream(gos);
    oos.writeObject(object);
    oos.close();
    return bos.toByteArray();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: prestodb/presto

/** @serialData the ConcurrentMap of elements and their counts. */
private void writeObject(ObjectOutputStream stream) throws IOException {
 stream.defaultWriteObject();
 stream.writeObject(countMap);
}

代码示例来源:origin: spring-projects/spring-framework

public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  ObjectOutputStream oos = new ObjectOutputStream(baos);
  oos.writeObject(o);
  oos.flush();
  baos.flush();
  byte[] bytes = baos.toByteArray();
  ByteArrayInputStream is = new ByteArrayInputStream(bytes);
  ObjectInputStream ois = new ObjectInputStream(is);
  Object o2 = ois.readObject();
  return o2;
}

相关文章

ObjectOutputStream类方法