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

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

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

ObjectInputStream.close介绍

[英]Closes this stream. This implementation closes the source stream.
[中]关闭此流。此实现将关闭源流。

代码示例

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

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

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

/**
 * Create a new input stream, along with the code to close it and clean up.
 * This code may be overridden, but should match nextObjectOrNull().
 * IMPORTANT NOTE: acquiring a lock (well, semaphore) with FileBackedCache#acquireFileLock(File)
 * is generally a good idea. Make sure to release() it in the close action as well.
 *
 * @param f The file to read from
 * @return A pair, corresponding to the stream and the code to close it.
 * @throws IOException
 */
protected Pair<? extends InputStream, CloseAction> newInputStream(File f) throws IOException {
 final FileSemaphore lock = acquireFileLock(f);
 final ObjectInputStream rtn = new ObjectInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(f))));
 return new Pair<>(rtn,
     () -> {
      lock.release();
      rtn.close();
     });
}

代码示例来源:origin: jenkinsci/jenkins

private ConsoleAnnotator<T> createAnnotator(StaplerRequest req) throws IOException {
  try {
    String base64 = req!=null ? req.getHeader("X-ConsoleAnnotator") : null;
    if (base64!=null) {
      Cipher sym = PASSING_ANNOTATOR.decrypt();
      ObjectInputStream ois = new ObjectInputStreamEx(new GZIPInputStream(
          new CipherInputStream(new ByteArrayInputStream(Base64.decode(base64.toCharArray())),sym)),
          Jenkins.getInstance().pluginManager.uberClassLoader);
      try {
        long timestamp = ois.readLong();
        if (TimeUnit.HOURS.toMillis(1) > abs(System.currentTimeMillis()-timestamp))
          // don't deserialize something too old to prevent a replay attack
          return (ConsoleAnnotator)ois.readObject();
      } finally {
        ois.close();
      }
    }
  } catch (ClassNotFoundException e) {
    throw new IOException(e);
  }
  // start from scratch
  return ConsoleAnnotator.initial(context);
}

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

FileInputStream fis = new FileInputStream("t.tmp");
ObjectInputStream ois = new ObjectInputStream(fis);
List<Club> clubs = (List<Club>) ois.readObject();
ois.close();

代码示例来源:origin: quartz-scheduler/quartz

public static Object deserialize(byte[] bytes) {
 try {
  ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  ObjectInputStream ois = new ObjectInputStream(bais);
  Object obj = ois.readObject();
  ois.close();
  return obj;
 } catch (Exception e) {
  throw new RuntimeException("error deserializing " + bytes, e);
 }
}

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

private static boolean loadBin(String path)
{
  try
  {
    ObjectInputStream in = new ObjectInputStream(IOUtil.newInputStream(path));
    CONVERT = (char[]) in.readObject();
    in.close();
  }
  catch (Exception e)
  {
    logger.warning("字符正规化表缓存加载失败,原因如下:" + e);
    return false;
  }
  return true;
}

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

ObjectOutputStream os = new ObjectOutputStream(outBytes);
os.writeObject(event);
os.close();
    raw[8 + i] = (byte) subClassName.charAt(i);
  ByteArrayInputStream inBytes = new ByteArrayInputStream(raw);
  ObjectInputStream is = new ObjectInputStream(inBytes);
  Object cracked = is.readObject();
  if (cracked instanceof LogEvent) {
    keySet = ((LogEvent) cracked).getPropertyKeySet();
  is.close();

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

public static <T> ClassicCounter<T> deserializeCounter(String filename) throws Exception {
 // reconstitute
 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(filename)));
 ClassicCounter<T> c = ErasureUtils.uncheckedCast(in.readObject());
 in.close();
 return c;
}

代码示例来源:origin: quartz-scheduler/quartz

public static Object deserialize(byte[] bytes) {
 try {
  ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  ObjectInputStream ois = new ObjectInputStream(bais);
  Object obj = ois.readObject();
  ois.close();
  return obj;
 } catch (Exception e) {
  throw new RuntimeException("error deserializing " + bytes, 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: redisson/redisson

private void lookupName(String cmd, InputStream ins, OutputStream outs)
    throws IOException
  {
    ObjectInputStream in = new ObjectInputStream(ins);
    String name = DataInputStream.readUTF(in);
    ExportedObject found = (ExportedObject)exportedNames.get(name);
    outs.write(okHeader);
    ObjectOutputStream out = new ObjectOutputStream(outs);
    if (found == null) {
      logging2(name + "not found.");
      out.writeInt(-1);           // error code
      out.writeUTF("error");
    }
    else {
      logging2(name);
      out.writeInt(found.identifier);
      out.writeUTF(found.object.getClass().getName());
    }

    out.flush();
    out.close();
    in.close();
  }
}

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

@Override
  public <T> T deserialize(byte[] bytes, Class<T> clazz) {
    try {
      ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
      GZIPInputStream gis = new GZIPInputStream(bis);
      ObjectInputStream ois = new ObjectInputStream(gis);
      Object ret = ois.readObject();
      ois.close();
      return (T) ret;
    } catch (IOException | ClassNotFoundException ioe) {
      throw new RuntimeException(ioe);
    }
  }
}

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

/**
 * 反序列化对象
 *
 * @param path
 * @return
 */
public static Object readObjectFrom(String path)
{
  ObjectInputStream ois = null;
  try
  {
    ois = new ObjectInputStream(IOUtil.newInputStream(path));
    Object o = ois.readObject();
    ois.close();
    return o;
  }
  catch (Exception e)
  {
    logger.warning("在从" + path + "读取对象时发生异常" + e);
  }
  return null;
}

相关文章

ObjectInputStream类方法