java.io.ObjectInputStream.readObject()方法的使用及代码示例

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

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

ObjectInputStream.readObject介绍

[英]Reads the next object from the source stream.
[中]从源流读取下一个对象。

代码示例

代码示例来源: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: hankcs/HanLP

public boolean open(InputStream stream)
{
  try
  {
    ObjectInputStream ois = new ObjectInputStream(stream);
    int version = (Integer) ois.readObject();
    costFactor_ = (Double) ois.readObject();
    maxid_ = (Integer) ois.readObject();
    xsize_ = (Integer) ois.readObject();
    y_ = (List<String>) ois.readObject();
    unigramTempls_ = (List<String>) ois.readObject();
    bigramTempls_ = (List<String>) ois.readObject();
    dat = (MutableDoubleArrayTrieInteger) ois.readObject();
    alpha_ = (double[]) ois.readObject();
    ois.close();
    return true;
  }
  catch (Exception e)
  {
    e.printStackTrace();
    return false;
  }
}

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

@SuppressWarnings("unchecked")
 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
  stream.defaultReadObject();
  init(DEFAULT_SIZE, DEFAULT_LOAD_FACTOR);
  int elementCount = stream.readInt();
  for (int i = elementCount; --i >= 0; ) {
   K key = (K) stream.readObject();
   V value = (V) stream.readObject();
   put(key, value);
  }
 }
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Prints out the pair of {@code Extractors} objects found in the
 * file that is the first and only argument.
 * @param args Filename of extractors file (standardly written with
 *       {@code .ex} extension)
 */
public static void main(String[] args) {
 try {
  ObjectInputStream in = new ObjectInputStream(new FileInputStream(args[0]));
  Extractors extrs = (Extractors) in.readObject();
  Extractors extrsRare = (Extractors) in.readObject();
  in.close();
  System.out.println("All words:  " + extrs);
  System.out.println("Rare words: " + extrsRare);
 } catch (Exception e) {
  throw new RuntimeException(e);
 }
}

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

public static KerberosTicket deserializeKerberosTicket(byte[] tgtBytes) {
  KerberosTicket ret;
  try {
    ByteArrayInputStream bin = new ByteArrayInputStream(tgtBytes);
    ObjectInputStream in = new ObjectInputStream(bin);
    ret = (KerberosTicket) in.readObject();
    in.close();
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
  return ret;
}

代码示例来源: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: stackoverflow.com

try {
  FileInputStream e = new FileInputStream("outings.ser");
  ObjectInputStream inputStream = new ObjectInputStream(e);
  return (ArrayList)inputStream.readObject();
} catch (ClassNotFoundException | IOException var3) {
  var3.printStackTrace();
}
return null;

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

/**
 * Deserialize the byte array into an object.
 * @param bytes a serialized object
 * @return the result of deserializing the bytes
 */
@Nullable
public static Object deserialize(@Nullable byte[] bytes) {
  if (bytes == null) {
    return null;
  }
  try {
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
    return ois.readObject();
  }
  catch (IOException ex) {
    throw new IllegalArgumentException("Failed to deserialize object", ex);
  }
  catch (ClassNotFoundException ex) {
    throw new IllegalStateException("Failed to deserialize object type", ex);
  }
}

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

public static Object deserialize(InputStream inputStream) throws IOException, ClassNotFoundException {
  try (ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) {
    return objectInputStream.readObject();
  }
}

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

@Override
  protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
      return null;
    }

    ObjectInputStream ois = new CompactObjectInputStream(new ByteBufInputStream(frame, true), classResolver);
    try {
      return ois.readObject();
    } finally {
      ois.close();
    }
  }
}

代码示例来源: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: guoguibing/librec

public static Object deserialize(String filePath) throws Exception {
  FileInputStream fis = new FileInputStream(filePath);
  ObjectInputStream ois = new ObjectInputStream(fis);
  Object obj = ois.readObject();
  ois.close();
  fis.close();
  return obj;
}

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

ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
GZIPInputStream gzipIn = new GZIPInputStream(bais);
ObjectInputStream objectIn = new ObjectInputStream(gzipIn);
MyObject myObj1 = (MyObject) objectIn.readObject();
MyObject myObj2 = (MyObject) objectIn.readObject();
objectIn.close();

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

Map map = new HashMap();
   map.put("1",new Integer(1));
   map.put("2",new Integer(2));
   map.put("3",new Integer(3));
   FileOutputStream fos = new FileOutputStream("map.ser");
   ObjectOutputStream oos = new ObjectOutputStream(fos);
   oos.writeObject(map);
   oos.close();
   FileInputStream fis = new FileInputStream("map.ser");
   ObjectInputStream ois = new ObjectInputStream(fis);
   Map anotherMap = (Map) ois.readObject();
   ois.close();
   System.out.println(anotherMap);

代码示例来源:origin: hankcs/HanLP

public static <T> DoubleArrayTrie<T> unSerialize(String path)
  {
    ObjectInputStream in;
    try
    {
      in = new ObjectInputStream(IOAdapter == null ? new FileInputStream(path) : IOAdapter.open(path));
      return (DoubleArrayTrie<T>) in.readObject();
    }
    catch (Exception e)
    {
//            e.printStackTrace();
      return null;
    }
  }

代码示例来源:origin: shuzheng/zheng

public static Session deserialize(String sessionStr) {
  if (StringUtils.isBlank(sessionStr)) {
    return null;
  }
  try {
    ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(sessionStr));
    ObjectInputStream ois = new ObjectInputStream(bis);
    return (Session) ois.readObject();
  } catch (Exception e) {
    throw new RuntimeException("deserialize session error", e);
  }
}

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

@GwtIncompatible // java.io.ObjectInputStream
@SuppressWarnings("unchecked") // reading data stored by writeObject
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
 stream.defaultReadObject();
 factory = (Supplier<? extends List<V>>) stream.readObject();
 Map<K, Collection<V>> map = (Map<K, Collection<V>>) stream.readObject();
 setMap(map);
}

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

FileInputStream fis = context.openFileInput(fileName);
ObjectInputStream is = new ObjectInputStream(fis);
SimpleClass simpleClass = (SimpleClass) is.readObject();
is.close();
fis.close();

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

@Override
  protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
      return null;
    }

    ObjectInputStream ois = new CompactObjectInputStream(new ByteBufInputStream(frame, true), classResolver);
    try {
      return ois.readObject();
    } finally {
      ois.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();
}

相关文章

ObjectInputStream类方法