ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } return result.toString("UTF-8");
@Override public String convert(Properties source) { try { ByteArrayOutputStream os = new ByteArrayOutputStream(256); source.store(os, null); return os.toString("ISO-8859-1"); } catch (IOException ex) { // Should never happen. throw new IllegalArgumentException("Failed to store [" + source + "] into String", ex); } }
public static byte[] isToBytes(InputStream is) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int read; try { while ((read = is.read(buffer)) != -1) { os.write(buffer, 0, read); } } finally { is.close(); } return os.toByteArray(); }
/** Serializes and deserializes the specified object. */ @SuppressWarnings("unchecked") static <T> T reserialize(T object) { checkNotNull(object); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); try { ObjectOutputStream out = new ObjectOutputStream(bytes); out.writeObject(object); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray())); return (T) in.readObject(); } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); } }
private Object serializeValue(SerializationDelegate serialization, Object storeValue) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { serialization.serialize(storeValue, out); return out.toByteArray(); } finally { out.close(); } }
public static byte[] readNullTerminatedBytes(byte[] data, int index) { ByteArrayOutputStream out = new ByteArrayOutputStream(); for (int i = index; i < data.length; i++) { byte item = data[i]; if (item == MSC.NULL_TERMINATED_STRING_DELIMITER) { break; } out.write(item); } return out.toByteArray(); }
private void transformAndMarshal(Object graph, Result result) throws IOException { try { ByteArrayOutputStream os = new ByteArrayOutputStream(1024); marshalOutputStream(graph, os); ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); Transformer transformer = this.transformerFactory.newTransformer(); transformer.transform(new StreamSource(is), result); } catch (TransformerException ex) { throw new MarshallingFailureException( "Could not transform to [" + ClassUtils.getShortName(result.getClass()) + "]", ex); } }
@Test public void testMissingValue() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream stream = new PrintStream(baos); System.setOut(stream); itf.execute("hello", "-n"); assertThat(baos.toString()) .contains("The option 'name' requires a value"); }
public static String toString(InputStream inputStream) throws IOException { ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } return result.toString("UTF-8"); }
@GwtIncompatible // java serialization not supported in GWT. private static byte[] serializeWithBackReference(Object original, int handleOffset) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(original); byte[] handle = toByteArray(baseWireHandle + handleOffset); byte[] ref = prepended(TC_REFERENCE, handle); bos.write(ref); return bos.toByteArray(); }
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = null; try { out = new ObjectOutputStream(bos); out.writeObject(yourObject); out.flush(); byte[] yourBytes = bos.toByteArray(); ... } finally { try { bos.close(); } catch (IOException ex) { // ignore close exception } }
/** * Attempts to lift the security restriction on the underlying channel. * This requires the administer privilege on the server. * * @throws SecurityException * If we fail to upgrade the connection. */ public void upgrade() { ByteArrayOutputStream out = new ByteArrayOutputStream(); if (execute(Arrays.asList("groovy", "="), new ByteArrayInputStream("hudson.remoting.Channel.current().setRestricted(false)".getBytes()), out,out)!=0) throw new SecurityException(out.toString()); // failed to upgrade }
public byte[] convert(String javaFile) throws JadxException { try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream errOut = new ByteArrayOutputStream()) { DxContext context = new DxContext(out, errOut); DxArgs args = new DxArgs(context, "-", new String[]{javaFile}); int result = (new Main(context)).runDx(args); dxErrors = errOut.toString(CHARSET_NAME); if (result != 0) { throw new JadxException("Java to dex conversion error, code: " + result); } return out.toByteArray(); } catch (Exception e) { throw new JadxException("dx exception: " + e.getMessage(), e); } }
HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(new HttpGet(URL)); StatusLine statusLine = response.getStatusLine(); if(statusLine.getStatusCode() == HttpStatus.SC_OK){ ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); String responseString = out.toString(); out.close(); //..more logic } else{ //Closes the connection. response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); }
@SuppressWarnings("unchecked") static <T> T reserialize(T object) { try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bytes); out.writeObject(object); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray())); return (T) in.readObject(); } catch (IOException | ClassNotFoundException e) { Helpers.fail(e, e.getMessage()); } throw new AssertionError("not reachable"); }
private static byte[] readAllBytes(InputStream input) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); int numRead; byte[] buffer = new byte[16384]; while ((numRead = input.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, numRead); } return out.toByteArray(); }
/** * @return a byte[] containing the information contained in the * specified InputStream. * @throws java.io.IOException */ public static byte[] getBytes(InputStream input) throws IOException { ByteArrayOutputStream result = new ByteArrayOutputStream(); copy(input, result); result.close(); return result.toByteArray(); }
private String readCommand(ByteBuffer byteBuffer) { ByteArrayOutputStream command = new ByteArrayOutputStream(256); while (byteBuffer.remaining() > 0 && !tryConsumeEndOfLine(byteBuffer)) { command.write(byteBuffer.get()); } return new String(command.toByteArray(), StandardCharsets.UTF_8); }
public void testCopyChannel() throws IOException { byte[] expected = newPreFilledByteArray(100); ByteArrayOutputStream out = new ByteArrayOutputStream(); WritableByteChannel outChannel = Channels.newChannel(out); ReadableByteChannel inChannel = Channels.newChannel(new ByteArrayInputStream(expected)); ByteStreams.copy(inChannel, outChannel); assertEquals(expected, out.toByteArray()); }
@Test public void testMissingOption() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream stream = new PrintStream(baos); System.setOut(stream); itf.execute("hello"); assertThat(baos.toString()) .contains("The option", "name", "is required"); }