/* * Copyright (C) 2007-2013 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.InputStream; import java.io.DataInputStream; /** * Facilitates reading GatsObjects from an InputStream. This doesn't really * inherit from InputStream, so maybe it would make more sense to call it * something else. Use the readObject function to read an entire object from * the InputStream. *
* At the moment this class will require all data to be available in continuous * read operations from the provided InputStream. This means that only complete * packets can be read from files on the disk, or that if a socket is provided * it is in blocking or synchronous I/O mode. In java, this should rarely be * an issue. *
* Each call to readObject returns a new GatsObject read from the InputStream or * null if nothing else could be read. While reading, all zero bytes discovered * in between packets will be considered padding and will be ignored. *
* Like with the GatsOutputStream, there are generally many small reads * performed during a single readObject operation, so it is a good idea to * provide some sort of buffered input stream. *@see com.xagasoft.gats.GatsOutputStream */ public class GatsInputStream { private InputStream is; private int iVer = 0; private int iLastSize = 0; public GatsInputStream( InputStream is ) { this.is = is; } /** * Reads an object from the input and returns it. */ public GatsObject readObject() throws java.io.IOException { do { iVer = is.read(); } while( iVer == 0 ); switch( iVer ) { case 1: DataInputStream dis = new DataInputStream( is ); iLastSize = dis.readInt(); return GatsObject.read( is ); } return null; } /** * Set after each call to readObject, this will tell you how many bytes * were actually read in to build the object that was returned. * @return Size in bytes of the most recently read packet. */ public int getLastReadPacketSize() { return iLastSize; } };