package com.xagasoft.gats; import java.io.OutputStream; import java.io.InputStream; public class GatsInteger extends GatsObject { private long iValue = 0; public GatsInteger() { } public GatsInteger( long iValue ) { this.iValue = iValue; } public long getValue() { return iValue; } public void setValue( long iValue ) { this.iValue = iValue; } public String toString() { return "" + iValue; } public int getType() { return GatsObject.INTEGER; }; public void read( InputStream is, char cType ) throws java.io.IOException { iValue = readPackedInt( is ); } public void write( OutputStream os ) throws java.io.IOException { os.write( (int)'i' ); writePackedInt( os, iValue ); } /** * Possible TODO: have this return a Number, and construct either a Long * or BigInteger when appropriate. */ public static long readPackedInt( InputStream is ) throws java.io.IOException { int b; long rOut = 0; boolean bNeg; b = is.read(); bNeg = (b&0x40) == 0x40; rOut |= b&0x3F; int c = 0; while( (b&0x80) == 0x80 ) { b = is.read(); rOut |= (long)(b&0x7F) << (6+7*(c++)); } if( bNeg ) return -rOut; return rOut; } public static void writePackedInt( OutputStream os, long iIn ) throws java.io.IOException { int b; if( iIn < 0 ) { iIn = -iIn; b = (int)(iIn&0x3F); if( iIn > b ) b |= 0x80 | 0x40; else b |= 0x40; } else { b = (int)(iIn&0x3F); if( iIn > b ) b |= 0x80; } os.write( b ); iIn = iIn >> 6; while( iIn > 0 ) { b = (int)(iIn&0x7F); if( iIn > b ) b |= 0x80; os.write( b ); iIn = iIn >> 7; } } };