Buffer.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 package com.jcraft.jsch;
00031 
00047 public class Buffer{
00048   final byte[] tmp=new byte[4];
00049   byte[] buffer;
00050   int index;
00051   int s;
00052 
00056   public Buffer(int size){
00057     buffer=new byte[size];
00058     index=0;
00059     s=0;
00060   }
00061 
00065   public Buffer(byte[] buffer){
00066     this.buffer=buffer;
00067     index=0;
00068     s=0;
00069   }
00070 
00074   public Buffer(){ this(1024*10*2); }
00075 
00079   public void putByte(byte foo){
00080     buffer[index++]=foo;
00081   }
00082 
00088   public void putByte(byte[] foo) {
00089     putByte(foo, 0, foo.length);
00090   }
00091 
00097   public void putByte(byte[] foo, int begin, int length) {
00098     System.arraycopy(foo, begin, buffer, index, length);
00099     index+=length;
00100   }
00101 
00106   public void putString(byte[] foo){
00107     putString(foo, 0, foo.length);
00108   }
00119   public void putString(byte[] foo, int begin, int length) {
00120     putInt(length);
00121     putByte(foo, begin, length);
00122   }
00123 
00127   public void putInt(int val) {
00128     tmp[0]=(byte)(val >>> 24);
00129     tmp[1]=(byte)(val >>> 16);
00130     tmp[2]=(byte)(val >>> 8);
00131     tmp[3]=(byte)(val);
00132     System.arraycopy(tmp, 0, buffer, index, 4);
00133     index+=4;
00134   }
00135 
00139   public void putLong(long val) {
00140     tmp[0]=(byte)(val >>> 56);
00141     tmp[1]=(byte)(val >>> 48);
00142     tmp[2]=(byte)(val >>> 40);
00143     tmp[3]=(byte)(val >>> 32);
00144     System.arraycopy(tmp, 0, buffer, index, 4);
00145     tmp[0]=(byte)(val >>> 24);
00146     tmp[1]=(byte)(val >>> 16);
00147     tmp[2]=(byte)(val >>> 8);
00148     tmp[3]=(byte)(val);
00149     System.arraycopy(tmp, 0, buffer, index+4, 4);
00150     index+=8;
00151   }
00152 
00156   void skip(int n) {
00157     index+=n;
00158   }
00159 
00164   void putPad(int n) {
00165     while(n>0){
00166       buffer[index++]=(byte)0;
00167       n--;
00168     }
00169   }
00170 
00177   public void putMPInt(byte[] foo){
00178     // wouldn't this give an ArrayIndexOutOfBoundsException
00179     // for 0 = new byte[]{}?   -- P.E.
00180     int i=foo.length;
00181     if((foo[0]&0x80)!=0){
00182       i++;
00183       putInt(i);
00184       putByte((byte)0);
00185     }
00186     else{
00187       putInt(i);
00188     }
00189     putByte(foo);
00190   }
00191 
00196   public int getLength(){
00197     return index-s;
00198   }
00199 
00203   public int getOffSet(){
00204     return s;
00205   }
00213   public void setOffSet(int s){
00214     this.s=s;
00215   }
00216 
00221   public long getLong(){
00222     long foo = getInt()&0xffffffffL;
00223     foo = ((foo<<32)) | (getInt()&0xffffffffL);
00224     return foo;
00225   }
00226 
00230   public int getInt(){
00231     int foo = getShort();
00232     foo = ((foo<<16)&0xffff0000) | (getShort()&0xffff);
00233     return foo;
00234   }
00235 
00239   public long getUInt(){
00240     long foo = 0L;
00241     long bar = 0L;
00242     foo = getByte();
00243     foo = ((foo<<8)&0xff00)|(getByte()&0xff);
00244     bar = getByte();
00245     bar = ((bar<<8)&0xff00)|(getByte()&0xff);
00246     foo = ((foo<<16)&0xffff0000) | (bar&0xffff);
00247     return foo;
00248   }
00249 
00253   int getShort() {
00254     int foo = getByte();
00255     foo = ((foo<<8)&0xff00)|(getByte()&0xff);
00256     return foo;
00257   }
00258 
00262   public int getByte() {
00263     return (buffer[s++]&0xff);
00264   }
00265 
00271   public void getByte(byte[] foo) {
00272     getByte(foo, 0, foo.length);
00273   }
00274 
00281   void getByte(byte[] foo, int start, int len) {
00282     System.arraycopy(buffer, s, foo, start, len); 
00283     s+=len;
00284   }
00285 
00297   public int getByte(int len) {
00298     int foo=s;
00299     s+=len;
00300     return foo;
00301   }
00302 
00307   public byte[] getMPInt() {
00308     int i=getInt();  // uint32
00309     if(i<0 ||  // bigger than 0x7fffffff
00310        i>8*1024){
00311       // TODO: an exception should be thrown.
00312       i = 8*1024; // the session will be broken, but working around OOME.
00313     }
00314     byte[] foo=new byte[i];
00315     getByte(foo, 0, i);
00316     return foo;
00317   }
00318 
00324   public byte[] getMPIntBits() {
00325     int bits=getInt();
00326     int bytes=(bits+7)/8;
00327     byte[] foo=new byte[bytes];
00328     getByte(foo, 0, bytes);
00329     if((foo[0]&0x80)!=0){
00330       byte[] bar=new byte[foo.length+1];
00331       bar[0]=0; // ??
00332       System.arraycopy(foo, 0, bar, 1, foo.length);
00333       foo=bar;
00334     }
00335     return foo;
00336   }
00337 
00343   public byte[] getString() {
00344     int i = getInt();  // uint32
00345     if(i<0 ||  // bigger than 0x7fffffff
00346        i>256*1024){
00347       // TODO: an exception should be thrown.
00348       i = 256*1024; // the session will be broken, but working around OOME.
00349     }
00350     byte[] foo=new byte[i];
00351     getByte(foo, 0, i);
00352     return foo;
00353   }
00354 
00366   byte[] getString(int[]start, int[]len) {
00367     int i=getInt();
00368     start[0]=getByte(i);
00369     len[0]=i;
00370     return buffer;
00371   }
00372 
00376   public void reset(){
00377     index=0;
00378     s=0;
00379   }
00380 
00386   public void shift(){
00387     if(s==0)return;
00388     System.arraycopy(buffer, s, buffer, 0, index-s);
00389     index=index-s;
00390     s=0;
00391   }
00392 
00396   void rewind(){
00397     s=0;
00398   }
00399 
00407   byte getCommand(){
00408     return buffer[5];
00409   }
00410 
00411 
00420   void checkFreeSize(int n){
00421     if(buffer.length<index+n){
00422       byte[] tmp = new byte[buffer.length*2];
00423       System.arraycopy(buffer, 0, tmp, 0, index);
00424       buffer = tmp;
00425     }
00426   }
00427 
00428 /*
00429   static String[] chars={
00430     "0","1","2","3","4","5","6","7","8","9", "a","b","c","d","e","f"
00431   };
00432   static void dump_buffer(){
00433     int foo;
00434     for(int i=0; i<tmp_buffer_index; i++){
00435         foo=tmp_buffer[i]&0xff;
00436     System.err.print(chars[(foo>>>4)&0xf]);
00437     System.err.print(chars[foo&0xf]);
00438         if(i%16==15){
00439           System.err.println("");
00440       continue;
00441     }
00442         if(i>0 && i%2==1){
00443           System.err.print(" ");
00444     }
00445     }
00446     System.err.println("");
00447   }
00448   static void dump(byte[] b){
00449     dump(b, 0, b.length);
00450   }
00451   static void dump(byte[] b, int s, int l){
00452     for(int i=s; i<s+l; i++){
00453       System.err.print(Integer.toHexString(b[i]&0xff)+":");
00454     }
00455     System.err.println("");
00456   }
00457 */
00458 
00459 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1