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

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

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

ObjectOutputStream.flush介绍

[英]Writes buffered data to the target stream and calls the flushmethod of the target stream.
[中]将缓冲数据写入目标流,并调用目标流的flushmethod。

代码示例

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

protected byte[] toByteArray(Object value) throws IOException {
  if (value == null) {
    return null;
  }
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  ObjectOutputStream out = new ObjectOutputStream(bos);
  out.writeObject(value);
  out.flush();
  return bos.toByteArray();
}

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

ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.flush();
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());

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

@Override
  protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
    ObjectOutputStream oos = newObjectOutputStream(new ByteBufOutputStream(out));
    try {
      if (resetInterval != 0) {
        // Resetting will prevent OOM on the receiving side.
        writtenObjects ++;
        if (writtenObjects % resetInterval == 0) {
          oos.reset();
        }
      }

      oos.writeObject(msg);
      oos.flush();
    } finally {
      oos.close();
    }
  }
}

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

private 
void sendCachedEvents(ObjectOutputStream stream) throws IOException {
 if (buffer != null) {
  for (int i = 0; i < buffer.length(); i++) {
   stream.writeObject(buffer.get(i));
  }
  stream.flush();
  stream.reset();
 }
}

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

protected byte[] toByteArray(Object value) throws IOException {
  if (value == null) {
    return null;
  }
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  ObjectOutputStream out = new ObjectOutputStream(bos);
  out.writeObject(value);
  out.flush();
  return bos.toByteArray();
}

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

@Override
public void writeObject(Object obj) throws IOException {
  ByteBuf buf = Unpooled.buffer(estimatedLength);
  try {
    ObjectOutputStream oout = new CompactObjectOutputStream(new ByteBufOutputStream(buf));
    try {
      oout.writeObject(obj);
      oout.flush();
    } finally {
      oout.close();
    }
    int objectSize = buf.readableBytes();
    writeInt(objectSize);
    buf.getBytes(0, this, objectSize);
  } finally {
    buf.release();
  }
}

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

/**
 * Writes the lambdas to a stream.
 */
public static void save_lambdas(DataOutputStream rf, double[] lambdas) {
 try {
  ObjectOutputStream oos = new ObjectOutputStream(rf);
  oos.writeObject(lambdas);
  oos.flush();
 } catch (IOException e) {
  e.printStackTrace();
 }
}

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

protected byte[] toByteArray(Object value) throws IOException {
  if (value == null) {
    return null;
  }
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  ObjectOutputStream out = new ObjectOutputStream(bos);
  out.writeObject(value);
  out.flush();
  return bos.toByteArray();
}

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

@Override
  protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
    int startIdx = out.writerIndex();

    ByteBufOutputStream bout = new ByteBufOutputStream(out);
    ObjectOutputStream oout = null;
    try {
      bout.write(LENGTH_PLACEHOLDER);
      oout = new CompactObjectOutputStream(bout);
      oout.writeObject(msg);
      oout.flush();
    } finally {
      if (oout != null) {
        oout.close();
      } else {
        bout.close();
      }
    }

    int endIdx = out.writerIndex();

    out.setInt(startIdx, endIdx - startIdx - 4);
  }
}

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

/** Serialize the ExtractorFrames and ExtractorFramesRare to os. */
private void saveExtractors(OutputStream os) throws IOException {
 ObjectOutputStream out = new ObjectOutputStream(os);
 out.writeObject(extractors);
 out.writeObject(extractorsRare);
 out.flush();
}

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

protected byte[] toByteArray(Object value) throws IOException {
  if (value == null) {
    return null;
  }
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  ObjectOutputStream out = new ObjectOutputStream(bos);
  out.writeObject(value);
  out.flush();
  return bos.toByteArray();
}

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

@Override
  protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
    ObjectOutputStream oos = newObjectOutputStream(new ByteBufOutputStream(out));
    try {
      if (resetInterval != 0) {
        // Resetting will prevent OOM on the receiving side.
        writtenObjects ++;
        if (writtenObjects % resetInterval == 0) {
          oos.reset();
        }
      }

      oos.writeObject(msg);
      oos.flush();
    } finally {
      oos.close();
    }
  }
}

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

public static byte[] serializeObject(Object o) throws IOException {
  try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(baos)) {
    oos.writeObject(o);
    oos.flush();
    return baos.toByteArray();
  }
}

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

@Override
public void writeObject(Object obj) throws IOException {
  ByteBuf buf = Unpooled.buffer(estimatedLength);
  try {
    ObjectOutputStream oout = new CompactObjectOutputStream(new ByteBufOutputStream(buf));
    try {
      oout.writeObject(obj);
      oout.flush();
    } finally {
      oout.close();
    }
    int objectSize = buf.readableBytes();
    writeInt(objectSize);
    buf.getBytes(0, this, objectSize);
  } finally {
    buf.release();
  }
}

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

public static byte[] serializeObject(Object o) throws IOException {
  try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)) {
    oos.writeObject(o);
    oos.flush();
    return baos.toByteArray();
  }
}

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

/**
 * Serialize the given object to a byte array.
 * @param object the object to serialize
 * @return an array of bytes representing the object in a portable fashion
 */
@Nullable
public static byte[] serialize(@Nullable Object object) {
  if (object == null) {
    return null;
  }
  ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
  try {
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(object);
    oos.flush();
  }
  catch (IOException ex) {
    throw new IllegalArgumentException("Failed to serialize object of type: " + object.getClass(), ex);
  }
  return baos.toByteArray();
}

相关文章

ObjectOutputStream类方法