1 2 29 30 package com.jcraft.jsch; 31 32 public class Buffer{ 33 final byte[] tmp=new byte[4]; 34 byte[] buffer; 35 int index; 36 int s; 37 public Buffer(int size){ 38 buffer=new byte[size]; 39 index=0; 40 s=0; 41 } 42 public Buffer(byte[] buffer){ 43 this.buffer=buffer; 44 index=0; 45 s=0; 46 } 47 public Buffer(){ this(1024*10*2); } 48 public void putByte(byte foo){ 49 buffer[index++]=foo; 50 } 51 public void putByte(byte[] foo) { 52 putByte(foo, 0, foo.length); 53 } 54 public void putByte(byte[] foo, int begin, int length) { 55 System.arraycopy(foo, begin, buffer, index, length); 56 index+=length; 57 } 58 public void putString(byte[] foo){ 59 putString(foo, 0, foo.length); 60 } 61 public void putString(byte[] foo, int begin, int length) { 62 putInt(length); 63 putByte(foo, begin, length); 64 } 65 public void putInt(int val) { 66 tmp[0]=(byte)(val >>> 24); 67 tmp[1]=(byte)(val >>> 16); 68 tmp[2]=(byte)(val >>> 8); 69 tmp[3]=(byte)(val); 70 System.arraycopy(tmp, 0, buffer, index, 4); 71 index+=4; 72 } 73 public void putLong(long val) { 74 tmp[0]=(byte)(val >>> 56); 75 tmp[1]=(byte)(val >>> 48); 76 tmp[2]=(byte)(val >>> 40); 77 tmp[3]=(byte)(val >>> 32); 78 System.arraycopy(tmp, 0, buffer, index, 4); 79 tmp[0]=(byte)(val >>> 24); 80 tmp[1]=(byte)(val >>> 16); 81 tmp[2]=(byte)(val >>> 8); 82 tmp[3]=(byte)(val); 83 System.arraycopy(tmp, 0, buffer, index+4, 4); 84 index+=8; 85 } 86 void skip(int n) { 87 index+=n; 88 } 89 void putPad(int n) { 90 while(n>0){ 91 buffer[index++]=(byte)0; 92 n--; 93 } 94 } 95 public void putMPInt(byte[] foo){ 96 int i=foo.length; 97 if((foo[0]&0x80)!=0){ 98 i++; 99 putInt(i); 100 putByte((byte)0); 101 } 102 else{ 103 putInt(i); 104 } 105 putByte(foo); 106 } 107 public int getLength(){ 108 return index-s; 109 } 110 public int getOffSet(){ 111 return s; 112 } 113 public void setOffSet(int s){ 114 this.s=s; 115 } 116 public long getLong(){ 117 long foo = getInt()&0xffffffffL; 118 foo = ((foo<<32)) | (getInt()&0xffffffffL); 119 return foo; 120 } 121 public int getInt(){ 122 int foo = getShort(); 123 foo = ((foo<<16)&0xffff0000) | (getShort()&0xffff); 124 return foo; 125 } 126 int getShort() { 127 int foo = getByte(); 128 foo = ((foo<<8)&0xff00)|(getByte()&0xff); 129 return foo; 130 } 131 public int getByte() { 132 return (buffer[s++]&0xff); 133 } 134 public void getByte(byte[] foo) { 135 getByte(foo, 0, foo.length); 136 } 137 void getByte(byte[] foo, int start, int len) { 138 System.arraycopy(buffer, s, foo, start, len); 139 s+=len; 140 } 141 public int getByte(int len) { 142 int foo=s; 143 s+=len; 144 return foo; 145 } 146 public byte[] getMPInt() { 147 int i=getInt(); 148 byte[] foo=new byte[i]; 149 getByte(foo, 0, i); 150 return foo; 151 } 152 public byte[] getMPIntBits() { 153 int bits=getInt(); 154 int bytes=(bits+7)/8; 155 byte[] foo=new byte[bytes]; 156 getByte(foo, 0, bytes); 157 if((foo[0]&0x80)!=0){ 158 byte[] bar=new byte[foo.length+1]; 159 bar[0]=0; System.arraycopy(foo, 0, bar, 1, foo.length); 161 foo=bar; 162 } 163 return foo; 164 } 165 public byte[] getString() { 166 int i=getInt(); 167 byte[] foo=new byte[i]; 168 getByte(foo, 0, i); 169 return foo; 170 } 171 byte[] getString(int[]start, int[]len) { 172 int i=getInt(); 173 start[0]=getByte(i); 174 len[0]=i; 175 return buffer; 176 } 177 public void reset(){ 178 index=0; 179 s=0; 180 } 181 public void shift(){ 182 if(s==0)return; 183 System.arraycopy(buffer, s, buffer, 0, index-s); 184 index=index-s; 185 s=0; 186 } 187 void rewind(){ 188 s=0; 189 } 190 191 221 222 } 223 | Popular Tags |