/* * Copyright (C) 2007-2012 Xagasoft, All rights reserved. * * This file is part of the libgats library and is released under the * terms of the license contained in the file LICENSE. */ package com.xagasoft.gats; import java.io.OutputStream; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; /** * Facilitates writing GatsObjects to an OutputStream. This doesn't really * inherit from OutputStream, so maybe it would make more sense to call it * something else, but this is how it is right now. Use the writeObject * function to write any given GatsObject to the OutputStream. *

* Each time you write an object with this class it actually writes a Gats * Packet data structure which consists of a 5 byte header followed by the * encoded GatsObject data. In the packet header is information about which * version of Gats is in use, which options are enabled, etc. This ensures * that Gats is backward compatible. *

* According to the GATS standard only fully formed Gats packets may be written * to files or sockets to ensure integrity and context. Since each packet can * only contain one GatsObject that means that each writeObject call should * write one fully formed message or data structure to ensure maximum * efficiency. *

* The OutputStream is written to frequently, and often in small increments, so * it is highly advisable to pass in a BufferedOutputStream or similar structure * to ensure maximum performance. *

* The Gats format stipulates that all zero bytes found in between packets are * simply ignored, which allows you to pad streams of sequential Gats objects * if necessary. This can be handy in some encoding/compression/encryption * schemes. *@see com.xagasoft.gats.GatsInputStream */ public class GatsOutputStream { private OutputStream os; public GatsOutputStream( OutputStream os ) { this.os = os; } /** * Write an object to the provided output stream. *@return The total number of bytes written. */ public int writeObject( GatsObject obj ) throws java.io.IOException { ByteArrayOutputStream bos1 = new ByteArrayOutputStream(); obj.write( bos1 ); ByteArrayOutputStream bos2 = new ByteArrayOutputStream( 5 + bos1.size() ); DataOutputStream dos = new DataOutputStream( bos2 ); dos.writeByte( 1 ); dos.writeInt( bos1.size()+5 ); bos2.write( bos1.toByteArray() ); os.write( bos2.toByteArray() ); return bos1.size()+5; } };