@Test public void testAsBufferedInputStreamWithBufferSize() { final InputStream is = new InputStream() { @Override public int read() throws IOException { return 0; } }; final BufferedInputStream bis = IOUtils.buffer(is, 2048); assertNotSame(is, bis); assertSame(bis, IOUtils.buffer(bis)); assertSame(bis, IOUtils.buffer(bis, 1024)); }
@Test public void testAsBufferedOutputStreamWithBufferSize() { final OutputStream os = new OutputStream() { @Override public void write(final int b) throws IOException { } }; final BufferedOutputStream bos = IOUtils.buffer(os, 2048); assertNotSame(os, bos); assertSame(bos, IOUtils.buffer(bos)); assertSame(bos, IOUtils.buffer(bos, 1024)); }
@Test public void testAsBufferedReaderWithBufferSize() { final Reader r = new Reader() { @Override public int read(final char[] cbuf, final int off, final int len) throws IOException { return 0; } @Override public void close() throws IOException { } }; final BufferedReader br = IOUtils.buffer(r, 2048); assertNotSame(r, br); assertSame(br, IOUtils.buffer(br)); assertSame(br, IOUtils.buffer(br, 1024)); }
@Test public void testAsBufferedWriterWithBufferSize() { final Writer w = new Writer() { @Override public void write(final int b) throws IOException { } @Override public void write(final char[] cbuf, final int off, final int len) throws IOException { } @Override public void flush() throws IOException { } @Override public void close() throws IOException { } }; final BufferedWriter bw = IOUtils.buffer(w, 2024); assertNotSame(w, bw); assertSame(bw, IOUtils.buffer(bw)); assertSame(bw, IOUtils.buffer(bw, 1024)); }
@Test public void testAsBufferedReader() { final Reader is = new Reader() { @Override public int read(final char[] cbuf, final int off, final int len) throws IOException { return 0; } @Override public void close() throws IOException { } }; final BufferedReader bis = IOUtils.buffer(is); assertNotSame(is, bis); assertSame(bis, IOUtils.buffer(bis)); }
@Test public void testAsBufferedWriter() { final Writer is = new Writer() { @Override public void write(final int b) throws IOException { } @Override public void write(final char[] cbuf, final int off, final int len) throws IOException { } @Override public void flush() throws IOException { } @Override public void close() throws IOException { } }; final BufferedWriter bis = IOUtils.buffer(is); assertNotSame(is, bis); assertSame(bis, IOUtils.buffer(bis)); }
@Test public void testAsBufferedNull() { try { IOUtils.buffer((InputStream) null); fail("Expected NullPointerException"); } catch (final NullPointerException npe) { // expected } try { IOUtils.buffer((OutputStream) null); fail("Expected NullPointerException"); } catch (final NullPointerException npe) { // expected } try { IOUtils.buffer((Reader) null); fail("Expected NullPointerException"); } catch (final NullPointerException npe) { // expected } try { IOUtils.buffer((Writer) null); fail("Expected NullPointerException"); } catch (final NullPointerException npe) { // expected } }
/** * {@inheritDoc} */ @Override public T read(InputStream inputStream, @Nullable String mimeType, @Nullable Charset charset) throws IOException { if (log.isLoggable(Level.FINEST)) { inputStream = IOUtils.buffer(inputStream); inputStream.mark(Integer.MAX_VALUE); String s = IOUtils.toString(inputStream, charset != null ? charset : StandardCharsets.UTF_8); log.log(Level.FINEST, "JSON: {0}", s); inputStream.reset(); } try { return objectMapper.readValue(inputStream, valueTypeRef); } catch (JsonMappingException jme) { if (jme.getCause() instanceof RuntimeException) { throw (RuntimeException) jme.getCause(); } else { throw jme; } } }
public void saveCertificate(X509Certificate certificate) { try { Writer writer = IOUtils.buffer(new FileWriter(new File("/tmp/cert-" + certificate.getSerialNumber() + ".crt"))); JcaPEMWriter pemWriter = new JcaPEMWriter(writer); try { pemWriter.writeObject(certificate); pemWriter.flush(); } finally { IOUtils.closeQuietly(pemWriter); } } catch (IOException e) { throw new Fido2RPRuntimeException("Failed to write root certificate"); } }
/** * Unpacks a pack packFile. * * @param packFile the pack packFile meta-data * @param packInputStream the pack input stream * @param target the target * @throws IOException for any I/O error * @throws InstallerException for any installer exception */ @Override public void unpack(PackFile packFile, InputStream packInputStream, File target) throws IOException, InstallerException { InputStream in = IOUtils.buffer(packInputStream); JarOutputStream jarOut = null; try { jarOut = new JarOutputStream(getTarget(packFile, target)); Pack200.Unpacker unpacker = createPack200Unpacker(packFile); unpacker.unpack(in, jarOut); } finally { IOUtils.closeQuietly(jarOut); IOUtils.closeQuietly(in); } postCopy(packFile); }
try (BufferedInputStream bis = IOUtils.buffer(uploadedFile.getInputStream())) { byte[] buf = new byte[5]; bis.mark(buf.length + 1);
try (BufferedInputStream bis = IOUtils.buffer(uploadedFile.getInputStream())) { byte[] buf = new byte[5]; bis.mark(buf.length + 1);