org.apache.commons.io.output.ByteArrayOutputStream.toByteArray()方法的使用及代码示例

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

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

ByteArrayOutputStream.toByteArray介绍

[英]Gets the curent contents of this byte stream as a byte array. The result is independent of this stream.
[中]以字节数组的形式获取此字节流的当前内容。结果与此流无关。

代码示例

代码示例来源:origin: commons-io/commons-io

/**
 * Gets the contents of an <code>InputStream</code> as a <code>byte[]</code>.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 *
 * @param input the <code>InputStream</code> to read from
 * @return the requested byte array
 * @throws NullPointerException if the input is null
 * @throws IOException          if an I/O error occurs
 */
public static byte[] toByteArray(final InputStream input) throws IOException {
  try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) {
    copy(input, output);
    return output.toByteArray();
  }
}

代码示例来源:origin: commons-io/commons-io

/**
 * Gets the contents of a <code>Reader</code> as a <code>byte[]</code>
 * using the specified character encoding.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedReader</code>.
 *
 * @param input the <code>Reader</code> to read from
 * @param encoding the encoding to use, null means platform default
 * @return the requested byte array
 * @throws NullPointerException if the input is null
 * @throws IOException          if an I/O error occurs
 * @since 2.3
 */
public static byte[] toByteArray(final Reader input, final Charset encoding) throws IOException {
  try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) {
    copy(input, output, encoding);
    return output.toByteArray();
  }
}

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

private void encryptPasswords(File configFile) throws Exception {
  String currentContent = FileUtils.readFileToString(configFile, UTF_8);
  GoConfigHolder configHolder = magicalGoConfigXmlLoader.loadConfigHolder(currentContent);
  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  magicalGoConfigXmlWriter.write(configHolder.configForEdit, stream, true);
  String postEncryptContent = new String(stream.toByteArray());
  if (!currentContent.equals(postEncryptContent)) {
    LOGGER.debug("[Encrypt] Writing config to file");
    FileUtils.writeStringToFile(configFile, postEncryptContent);
  }
}

代码示例来源:origin: commons-io/commons-io

public static byte[] generateTestData(final long size) {
  try {
    final ByteArrayOutputStream baout = new ByteArrayOutputStream();
    generateTestData(baout, size);
    return baout.toByteArray();
  } catch (final IOException ioe) {
    throw new RuntimeException("This should never happen: " + ioe.getMessage());
  }
}

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

public long writeHtmlTo(long start, Writer w) throws IOException {
  ConsoleAnnotationOutputStream<T> caw = new ConsoleAnnotationOutputStream<>(
      w, createAnnotator(Stapler.getCurrentRequest()), context, charset);
  long r = super.writeLogTo(start,caw);
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  Cipher sym = PASSING_ANNOTATOR.encrypt();
  ObjectOutputStream oos = AnonymousClassWarnings.checkingObjectOutputStream(new GZIPOutputStream(new CipherOutputStream(baos,sym)));
  oos.writeLong(System.currentTimeMillis()); // send timestamp to prevent a replay attack
  oos.writeObject(caw.getConsoleAnnotator());
  oos.close();
  StaplerResponse rsp = Stapler.getCurrentResponse();
  if (rsp!=null)
    rsp.setHeader("X-ConsoleAnnotator", new String(Base64.encode(baos.toByteArray())));
  return r;
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testWrite_byteArrayToWriter_Encoding() throws Exception {
  final ByteArrayOutputStream baout = new ByteArrayOutputStream();
  @SuppressWarnings("resource") // deliberately not closed
  final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
  final Writer writer = new OutputStreamWriter(baout, "US-ASCII");
  IOUtils.write(inData, writer, "UTF8");
  out.off();
  writer.flush();
  byte[] bytes = baout.toByteArray();
  bytes = new String(bytes, "UTF8").getBytes("US-ASCII");
  assertTrue("Content differs", Arrays.equals(inData, bytes));
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testWrite_charSequenceToOutputStream_Encoding() throws Exception {
  final CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
  final ByteArrayOutputStream baout = new ByteArrayOutputStream();
  final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
  IOUtils.write(csq, out, "UTF16");
  out.off();
  out.flush();
  byte[] bytes = baout.toByteArray();
  bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
  assertTrue("Content differs", Arrays.equals(inData, bytes));
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testWrite_stringToOutputStream_Encoding() throws Exception {
  final String str = new String(inData, "US-ASCII");
  final ByteArrayOutputStream baout = new ByteArrayOutputStream();
  final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
  IOUtils.write(str, out, "UTF16");
  out.off();
  out.flush();
  byte[] bytes = baout.toByteArray();
  bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
  assertTrue("Content differs", Arrays.equals(inData, bytes));
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testWrite_charArrayToOutputStream_Encoding() throws Exception {
  final String str = new String(inData, "US-ASCII");
  final ByteArrayOutputStream baout = new ByteArrayOutputStream();
  final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
  IOUtils.write(str.toCharArray(), out, "UTF16");
  out.off();
  out.flush();
  byte[] bytes = baout.toByteArray();
  bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
  assertTrue("Content differs", Arrays.equals(inData, bytes));
}

代码示例来源:origin: commons-io/commons-io

@SuppressWarnings("resource") // 'in' is deliberately not closed
@Test
public void testCopy_readerToOutputStream_Encoding() throws Exception {
  InputStream in = new ByteArrayInputStream(inData);
  in = new YellOnCloseInputStream(in);
  final Reader reader = new InputStreamReader(in, "US-ASCII");
  final ByteArrayOutputStream baout = new ByteArrayOutputStream();
  final OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
  IOUtils.copy(reader, out, "UTF16");
  // note: this method *does* flush.
  // note: we don't flush here; this IOUtils method does it for us
  byte[] bytes = baout.toByteArray();
  bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
  assertTrue("Content differs", Arrays.equals(inData, bytes));
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testWrite_charSequenceToWriter() throws Exception {
  final CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
  final ByteArrayOutputStream baout = new ByteArrayOutputStream();
  @SuppressWarnings("resource") // deliberately not closed
  final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
  final Writer writer = new OutputStreamWriter(baout, "US-ASCII");
  IOUtils.write(csq, writer);
  out.off();
  writer.flush();
  assertEquals("Sizes differ", inData.length, baout.size());
  assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testWrite_charArrayToWriter() throws Exception {
  final String str = new String(inData, "US-ASCII");
  final ByteArrayOutputStream baout = new ByteArrayOutputStream();
  @SuppressWarnings("resource") // deliberately not closed
  final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
  final Writer writer = new OutputStreamWriter(baout, "US-ASCII");
  IOUtils.write(str.toCharArray(), writer);
  out.off();
  writer.flush();
  assertEquals("Sizes differ", inData.length, baout.size());
  assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testNormalStream() {
  try {
    final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    final OutputStream stream = new TaggedOutputStream(buffer);
    stream.write('a');
    stream.write(new byte[] { 'b' });
    stream.write(new byte[] { 'c' }, 0, 1);
    stream.flush();
    stream.close();
    assertEquals(3, buffer.size());
    assertEquals('a', buffer.toByteArray()[0]);
    assertEquals('b', buffer.toByteArray()[1]);
    assertEquals('c', buffer.toByteArray()[2]);
  } catch (final IOException e) {
    fail("Unexpected exception thrown");
  }
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testWrite_byteArrayToWriter_Encoding_nullEncoding() throws Exception {
  final ByteArrayOutputStream baout = new ByteArrayOutputStream();
  @SuppressWarnings("resource") // deliberately not closed
  final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
  final Writer writer = new OutputStreamWriter(baout, "US-ASCII");
  IOUtils.write(inData, writer, (String) null);
  out.off();
  writer.flush();
  assertEquals("Sizes differ", inData.length, baout.size());
  assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}

代码示例来源:origin: commons-io/commons-io

@SuppressWarnings("resource") // 'in' is deliberately not closed
@Test
public void testCopy_inputStreamToWriter_Encoding() throws Exception {
  InputStream in = new ByteArrayInputStream(inData);
  in = new YellOnCloseInputStream(in);
  final ByteArrayOutputStream baout = new ByteArrayOutputStream();
  final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
  final Writer writer = new OutputStreamWriter(baout, "US-ASCII");
  IOUtils.copy(in, writer, "UTF8");
  out.off();
  writer.flush();
  assertEquals("Not all bytes were read", 0, in.available());
  byte[] bytes = baout.toByteArray();
  bytes = new String(bytes, "UTF8").getBytes("US-ASCII");
  assertTrue("Content differs", Arrays.equals(inData, bytes));
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testWrite_stringToWriter() throws Exception {
  final String str = new String(inData, "US-ASCII");
  final ByteArrayOutputStream baout = new ByteArrayOutputStream();
  @SuppressWarnings("resource") // deliberately not closed
  final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
  final Writer writer = new OutputStreamWriter(baout, "US-ASCII");
  IOUtils.write(str, writer);
  out.off();
  writer.flush();
  assertEquals("Sizes differ", inData.length, baout.size());
  assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}

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

@Override
public byte[] serializeValue(Tuple3<Integer, Integer, String> element) {
  ByteArrayOutputStream by = new ByteArrayOutputStream();
  DataOutputView out = new DataOutputViewStreamWrapper(by);
  try {
    ts.serialize(new Tuple2<>(element.f0, element.f1), out);
  } catch (IOException e) {
    throw new RuntimeException("Error" , e);
  }
  return by.toByteArray();
}

代码示例来源:origin: commons-io/commons-io

@Test
public void copy_stringToWriter() throws Exception {
  final String str = new String(inData, "US-ASCII");
  final ByteArrayOutputStream baout = new ByteArrayOutputStream();
  final OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
  final Writer writer = new java.io.OutputStreamWriter(out, "US-ASCII");
  CopyUtils.copy(str, writer);
  writer.flush();
  assertEquals("Sizes differ", inData.length, baout.size());
  assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testWrite_byteArrayToWriter() throws Exception {
  final ByteArrayOutputStream baout = new ByteArrayOutputStream();
  @SuppressWarnings("resource") // deliberately not closed
  final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
  final Writer writer = new OutputStreamWriter(baout, "US-ASCII");
  IOUtils.write(inData, writer);
  out.off();
  writer.flush();
  assertEquals("Sizes differ", inData.length, baout.size());
  assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testWrite_charSequenceToOutputStream() throws Exception {
  final CharSequence csq = new StringBuilder(new String(inData, "US-ASCII"));
  final ByteArrayOutputStream baout = new ByteArrayOutputStream();
  final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
  IOUtils.write(csq, out);
  out.off();
  out.flush();
  assertEquals("Sizes differ", inData.length, baout.size());
  assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}

相关文章