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

x33g5p2x  于2022-01-20 转载在 其他  
字(7.2k)|赞(0)|评价(0)|浏览(543)

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

IOUtils.toByteArray介绍

[英]Get the contents of an InputStream as a byte[].

This method buffers the input internally, so there is no need to use a BufferedInputStream.
[中]将InputStream的内容作为byte[]获取。
此方法在内部缓冲输入,因此无需使用BufferedInputStream

代码示例

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

/**
 * Gets the contents of a <code>URI</code> as a <code>byte[]</code>.
 *
 * @param uri the <code>URI</code> to read
 * @return the requested byte array
 * @throws NullPointerException if the uri is null
 * @throws IOException          if an I/O exception occurs
 * @since 2.4
 */
public static byte[] toByteArray(final URI uri) throws IOException {
  return IOUtils.toByteArray(uri.toURL());
}

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

/**
 * @deprecated Use instead {@link org.apache.commons.io.IOUtils#toByteArray(String)}
 */
@Deprecated
public static byte[] toByteArray(String input) throws IOException {
  return org.apache.commons.io.IOUtils.toByteArray(input);
}

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

/**
 * @deprecated Use instead {@link org.apache.commons.io.IOUtils#toByteArray(java.io.InputStream)}
 */
@Deprecated
public static byte[] toByteArray(InputStream input) throws IOException {
  return org.apache.commons.io.IOUtils.toByteArray(input);
}

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

/**
 * @deprecated Use instead {@link org.apache.commons.io.IOUtils#toByteArray(java.io.Reader)}
 */
@Deprecated
public static byte[] toByteArray(Reader input) throws IOException {
  return org.apache.commons.io.IOUtils.toByteArray(input);
}

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

/**
 * @deprecated Use instead {@link org.apache.commons.io.IOUtils#toByteArray(java.io.Reader, String)}
 */
@Deprecated
public static byte[] toByteArray(Reader input, String encoding) throws IOException {
  return org.apache.commons.io.IOUtils.toByteArray(input, encoding);
}

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

public byte[] get() {
  try {
    try (InputStream inputStream = Files.newInputStream(file.toPath())) {
      return IOUtils.toByteArray(inputStream);
    }
  } catch (IOException | InvalidPathException e) {
    throw new Error(e);
  }
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
  byte[] buffer = IOUtils.toByteArray(context.getInputStream());
  logger.info("The contents of request body is: \n" + new String(buffer, "UTF-8") + "\n");
  context.setInputStream(new ByteArrayInputStream(buffer));
  return context.proceed();
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
  byte[] buffer = IOUtils.toByteArray(context.getInputStream());
  logger.info("The contents of request body is: \n" + new String(buffer, "UTF-8") + "\n");
  context.setInputStream(new ByteArrayInputStream(buffer));
  return context.proceed();
}

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

@Test public void testToByteArray_String() throws Exception {
  try (FileReader fin = new FileReader(m_testFile)) {
    // Create our String. Rely on testReaderToString() to make sure this is valid.
    final String str = IOUtils.toString(fin);
    final byte[] out = IOUtils.toByteArray(str);
    assertEqualContent(str.getBytes(), out);
  }
}

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

@Test public void testToString_ByteArray() throws Exception {
  try (FileInputStream fin = new FileInputStream(m_testFile)) {
    final byte[] in = IOUtils.toByteArray(fin);
    // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
    final String str = IOUtils.toString(in);
    assertEqualContent(in, str.getBytes());
  }
}

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

@Test public void testToByteArray_URL() throws Exception {
  final URL url = m_testFile.toURI().toURL();
  final byte[] actual = IOUtils.toByteArray(url);
  assertEquals(FILE_SIZE, actual.length);
}

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

@Test public void testToByteArray_URI() throws Exception {
  final URI url = m_testFile.toURI();
  final byte[] actual = IOUtils.toByteArray(url);
  assertEquals(FILE_SIZE, actual.length);
}

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

@Test public void testToByteArray_InputStream_SizeIllegal() throws Exception {
  try (FileInputStream fin = new FileInputStream(m_testFile)) {
    IOUtils.toByteArray(fin, m_testFile.length() + 1);
    fail("IOException expected");
  } catch (final IOException exc) {
    assertTrue("Exception message does not start with \"Unexpected read size\"",
        exc.getMessage().startsWith("Unexpected read size"));
  }
}

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

@Test public void testToByteArray_InputStream_SizeLong() throws Exception {
  try (FileInputStream fin = new FileInputStream(m_testFile)) {
    IOUtils.toByteArray(fin, (long) Integer.MAX_VALUE + 1);
    fail("IOException expected");
  } catch (final IllegalArgumentException exc) {
    assertTrue("Exception message does not start with \"Size cannot be greater than Integer max value\"", exc
        .getMessage().startsWith("Size cannot be greater than Integer max value"));
  }
}

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

@Test public void testToByteArray_URLConnection() throws Exception {
  final URLConnection urlConn = m_testFile.toURI().toURL().openConnection();
  byte[] actual;
  try {
    actual = IOUtils.toByteArray(urlConn);
  } finally {
    IOUtils.close(urlConn);
  }
  assertEquals(FILE_SIZE, actual.length);
}

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

@Test public void testToByteArray_InputStream_SizeZero() throws Exception {
  try (FileInputStream fin = new FileInputStream(m_testFile)) {
    final byte[] out = IOUtils.toByteArray(fin, 0);
    assertNotNull("Out cannot be null", out);
    assertEquals("Out length must be 0", 0, out.length);
  }
}

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

@Test public void testToByteArray_InputStream_Size() throws Exception {
  try (FileInputStream fin = new FileInputStream(m_testFile)) {
    final byte[] out = IOUtils.toByteArray(fin, m_testFile.length());
    assertNotNull(out);
    assertEquals("Not all bytes were read", 0, fin.available());
    assertEquals("Wrong output size: out.length=" + out.length + "!=" + FILE_SIZE, FILE_SIZE, out.length);
    TestUtils.assertEqualContent(out, m_testFile);
  }
}

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

@Test public void testToByteArray_InputStream() throws Exception {
  try (FileInputStream fin = new FileInputStream(m_testFile)) {
    final byte[] out = IOUtils.toByteArray(fin);
    assertNotNull(out);
    assertEquals("Not all bytes were read", 0, fin.available());
    assertEquals("Wrong output size", FILE_SIZE, out.length);
    TestUtils.assertEqualContent(out, m_testFile);
  }
}

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

@Test public void testToBufferedInputStreamWithBufferSize_InputStream() throws Exception {
  try (FileInputStream fin = new FileInputStream(m_testFile)) {
    final InputStream in = IOUtils.toBufferedInputStream(fin, 2048);
    final byte[] out = IOUtils.toByteArray(in);
    assertNotNull(out);
    assertEquals("Not all bytes were read", 0, fin.available());
    assertEquals("Wrong output size", FILE_SIZE, out.length);
    TestUtils.assertEqualContent(out, m_testFile);
  }
}

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

@Test public void testToBufferedInputStream_InputStream() throws Exception {
  try (FileInputStream fin = new FileInputStream(m_testFile)) {
    final InputStream in = IOUtils.toBufferedInputStream(fin);
    final byte[] out = IOUtils.toByteArray(in);
    assertNotNull(out);
    assertEquals("Not all bytes were read", 0, fin.available());
    assertEquals("Wrong output size", FILE_SIZE, out.length);
    TestUtils.assertEqualContent(out, m_testFile);
  }
}

相关文章