Refine search
/** * 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(); } }
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); } }
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()); } }
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; }
@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)); }
@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)); }
@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)); }
@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)); }
@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)); }
@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())); }
@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())); }
@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)); }
@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(); }
@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())); }
@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())); }
@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())); }
@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())); }
@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())); }
@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())); }
@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())); }