From 4fb4a056c52d3a96b6ef9a4ceb5ce39fc716a9aa Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 21 Apr 2011 17:40:13 +0000 Subject: It...builds? --- java/com/xagasoft/gats/GatsInteger.java | 102 ++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 java/com/xagasoft/gats/GatsInteger.java (limited to 'java/com/xagasoft/gats/GatsInteger.java') diff --git a/java/com/xagasoft/gats/GatsInteger.java b/java/com/xagasoft/gats/GatsInteger.java new file mode 100644 index 0000000..1093ab2 --- /dev/null +++ b/java/com/xagasoft/gats/GatsInteger.java @@ -0,0 +1,102 @@ +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 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; + } + } +}; + -- cgit v1.2.3