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

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

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

ByteArrayOutputStream.<init>介绍

[英]Creates a new byte array output stream. The buffer capacity is initially 1024 bytes, though its size increases if necessary.
[中]创建新的字节数组输出流。缓冲区容量最初为1024字节,但如有必要,其大小会增加。

代码示例

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

@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: 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 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

@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: 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()));
}

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

@Test
public void testWrite_charSequenceToOutputStream_nullEncoding() 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, (String) null);
  out.off();
  out.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_charArrayToOutputStream() 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);
  out.off();
  out.flush();
  assertEquals("Sizes differ", inData.length, baout.size());
  assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}

相关文章