/** * 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()); }
/** * @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); }
/** * @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); }
/** * @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); }
/** * @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); }
public byte[] get() { try { try (InputStream inputStream = Files.newInputStream(file.toPath())) { return IOUtils.toByteArray(inputStream); } } catch (IOException | InvalidPathException e) { throw new Error(e); } }
@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(); }
@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(); }
@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); } }
@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()); } }
@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); }
@Test public void testToByteArray_URI() throws Exception { final URI url = m_testFile.toURI(); final byte[] actual = IOUtils.toByteArray(url); assertEquals(FILE_SIZE, actual.length); }
@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")); } }
@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")); } }
@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); }
@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); } }
@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); } }
@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); } }
@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); } }
@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); } }