本文整理了Java中java.io.ObjectOutputStream
类的一些代码示例,展示了ObjectOutputStream
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ObjectOutputStream
类的具体详情如下:
包路径:java.io.ObjectOutputStream
类名称:ObjectOutputStream
[英]A specialized OutputStream that is able to write (serialize) Java objects as well as primitive data types (int, byte, char etc.). The data can later be loaded using an ObjectInputStream.
[中]一种专门的输出流,能够写入(序列化)Java对象以及基本数据类型(int、byte、char等)。稍后可以使用ObjectInputStream加载数据。
代码示例来源:origin: google/guava
/** Serializes and deserializes the specified object. */
@SuppressWarnings("unchecked")
static
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: 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: 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
/**
* 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
stream.writeObject(values[i]);
}
}
代码示例来源:origin: google/guava
/** @serialData the ConcurrentMap of elements and their counts. */
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
stream.writeObject(countMap);
}
代码示例来源:origin: apache/incubator-dubbo
@Override
public void writeBytes(byte[] v, int off, int len) throws IOException {
if (v == null) {
outputStream.writeInt(-1);
} else {
outputStream.writeInt(len);
outputStream.write(v, off, len);
}
}
代码示例来源:origin: spotbugs/spotbugs
private static Bug1234 writeRead(final Bug1234 original) {
try {
final ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(fileName));
outputStream.writeObject(original);
outputStream.close();
final ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(fileName));
final Bug1234 result = (Bug1234)inputStream.readObject();
inputStream.close();
return result;
} catch (final Throwable e) {
throw new RuntimeException("Error: " + e);
}
}
代码示例来源:origin: org.apache.commons/commons-lang3
@Test
public void testDeserializeStreamOfNull() throws Exception {
final ByteArrayOutputStream streamReal = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(streamReal);
oos.writeObject(null);
oos.flush();
oos.close();
final ByteArrayInputStream inTest = new ByteArrayInputStream(streamReal.toByteArray());
final Object test = SerializationUtils.deserialize(inTest);
assertNull(test);
}
代码示例来源:origin: ethereum/ethereumj
if (fileCacheEnabled && file.canRead()) {
fireDatatasetStatusUpdate(FULL_DATASET_LOAD_START);
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
logger.info("Loading dataset from " + file.getAbsolutePath());
long bNum = ois.readLong();
if (bNum == blockNumber) {
fullData = (int[]) ois.readObject();
logger.info("Dataset loaded.");
fireDatatasetStatusUpdate(FULL_DATASET_LOADED);
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
logger.info("Writing dataset to " + file.getAbsolutePath());
oos.writeLong(blockNumber);
oos.writeObject(fullData);
} catch (IOException e) {
throw new RuntimeException(e);
代码示例来源:origin: commons-io/commons-io
@org.junit.Test
public void testPrimitiveLong() throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
final long input = 12345L;
oos.writeLong(input);
oos.close();
final InputStream bais = new ByteArrayInputStream(baos.toByteArray());
final ClassLoaderObjectInputStream clois =
new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais);
final long result = clois.readLong();
assertEquals(input, result);
clois.close();
}
代码示例来源:origin: redisson/redisson
Socket sock = new Socket(servername, port);
OutputStream out = sock.getOutputStream();
out.write(lookupCommand);
out.write(endofline);
out.write(endofline);
ObjectOutputStream dout = new ObjectOutputStream(out);
dout.writeUTF(name);
dout.flush();
InputStream in = new BufferedInputStream(sock.getInputStream());
skipHeader(in);
ObjectInputStream din = new ObjectInputStream(in);
int n = din.readInt();
String classname = din.readUTF();
din.close();
dout.close();
sock.close();
代码示例来源:origin: stackoverflow.com
public static void main(String[] args) throws Exception {
LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
File file = new File("COOKIE.file");
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
Set
in.close();
WebClient wc = new WebClient();
Iterator
while (i.hasNext()) {
wc.getCOOKIEManager().addCOOKIE(i.next());
}
HtmlPage p = wc.getPage("http://google.com");
ObjectOutput out = new ObjectOutputStream(new FileOutputStream("COOKIE.file"));
out.writeObject(wc.getCOOKIEManager().getCOOKIEs());
out.close();
}
代码示例来源:origin: google/guava
@GwtIncompatible // java serialization not supported in GWT.
private static byte[] serializeWithBackReference(Object original, int handleOffset)
throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(original);
byte[] handle = toByteArray(baseWireHandle + handleOffset);
byte[] ref = prepended(TC_REFERENCE, handle);
bos.write(ref);
return bos.toByteArray();
}
代码示例来源:origin: redisson/redisson
Socket sock = new Socket(servername, port);
OutputStream out = new BufferedOutputStream(
sock.getOutputStream());
out.write(rmiCommand);
out.write(endofline);
out.write(endofline);
ObjectOutputStream dout = new ObjectOutputStream(out);
dout.writeInt(objectid);
dout.writeInt(methodid);
writeParameters(dout, args);
dout.flush();
InputStream ins = new BufferedInputStream(sock.getInputStream());
skipHeader(ins);
ObjectInputStream din = new ObjectInputStream(ins);
result = din.readBoolean();
rvalue = null;
errmsg = null;
if (result)
rvalue = din.readObject();
else
errmsg = din.readUTF();
din.close();
dout.close();
sock.close();
代码示例来源:origin: robolectric/robolectric
@Override
public
try {
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File(dir, id)))) {
out.writeObject(object);
return true;
}
} catch (IOException e) {
return false;
}
}
}
代码示例来源:origin: zendesk/maxwell
public T removeFirst(Class
if ( elementsInFile > 0 ) {
if ( is == null ) {
os.flush();
is = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
}
Object object = is.readObject();
T element = clazz.cast(object);
elementsInFile--;
return element;
} else {
return list.removeFirst();
}
}
代码示例来源:origin: org.freemarker/freemarker
throws IOException {
try {
Socket s = new Socket(host, port);
try {
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
int protocolVersion = in.readInt();
if (protocolVersion > 220) {
throw new IOException(
". At most 220 was expected.");
byte[] challenge = (byte[]) in.readObject();
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(password.getBytes("UTF-8"));
md.update(challenge);
out.writeObject(md.digest());
return new LocalDebuggerProxy((Debugger) in.readObject());
代码示例来源:origin: stanfordnlp/CoreNLP
public static ObjectOutputStream writeStreamFromString(String serializePath)
throws IOException {
ObjectOutputStream oos;
if (serializePath.endsWith(".gz")) {
oos = new ObjectOutputStream(new BufferedOutputStream(
new GZIPOutputStream(new FileOutputStream(serializePath))));
} else {
oos = new ObjectOutputStream(new BufferedOutputStream(
new FileOutputStream(serializePath)));
}
return oos;
}
代码示例来源: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: org.freemarker/freemarker
public void run() {
try {
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
byte[] challenge = new byte[512];
R.nextBytes(challenge);
out.writeInt(220); // protocol version
out.writeObject(challenge);
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(password);
md.update(challenge);
byte[] respOnse= (byte[]) in.readObject();
if (Arrays.equals(response, md.digest())) {
out.writeObject(debuggerStub);
} else {
out.writeObject(null);
}
} catch (Exception e) {
LOG.warn("Connection to " + s.getInetAddress().getHostAddress() + " abruply broke", e);
}
}