ByteArrayOutputStream toByteArray - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for ByteArrayOutputStream toByteArray method.

This code example shows how to use the following methods:toByteArray
    throws IOException; 
 
  @Override 
  public final void write(DataOutput out) throws IOException {
    if (compressed == null) {
      ByteArrayOutputStream deflated = new ByteArrayOutputStream();
      Deflater deflater = new Deflater(Deflater.BEST_SPEED);
      DataOutputStream dout =
        new DataOutputStream(new DeflaterOutputStream(deflated, deflater));
      writeCompressed(dout);
      dout.close();
      deflater.end();
      compressed = deflated.toByteArray();
    } 
    out.writeInt(compressed.length);
    out.write(compressed);
  } 
 
  /** Subclasses implement this instead of {@link #write(DataOutput)}. */ 
  protected abstract void writeCompressed(DataOutput out) throws IOException;
10
CodeRank
This code example shows how to use the following methods:toByteArray
 
  private static long OFFSET_IN_BLOCK = 12345L;
  private static int SEQNO = 54321;
 
  private byte[] prepareFakePacket(byte[] data, byte[] sums) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
     
    int packetLen = data.length + sums.length + 4;
    PacketHeader header = new PacketHeader(
        packetLen, OFFSET_IN_BLOCK, SEQNO, false, data.length, false);
    header.write(dos);
     
    dos.write(sums);
    dos.write(data);
    dos.flush();
    return baos.toByteArray();
  } 
   
  private static byte[] remainingAsArray(ByteBuffer buf) {

10
CodeRank
This code example shows how to use the following methods:toByteArray
 
	public byte[] toStream(final Object iObject) throws IOException {
		if (iObject == null)
			return new byte[0]; 
 
		final ByteArrayOutputStream os = new ByteArrayOutputStream();
		final ObjectOutputStream oos = new ObjectOutputStream(os);
		oos.writeObject(iObject);
		oos.close();
 
		return os.toByteArray();
	} 
} 
10
CodeRank
This code example shows how to use the following methods:toByteArray
This code example shows how to use the following methods:close, toByteArray
        assertEquals(request, decodedRequest);
        assertEquals(request.hashCode(), decodedRequest.hashCode());
    } 
 
    private MultiTransactionRecord codeDecode(MultiTransactionRecord request) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
        request.serialize(boa, "request");
        baos.close();
        ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
        bb.rewind();
 
        BinaryInputArchive bia = BinaryInputArchive.getArchive(new ByteBufferInputStream(bb));
        MultiTransactionRecord decodedRequest = new MultiTransactionRecord();
        decodedRequest.deserialize(bia, "request");
        return decodedRequest;
    } 
} 
8
CodeRank
This code example shows how to use the following methods:close, toByteArray
//    RequestType request2 = _inner.requestFromBytes(uncompressedBytes); 
//    if(!request.equals(request2)) { 
//      throw new IllegalArgumentException(); 
//    } 
 
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
    try { 
      SnappyOutputStream snappyOutputStream = new SnappyOutputStream(baos);
      snappyOutputStream.write(uncompressedBytes);
      snappyOutputStream.close();
      baos.close();
    } catch (IOException e) {
      // This should not happen 
      logger.error("Could not compress sensei request ", e);
    } 
 
    return baos.toByteArray();
  } 
 

8
CodeRank
This code example shows how to use the following methods:close, toByteArray
    @Override 
    public byte[] encodeObject(final Object obj) throws IOException {
        ByteArrayOutputStream baos = null;
        HessianOutput output = null;
        try { 
            baos = new ByteArrayOutputStream(1024);
            output = new HessianOutput(baos);
            output.startCall();
            output.writeObject(obj);
            output.completeCall();
        } 
        catch (final IOException ex) {
            throw ex;
        } 
        finally { 
            if (output != null) {
                try { 
                    baos.close();
                } 
                catch (final IOException ex) {

8
CodeRank
This code example shows how to use the following methods:toByteArray
		assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
	} 
 
	@Test
	public void marshalStreamResultOutputStream() throws Exception {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		StreamResult result = new StreamResult(os);
		marshaller.marshal(flights, result);
		assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING,
				new String(os.toByteArray(), "UTF-8"));
	} 
 
	@Test
	public void marshalStaxResultStreamWriter() throws Exception {
		XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
		StringWriter writer = new StringWriter();
		XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
		Result result = StaxUtils.createStaxResult(streamWriter);
		marshaller.marshal(flights, result);
		assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
8
CodeRank
This code example shows how to use the following methods:toByteArray
    Assert.assertEquals(-1, comparator.compare(thingie1Bytes, 0, thingie1Bytes.length, thingie2Bytes, 0, thingie2Bytes.length));
  } 
 
  private byte[] getByteArrayBytes(SortableBytes thingie1) throws IOException
  { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(baos);
    thingie1.toBytesWritable().write(out);
    out.flush();
    return baos.toByteArray();
  } 
 
  private byte[] toBytes(String string)
  { 
    return string.getBytes(Charsets.UTF_8);
  } 
 
  private String fromBytes(byte[] bytes)
  { 
    return new String(bytes, Charsets.UTF_8);
10
CodeRank
This code example shows how to use the following methods:toByteArray
        MarshallerFactory marshallerFactory = createMarshallerFactory();
        MarshallingConfiguration configuration = createMarshallingConfig();
 
        EmbeddedChannel ch = new EmbeddedChannel(createDecoder(Integer.MAX_VALUE));
 
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        Marshaller marshaller = marshallerFactory.createMarshaller(configuration);
        marshaller.start(Marshalling.createByteOutput(bout));
        marshaller.writeObject(testObject);
        marshaller.finish();
        marshaller.close();
 
        byte[] testBytes = bout.toByteArray();
 
        ch.writeInbound(input(testBytes));
        assertTrue(ch.finish());
 
        String unmarshalled = ch.readInbound();
 
        assertEquals(testObject, unmarshalled);
8
CodeRank

Related ByteArrayOutputStream toByteArray Questions & Answers:

toByteArray Method Overview
Creates a newly allocated byte array.
See Code Examples for other Java 8 ByteArrayOutputStream Methods: