public static byte[] readBytes(Reader input) throws IOException { FastByteArrayOutputStream output = new FastByteArrayOutputStream(); copy(input, output); return output.toByteArray(); } public static byte[] readBytes(Reader input, int byteCount) throws IOException {
/** * Reads boundary from the input stream. */ public byte[] readBoundary() throws IOException { FastByteArrayOutputStream boundaryOutput = new FastByteArrayOutputStream(); byte b; // skip optional whitespaces while ((b = readByte()) <= ' ') { } boundaryOutput.write(b); // now read boundary chars while ((b = readByte()) != '\r') { boundaryOutput.write(b); } if (boundaryOutput.size() == 0) { throw new IOException("Problems with parsing request: invalid boundary"); } skipBytes(1); boundary = new byte[boundaryOutput.size() + 2]; System.arraycopy(boundaryOutput.toByteArray(), 0, boundary, 2, boundary.length - 2); boundary[0] = '\r'; boundary[1] = '\n'; return boundary; }
/** * Get a string representation of the entire buffer. */ @Override public String toString() { return out.getByteArrayStream().toString(); }
@Test void testFbat() throws IOException { FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream(); fbaos.write(173); fbaos.write(new byte[]{1, 2, 3}); fbaos.write(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 4, 3); byte[] result = fbaos.toByteArray(); byte[] expected = new byte[]{(byte) 173, 1, 2, 3, 5, 6, 7}; assertTrue(Arrays.equals(expected, result)); }
public static byte[] readBytes(InputStream inputStream) { try { FastByteArrayOutputStream byteArrayOutputStream = new FastByteArrayOutputStream(); byte[] buffer = new byte[4096]; int read; while ((read = inputStream.read(buffer)) > -1) { byteArrayOutputStream.write(buffer, 0, read); } byteArrayOutputStream.flush(); byteArrayOutputStream.close(); return byteArrayOutputStream.toByteArray(); } catch (IOException e) { throw new BaseException(e, "Failed to read from InputStream: %s", e.getMessage()); } }
/** * @see java.io.ByteArrayOutputStream#toString() */ @Override public String toString() { return new String(toByteArray()); }
protected String readDataHeaderString(final String encoding) throws IOException { FastByteArrayOutputStream data = new FastByteArrayOutputStream(); byte b; while (true) { // end marker byte on offset +0 and +2 must be 13 if ((b = readByte()) != '\r') { data.write(b); continue; } mark(4); skipBytes(1); int i = read(); if (i == -1) { // reached end of stream return null; } if (i == '\r') { reset(); break; } reset(); data.write(b); } skipBytes(3); if (encoding != null) { return data.toString(encoding); } else { return data.toString(); } }
FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream(); assertEquals("one ctor!one ctor!", fbaos.toString()); // clone ctor calls super ctor, fbaos.reset(); assertEquals("REPLACED VIRTUAL! jodd.proxetta.fixtures.inv.Two * one!173>overriden sub", fbaos.toString()); fbaos.reset(); "REPLACED STATIC! one * jodd/proxetta/fixtures/inv/Two * " + "example2 * void example2() * jodd.proxetta.fixtures.inv.One * " + "jodd.proxetta.fixtures.inv.One" + ProxettaNames.invokeProxyClassNameSuffix + "!15013static: 4", fbaos.toString()); fbaos.reset(); assertEquals("state = REPLACED ctor!", fbaos.toString()); fbaos.reset();
public FastByteArrayServletOutputStream() { wrapped = new FastByteArrayOutputStream(); }
/** * Writes to wrapped buffer. */ @Override public void write(final int i) { wrapped.write(i); }
/** * Returns current buffer size. */ @Override public int getBufferSize() { return out.wrapped.size(); }
public void reset() { wrapped.reset(); }
@Test void testFbatSingle() throws IOException { FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream(2); fbaos.write(73); fbaos.write(74); fbaos.write(75); fbaos.write(76); fbaos.write(77); byte[] result = fbaos.toByteArray(); byte[] expected = new byte[]{73, 74, 75, 76, 77}; assertTrue(Arrays.equals(expected, result)); } }
public static byte[] readBytes(InputStream inputStream) { try { FastByteArrayOutputStream byteArrayOutputStream = new FastByteArrayOutputStream(); byte[] buffer = new byte[4096]; int read; while ((read = inputStream.read(buffer)) > -1) { byteArrayOutputStream.write(buffer, 0, read); } byteArrayOutputStream.flush(); byteArrayOutputStream.close(); return byteArrayOutputStream.toByteArray(); } catch (IOException e) { throw new BaseException(e, "Failed to read from InputStream: %s", e.getMessage()); } }
/** * @see java.io.ByteArrayOutputStream#toString(String) */ public String toString(String enc) throws UnsupportedEncodingException { return new String(toByteArray(), enc); }
protected String readDataHeaderString(final String encoding) throws IOException { FastByteArrayOutputStream data = new FastByteArrayOutputStream(); byte b; while (true) { // end marker byte on offset +0 and +2 must be 13 if ((b = readByte()) != '\r') { data.write(b); continue; } mark(4); skipBytes(1); int i = read(); if (i == -1) { // reached end of stream return null; } if (i == '\r') { reset(); break; } reset(); data.write(b); } skipBytes(3); if (encoding != null) { return data.toString(encoding); } else { return data.toString(); } }
/** * Returns new {@link FastByteArrayOutputStream} using default IO buffer size. * * @return new {@link FastByteArrayOutputStream} using default IO buffer size. */ private static FastByteArrayOutputStream createFastByteArrayOutputStream() { return new FastByteArrayOutputStream(bufferSize()); }
public static byte[] readBytes(Reader input, String encoding) throws IOException { FastByteArrayOutputStream output = new FastByteArrayOutputStream(); copy(input, output, encoding); return output.toByteArray(); } public static byte[] readBytes(Reader input, String encoding, int byteCount) throws IOException {
@Test void testFbat2() throws IOException { FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream(2); fbaos.write(173); fbaos.write(new byte[]{1, 2, 3}); fbaos.write(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 4, 3); byte[] result = fbaos.toByteArray(); byte[] expected = new byte[]{(byte) 173, 1, 2, 3, 5, 6, 7}; assertTrue(Arrays.equals(expected, result)); }
/** * Reads boundary from the input stream. */ public byte[] readBoundary() throws IOException { FastByteArrayOutputStream boundaryOutput = new FastByteArrayOutputStream(); byte b; // skip optional whitespaces while ((b = readByte()) <= ' ') { } boundaryOutput.write(b); // now read boundary chars while ((b = readByte()) != '\r') { boundaryOutput.write(b); } if (boundaryOutput.size() == 0) { throw new IOException("Problems with parsing request: invalid boundary"); } skipBytes(1); boundary = new byte[boundaryOutput.size() + 2]; System.arraycopy(boundaryOutput.toByteArray(), 0, boundary, 2, boundary.length - 2); boundary[0] = '\r'; boundary[1] = '\n'; return boundary; }