java.io.ByteArrayOutputStream.toByteArray()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(358)

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

ByteArrayOutputStream.toByteArray介绍

[英]Returns the contents of this ByteArrayOutputStream as a byte array. Any changes made to the receiver after returning will not be reflected in the byte array returned to the caller.
[中]以字节数组的形式返回此ByteArrayOutputStream的内容。返回后对接收器所做的任何更改都不会反映在返回给调用方的字节数组中。

代码示例

代码示例来源:origin: bumptech/glide

public static byte[] isToBytes(InputStream is) throws IOException {
 ByteArrayOutputStream os = new ByteArrayOutputStream();
 byte[] buffer = new byte[1024];
 int read;
 try {
  while ((read = is.read(buffer)) != -1) {
   os.write(buffer, 0, read);
  }
 } finally {
  is.close();
 }
 return os.toByteArray();
}

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

public static byte[] readNullTerminatedBytes(byte[] data, int index) {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  for (int i = index; i < data.length; i++) {
    byte item = data[i];
    if (item == MSC.NULL_TERMINATED_STRING_DELIMITER) {
      break;
    }
    out.write(item);
  }
  return out.toByteArray();
}

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

private Object serializeValue(SerializationDelegate serialization, Object storeValue) throws IOException {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  try {
    serialization.serialize(storeValue, out);
    return out.toByteArray();
  }
  finally {
    out.close();
  }
}

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

private void transformAndMarshal(Object graph, Result result) throws IOException {
  try {
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    marshalOutputStream(graph, os);
    ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    Transformer transformer = this.transformerFactory.newTransformer();
    transformer.transform(new StreamSource(is), result);
  }
  catch (TransformerException ex) {
    throw new MarshallingFailureException(
        "Could not transform to [" + ClassUtils.getShortName(result.getClass()) + "]", ex);
  }
}

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

@Override
public NFAState copy(NFAState from) {
  try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    serialize(from, new DataOutputViewStreamWrapper(baos));
    baos.close();
    byte[] data = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    NFAState copy = deserialize(new DataInputViewStreamWrapper(bais));
    bais.close();
    return copy;
  } catch (IOException e) {
    throw new RuntimeException("Could not copy NFA.", e);
  }
}

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

@SuppressWarnings("deprecation") // testing a deprecated method
public void testWriteBytes() throws IOException {
 /* Write out various test values in LITTLE ENDIAN FORMAT */
 out.writeBytes("r\u00C9sum\u00C9");
 byte[] data = baos.toByteArray();
 /* Setup input streams */
 DataInput in = new DataInputStream(new ByteArrayInputStream(data));
 /* Read in various values NORMALLY */
 byte[] b = new byte[6];
 in.readFully(b);
 assertEquals("r\u00C9sum\u00C9".getBytes(Charsets.ISO_8859_1), b);
}

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

@Override
protected void setUp() throws Exception {
 super.setUp();
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 DataOutputStream out = new DataOutputStream(baos);
 initializeData(out);
 data = baos.toByteArray();
}

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

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
 out = new ObjectOutputStream(bos);   
 out.writeObject(yourObject);
 out.flush();
 byte[] yourBytes = bos.toByteArray();
 ...
} finally {
 try {
  bos.close();
 } catch (IOException ex) {
  // ignore close exception
 }
}

代码示例来源:origin: org.mockito/mockito-core

/**
 * Creates the wrapper that be used in the serialization stream.
 *
 * <p>Immediately serializes the Mockito mock using specifically crafted {@link MockitoMockObjectOutputStream},
 * in a byte array.</p>
 *
 * @param mockitoMock The Mockito mock to serialize.
 * @throws java.io.IOException
 */
public CrossClassLoaderSerializationProxy(Object mockitoMock) throws IOException {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  ObjectOutputStream objectOutputStream = new MockitoMockObjectOutputStream(out);
  objectOutputStream.writeObject(mockitoMock);
  objectOutputStream.close();
  out.close();
  MockCreationSettings<?> mockSettings = MockUtil.getMockSettings(mockitoMock);
  this.serializedMock = out.toByteArray();
  this.typeToMock = mockSettings.getTypeToMock();
  this.extraInterfaces = mockSettings.getExtraInterfaces();
}

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

private static byte[] readAllBytes(InputStream input) throws IOException {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  int numRead;
  byte[] buffer = new byte[16384];
  while ((numRead = input.read(buffer, 0, buffer.length)) != -1) {
    out.write(buffer, 0, numRead);
  }
  return out.toByteArray();
}

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

/**
 * {@inheritDoc}
 */
public Sink write(Manifest manifest) throws IOException {
  if (manifest != null) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
      manifest.write(outputStream);
    } finally {
      outputStream.close();
    }
    storage.put(JarFile.MANIFEST_NAME, outputStream.toByteArray());
  }
  return this;
}

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

private String readCommand(ByteBuffer byteBuffer) {
  ByteArrayOutputStream command = new ByteArrayOutputStream(256);
  while (byteBuffer.remaining() > 0 && !tryConsumeEndOfLine(byteBuffer)) {
    command.write(byteBuffer.get());
  }
  return new String(command.toByteArray(), StandardCharsets.UTF_8);
}

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

public void testCopyChannel() throws IOException {
 byte[] expected = newPreFilledByteArray(100);
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 WritableByteChannel outChannel = Channels.newChannel(out);
 ReadableByteChannel inChannel = Channels.newChannel(new ByteArrayInputStream(expected));
 ByteStreams.copy(inChannel, outChannel);
 assertEquals(expected, out.toByteArray());
}

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

@Test
public void copyRange() throws Exception {
  ByteArrayOutputStream out = spy(new ByteArrayOutputStream());
  StreamUtils.copyRange(new ByteArrayInputStream(bytes), out, 0, 100);
  byte[] range = Arrays.copyOfRange(bytes, 0, 101);
  assertThat(out.toByteArray(), equalTo(range));
  verify(out, never()).close();
}

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

@SuppressWarnings("deprecation") // testing a deprecated method
public void testWriteBytes_discardHighOrderBytes() throws IOException {
 /* Write out various test values in LITTLE ENDIAN FORMAT */
 out.writeBytes("\uAAAA\uAABB\uAACC");
 byte[] data = baos.toByteArray();
 /* Setup input streams */
 DataInput in = new DataInputStream(new ByteArrayInputStream(data));
 /* Read in various values NORMALLY */
 byte[] b = new byte[3];
 in.readFully(b);
 byte[] expected = {(byte) 0xAA, (byte) 0xBB, (byte) 0xCC};
 assertEquals(expected, b);
}

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

public void testCopyTo_outputStream() throws IOException {
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 source.copyTo(out);
 assertExpectedBytes(out.toByteArray());
}

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

private static byte[] readAllBytes(InputStream input) throws IOException {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  int numRead;
  byte[] buffer = new byte[16384];
  while ((numRead = input.read(buffer, 0, buffer.length)) != -1) {
    out.write(buffer, 0, numRead);
  }
  return out.toByteArray();
}

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

public static String serialize(Session session) {
  if (null == session) {
    return null;
  }
  try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(session);
    return Base64.encodeToString(bos.toByteArray());
  } catch (Exception e) {
    throw new RuntimeException("serialize session error", e);
  }
}

相关文章