/** * @see #readBytes(InputStream, int) */ public static byte[] readBytes(final InputStream input) throws IOException { return readBytes(input, ALL); }
/** * @see #readBytes(Reader, String, int) */ public static byte[] readBytes(final Reader input, final String encoding) throws IOException { return readBytes(input, encoding, ALL); }
/** * Downloads resource as byte array. */ public static byte[] downloadBytes(String url) throws IOException { InputStream inputStream = new URL(url).openStream(); return StreamUtil.readBytes(inputStream); }
/** * Downloads resource as byte array. */ public static byte[] downloadBytes(final String url) throws IOException { try (InputStream inputStream = new URL(url).openStream()) { return StreamUtil.readBytes(inputStream); } }
/** * @see #readBytes(Reader, String) */ public static byte[] readBytes(final Reader input) throws IOException { return readBytes(input, encoding()); }
/** * @see #readBytes(Reader, String, int) */ public static byte[] readBytes(final Reader input, final int count) throws IOException { return readBytes(input, encoding(), count); }
/** * Reads stream bytes. Since stream can be read only once, the byte content * is cached. */ public byte[] readBytes() throws IOException { openInputStream(); if (inputStreamBytes == null) { inputStreamBytes = StreamUtil.readBytes(inputStream); } return inputStreamBytes; }
/** * Returns <code>true</code> if class contains {@link #getTypeSignatureBytes(Class) type signature}. * It searches the class content for bytecode signature. This is the fastest way of finding if come * class uses some type. Please note that if signature exists it still doesn't means that class uses * it in expected way, therefore, class should be loaded to complete the scan. */ protected boolean isTypeSignatureInUse(InputStream inputStream, byte[] bytes) { try { byte[] data = StreamUtil.readBytes(inputStream); int index = ArraysUtil.indexOf(data, bytes); return index != -1; } catch (IOException ioex) { throw new FindFileException("Read error", ioex); } }
protected Map<String, String> copyParts(HttpServletRequest req) { Map<String, String> parts = new HashMap<>(); if (req.getContentType() == null) { return parts; } if (req.getContentType() != null && !req.getContentType().toLowerCase().contains("multipart/form-data")) { return parts; } String enc = "UTF-8"; try { Collection<Part> prs = req.getParts(); for (Part p : prs) { parts.put(p.getName(), new String(StreamUtil.readBytes(p.getInputStream()), enc)); } } catch (IOException | ServletException e) { e.printStackTrace(); } return parts; }
@ParameterizedTest @MethodSource("testdata_testReadBytes_InputStream") void testReadBytes_InputStream(byte[] expected, String text) throws Exception { byte[] actual = null; try (ByteArrayInputStream inputStream = new ByteArrayInputStream(text.getBytes())) { actual = StreamUtil.readBytes(inputStream); } // asserts assertNotNull(actual); assertArrayEquals(expected, actual); }
@ParameterizedTest @MethodSource("testdata_testReadBytes_InputStream_ByteCount") void testReadBytes_InputStream_ByteCount(byte[] expected, String text, int byteCount) throws Exception { byte[] actual = null; try (ByteArrayInputStream inputStream = new ByteArrayInputStream(text.getBytes())) { actual = StreamUtil.readBytes(inputStream, byteCount); } // asserts assertNotNull(actual); assertArrayEquals(expected, actual); }
@ParameterizedTest @MethodSource("testdata_testReadBytes_Reader") void testReadBytes_Reader(byte[] expected, String text) throws Exception { byte[] actual = null; try (StringReader reader = new StringReader(text)) { actual = StreamUtil.readBytes(reader); } // asserts assertNotNull(actual); assertArrayEquals(expected, actual); }
@ParameterizedTest @MethodSource("testdata_testReadBytes_Reader_ByteCount") void testReadBytes_Reader_ByteCount(byte[] expected, String text, int byteCount) throws Exception { byte[] actual = null; try (StringReader reader = new StringReader(text)) { actual = StreamUtil.readBytes(reader, byteCount); } // asserts assertNotNull(actual); assertArrayEquals(expected, actual); }
@ParameterizedTest @MethodSource("testdata_testReadBytes_Reader_Encoding") void testReadBytes_Reader_Encoding(byte[] expected, String text, String encoding, TestInfo testInfo) throws Exception { final int random = MathUtil.randomInt(1, 2500); final File file = new File(StreamUtilTest.BASE_DIR, testInfo.getTestMethod().get().getName() + random); FileUtil.writeString(file, text, encoding); byte[] actual = null; try (FileReader reader = new FileReader(file)) { actual = StreamUtil.readBytes(reader, encoding); } // asserts assertNotNull(actual); assertArrayEquals(expected, actual); }
@ParameterizedTest @MethodSource("testdata_testReadBytes_Reader_Encoding_ByteCount") void testReadBytes_Reader_Encoding_ByteCount(byte[] expected, String text, String encoding, int byteCount, TestInfo testInfo) throws Exception { final int random = MathUtil.randomInt(1, 2500); final File file = new File(StreamUtilTest.BASE_DIR, testInfo.getTestMethod().get().getName() + random); FileUtil.writeString(file, text, encoding); byte[] actual = null; try (FileReader reader = new FileReader(file)) { actual = StreamUtil.readBytes(reader, encoding, byteCount); } // asserts assertNotNull(actual); assertArrayEquals(expected, actual); }
/** * @see #readBytes(InputStream, int) */ public static byte[] readBytes(final InputStream input) throws IOException { return readBytes(input, ALL); }
/** * @see #readBytes(Reader, String, int) */ public static byte[] readBytes(final Reader input, final String encoding) throws IOException { return readBytes(input, encoding, ALL); }
/** * Downloads resource as byte array. */ public static byte[] downloadBytes(final String url) throws IOException { try (InputStream inputStream = new URL(url).openStream()) { return StreamUtil.readBytes(inputStream); } }
/** * @see #readBytes(Reader, String) */ public static byte[] readBytes(final Reader input) throws IOException { return readBytes(input, encoding()); }
/** * Reads stream bytes. Since stream can be read only once, the byte content * is cached. */ public byte[] readBytes() throws IOException { openInputStream(); if (inputStreamBytes == null) { inputStreamBytes = StreamUtil.readBytes(inputStream); } return inputStreamBytes; }