public FastByteArrayServletOutputStream() { wrapped = new FastByteArrayOutputStream(); }
/** * 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()); }
/** * Serialize an object to byte array. */ public static byte[] objectToByteArray(Object obj) throws IOException { FastByteArrayOutputStream bos = new FastByteArrayOutputStream(); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(bos); oos.writeObject(obj); } finally { StreamUtil.close(oos); } return bos.toByteArray(); }
/** * Returns byte content of the attachment. * * @return byte array with content of the attachment. */ public byte[] toByteArray() { final FastByteArrayOutputStream out; if (size != -1) { out = new FastByteArrayOutputStream(size); } else { out = new FastByteArrayOutputStream(); } writeToStream(out); return out.toByteArray(); }
public static byte[] readBytes(InputStream input) throws IOException { FastByteArrayOutputStream output = new FastByteArrayOutputStream(); copy(input, output); return output.toByteArray(); } public static byte[] readBytes(InputStream input, int byteCount) throws IOException {
public static byte[] readBytes(Reader input, int byteCount) throws IOException { FastByteArrayOutputStream output = new FastByteArrayOutputStream(); copy(input, output, byteCount); return output.toByteArray(); } public static byte[] readBytes(Reader input, String encoding) throws IOException {
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 {
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 {
public static byte[] readBytes(InputStream input, int byteCount) throws IOException { FastByteArrayOutputStream output = new FastByteArrayOutputStream(); copy(input, output, byteCount); return output.toByteArray(); }
public static byte[] readBytes(Reader input, String encoding, int byteCount) throws IOException { FastByteArrayOutputStream output = new FastByteArrayOutputStream(); copy(input, output, encoding, byteCount); return output.toByteArray(); }
/** * Create object copy using serialization mechanism. */ public static <T extends Serializable> T cloneViaSerialization(T obj) throws IOException, ClassNotFoundException { FastByteArrayOutputStream bos = new FastByteArrayOutputStream(); ObjectOutputStream out = null; ObjectInputStream in = null; Object objCopy = null; try { out = new ObjectOutputStream(bos); out.writeObject(obj); out.flush(); byte[] bytes = bos.toByteArray(); in = new ObjectInputStream(new ByteArrayInputStream(bytes)); objCopy = in.readObject(); } finally { StreamUtil.close(out); StreamUtil.close(in); } return (T) objCopy; }
/** * Serialize an object to byte array. */ public static byte[] objectToByteArray(final Object obj) throws IOException { FastByteArrayOutputStream bos = new FastByteArrayOutputStream(); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(bos); oos.writeObject(obj); } finally { StreamUtil.close(oos); } return bos.toByteArray(); }
/** * Create object copy using serialization mechanism. */ public static <T extends Serializable> T cloneViaSerialization(final T obj) throws IOException, ClassNotFoundException { FastByteArrayOutputStream bos = new FastByteArrayOutputStream(); ObjectOutputStream out = null; ObjectInputStream in = null; Object objCopy = null; try { out = new ObjectOutputStream(bos); out.writeObject(obj); out.flush(); byte[] bytes = bos.toByteArray(); in = new ObjectInputStream(new ByteArrayInputStream(bytes)); objCopy = in.readObject(); } finally { StreamUtil.close(out); StreamUtil.close(in); } return (T) objCopy; }
/** * Reads data from input stream into byte array and stores file size. */ @Override public void processStream() throws IOException { FastByteArrayOutputStream out = new FastByteArrayOutputStream(); size = 0; if (maxFileSize == -1) { size += input.copyAll(out); } else { size += input.copyMax(out, maxFileSize + 1); // one more byte to detect larger files if (size > maxFileSize) { fileTooBig = true; valid = false; input.skipToBoundary(); return; } } data = out.toByteArray(); size = data.length; valid = true; }
/** * 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; }
@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)); }
@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 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)); } }
FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream();
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(); } }