Java Code Examples for java.io.ByteArrayOutputStream

The following are top voted examples for showing how to use java.io.ByteArrayOutputStream. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to product more good examples.

Example 1
Project: arduino-android   File: Template.java View source code 9 votes vote down vote up
private String readTextFile(InputStream inputStream) 
 {
 	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 	byte buf[] = new byte[1024];
 	int len;
 	try 
 	{
 		while ((len = inputStream.read(buf)) != -1) 
 		{
 	       outputStream.write(buf, 0, len);
 	    }
 	    outputStream.close();
 	    inputStream.close();
 	} 
 	catch (IOException e) 
 	{
 	}
 	return outputStream.toString();
}
 
Example 2
Project: sslcertx   File: CompressOutputStream.java View source code 6 votes vote down vote up
@Override
public void write(byte[] bytes, int off, int len) throws IOException{
    if (len > MAX_PACKET_LENGTH) {
        for(;;) {
           int bytesToWrite= Math.min(len, MAX_PACKET_LENGTH);
           write(bytes, off, bytesToWrite);
           off += bytesToWrite;
           len -= bytesToWrite;
           if(len == 0)
               return;
        }
    }

    int compressedLength = len;
    int uncompressedLength = 0;

    if (bytes.length > MIN_COMPRESSION_SIZE) {
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          DeflaterOutputStream deflater = new DeflaterOutputStream(baos);
          deflater.write(bytes, off, len);
          deflater.finish();
          deflater.close();
          byte[] compressedBytes = baos.toByteArray();
          baos.close();
          if (compressedBytes.length < (int)(MIN_COMPRESSION_RATIO *len)) {
             compressedLength = compressedBytes.length;
             uncompressedLength = len;
             bytes = compressedBytes;
             off = 0;
          }
    }

    header[0] = (byte)(compressedLength & 0xff);
    header[1] = (byte)((compressedLength >> 8) & 0xff);
    header[2] = (byte)((compressedLength >> 16) & 0xff);
    header[3] = (byte) seqNo++;
    header[4] = (byte)(uncompressedLength & 0xff);
    header[5] = (byte)((uncompressedLength >> 8) & 0xff);
    header[6] = (byte)((uncompressedLength >> 16) & 0xff);

    baseStream.write(header);
    baseStream.write(bytes, off, compressedLength);

}
 
Example 3
Project: jena   File: ThriftConverter.java View source code 6 votes vote down vote up
public static byte[] toBytes(RDF_Term term) throws TException {
    ByteArrayOutputStream output = getOutputStream();
    output.reset();

    TProtocol protocol = getOutputProtocol();
    term.write(protocol);

    return output.toByteArray();
}
 
Example 4
Project: jboss-dmr   File: StringModelValueTest.java View source code 6 votes vote down vote up
@Test
public void testWriteExternal() throws IOException {
    final Random r = new Random(1234L);
    final StringBuilder b = new StringBuilder(80000);
    for (int i = 0; i < 4000; i ++) {
        b.append((char) r.nextInt());
    }
    final String smallString = b.toString();
    final StringModelValue small = new StringModelValue(smallString);
    for (int i = 0; i < 76000; i ++) {
        b.append((char) r.nextInt());
    }
    final String largeString = b.toString();
    final StringModelValue large = new StringModelValue(largeString);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream(200000);
    final DataOutputStream dos = new DataOutputStream(baos);
    small.writeExternal(dos);
    large.writeExternal(dos);
    dos.flush();
    baos.flush();
    final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    final DataInputStream dis = new DataInputStream(bais);
    assertEquals(dis.readUnsignedByte(), 's');
    final StringModelValue smallIn = new StringModelValue('s', dis);
    assertEquals(smallString, smallIn.asString());
    assertEquals(dis.readUnsignedByte(), 'S');
    final StringModelValue largeIn = new StringModelValue('S', dis);
    assertEquals(largeString, largeIn.asString());
}
 
Example 5
Project: Disco-Build-System   File: TestPrintUtils.java View source code 6 votes vote down vote up
/**
 * Helper method for comparing a byte array and a string
 * @param baos The ByteArrayOutStream to compare against
 * @param string The String to compare against
 * @return True if they're same, else false.
 */
private boolean compareByteArray(ByteArrayOutputStream baos, String string) {
	
	if (baos.size() != string.length()) {
		return false;
	}
	
	byte bytes[] = baos.toByteArray();
	for (int i = 0; i != bytes.length; i++) {
		if (bytes[i] != string.charAt(i)) {
			return false;
		}
	}
	
	return true;
}
 
Example 6
Project: jentrata-msh   File: InputStreamDataSource.java View source code 6 votes vote down vote up
/**
    * Returns new byte array from a new BytesArrayOutStream always.
 * Logically, it is required to copy binary content from the input stream to output stream.
 * But it is found the same result without touching binary content.  
    * 
    * @return the content type when this data source created.
    */
public OutputStream getOutputStream() throws IOException {
    if (_os == null) {
    	ByteArrayOutputStream baos = new ByteArrayOutputStream();
    	/*
    	byte[] b = new byte[8192]; 
    	int count;
    	while ((count = _is.read(b)) > 0) { 
    		baos.write(b, 0, count); 
    		baos.flush(); 
    	} 
    	*/	    	
    	baos.writeTo(_os);
    }
    return _os;	
	
}