/** * Writes to wrapped buffer. */ @Override public void write(final int i) { wrapped.write(i); }
@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)); } }
@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)); }
@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)); }
/** * 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; }
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(); } }
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()); } }
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()); } }
/** * 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; }
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(); } }