KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcraft > jsch > Buffer


1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2002,2003,2004,2005,2006 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in
13      the documentation and/or other materials provided with the distribution.
14
15   3. The names of the authors may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */

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; // ??
160
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 /*
192   static String[] chars={
193     "0","1","2","3","4","5","6","7","8","9", "a","b","c","d","e","f"
194   };
195   static void dump_buffer(){
196     int foo;
197     for(int i=0; i<tmp_buffer_index; i++){
198         foo=tmp_buffer[i]&0xff;
199     System.err.print(chars[(foo>>>4)&0xf]);
200     System.err.print(chars[foo&0xf]);
201         if(i%16==15){
202           System.err.println("");
203       continue;
204     }
205         if(i>0 && i%2==1){
206           System.err.print(" ");
207     }
208     }
209     System.err.println("");
210   }
211   static void dump(byte[] b){
212     dump(b, 0, b.length);
213   }
214   static void dump(byte[] b, int s, int l){
215     for(int i=s; i<s+l; i++){
216       System.err.print(Integer.toHexString(b[i]&0xff)+":");
217     }
218     System.err.println("");
219   }
220 */

221
222 }
223
Popular Tags