KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.net.Socket JavaDoc;
32
33 class Util{
34
35   private static final byte[] b64 ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".getBytes();
36   private static byte val(byte foo){
37     if(foo == '=') return 0;
38     for(int j=0; j<b64.length; j++){
39       if(foo==b64[j]) return (byte)j;
40     }
41     return 0;
42   }
43   static byte[] fromBase64(byte[] buf, int start, int length){
44     byte[] foo=new byte[length];
45     int j=0;
46     for (int i=start;i<start+length;i+=4){
47       foo[j]=(byte)((val(buf[i])<<2)|((val(buf[i+1])&0x30)>>>4));
48       if(buf[i+2]==(byte)'='){ j++; break;}
49       foo[j+1]=(byte)(((val(buf[i+1])&0x0f)<<4)|((val(buf[i+2])&0x3c)>>>2));
50       if(buf[i+3]==(byte)'='){ j+=2; break;}
51       foo[j+2]=(byte)(((val(buf[i+2])&0x03)<<6)|(val(buf[i+3])&0x3f));
52       j+=3;
53     }
54     byte[] bar=new byte[j];
55     System.arraycopy(foo, 0, bar, 0, j);
56     return bar;
57   }
58   static byte[] toBase64(byte[] buf, int start, int length){
59
60     byte[] tmp=new byte[length*2];
61     int i,j,k;
62     
63     int foo=(length/3)*3+start;
64     i=0;
65     for(j=start; j<foo; j+=3){
66       k=(buf[j]>>>2)&0x3f;
67       tmp[i++]=b64[k];
68       k=(buf[j]&0x03)<<4|(buf[j+1]>>>4)&0x0f;
69       tmp[i++]=b64[k];
70       k=(buf[j+1]&0x0f)<<2|(buf[j+2]>>>6)&0x03;
71       tmp[i++]=b64[k];
72       k=buf[j+2]&0x3f;
73       tmp[i++]=b64[k];
74     }
75
76     foo=(start+length)-foo;
77     if(foo==1){
78       k=(buf[j]>>>2)&0x3f;
79       tmp[i++]=b64[k];
80       k=((buf[j]&0x03)<<4)&0x3f;
81       tmp[i++]=b64[k];
82       tmp[i++]=(byte)'=';
83       tmp[i++]=(byte)'=';
84     }
85     else if(foo==2){
86       k=(buf[j]>>>2)&0x3f;
87       tmp[i++]=b64[k];
88       k=(buf[j]&0x03)<<4|(buf[j+1]>>>4)&0x0f;
89       tmp[i++]=b64[k];
90       k=((buf[j+1]&0x0f)<<2)&0x3f;
91       tmp[i++]=b64[k];
92       tmp[i++]=(byte)'=';
93     }
94     byte[] bar=new byte[i];
95     System.arraycopy(tmp, 0, bar, 0, i);
96     return bar;
97
98 // return sun.misc.BASE64Encoder().encode(buf);
99
}
100
101   static String JavaDoc[] split(String JavaDoc foo, String JavaDoc split){
102     if(foo==null)
103       return null;
104     byte[] buf=foo.getBytes();
105     java.util.Vector JavaDoc bar=new java.util.Vector JavaDoc();
106     int start=0;
107     int index;
108     while(true){
109       index=foo.indexOf(split, start);
110       if(index>=0){
111     bar.addElement(new String JavaDoc(buf, start, index-start));
112     start=index+1;
113     continue;
114       }
115       bar.addElement(new String JavaDoc(buf, start, buf.length-start));
116       break;
117     }
118     String JavaDoc[] result=new String JavaDoc[bar.size()];
119     for(int i=0; i<result.length; i++){
120       result[i]=(String JavaDoc)(bar.elementAt(i));
121     }
122     return result;
123   }
124   static boolean glob(byte[] pattern, byte[] name){
125     return glob0(pattern, 0, name, 0);
126   }
127   static private boolean glob0(byte[] pattern, int pattern_index,
128                   byte[] name, int name_index){
129     if(name.length>0 && name[0]=='.'){
130       if(pattern.length>0 && pattern[0]=='.'){
131         if(pattern.length==2 && pattern[1]=='*') return true;
132         return glob(pattern, pattern_index+1, name, name_index+1);
133       }
134       return false;
135     }
136     return glob(pattern, pattern_index, name, name_index);
137   }
138   static private boolean glob(byte[] pattern, int pattern_index,
139                   byte[] name, int name_index){
140 //System.err.println("glob: "+new String(pattern)+", "+new String(name));
141
int patternlen=pattern.length;
142     if(patternlen==0)
143       return false;
144     int namelen=name.length;
145     int i=pattern_index;
146     int j=name_index;
147     while(i<patternlen && j<namelen){
148 //System.err.println("i="+i+", j="+j);
149
if(pattern[i]=='\\'){
150     if(i+1==patternlen)
151       return false;
152     i++;
153     if(pattern[i]!=name[j]) return false;
154     i++; j++;
155     continue;
156       }
157       if(pattern[i]=='*'){
158     if(patternlen==i+1) return true;
159     i++;
160     byte foo=pattern[i];
161     while(j<namelen){
162       if(foo==name[j]){
163         if(glob(pattern, i, name, j)){
164           return true;
165         }
166       }
167       j++;
168     }
169     return false;
170     /*
171     if(j==namelen) return false;
172     i++; j++;
173     continue;
174     */

175       }
176       if(pattern[i]=='?'){
177     i++; j++;
178     continue;
179       }
180       if(pattern[i]!=name[j]) return false;
181       i++; j++;
182       if(!(j<namelen)){
183         if(!(i<patternlen)){
184       return true;
185     }
186     if(pattern[i]=='*'
187            //&& !((i+1)<patternlen)
188
){
189       return true;
190     }
191       }
192       continue;
193     }
194     if(i==patternlen && j==namelen) return true;
195     return false;
196   }
197
198   static String JavaDoc unquote(String JavaDoc _path){
199     byte[] path=_path.getBytes();
200     int pathlen=path.length;
201     int i=0;
202     while(i<pathlen){
203       if(path[i]=='\\'){
204         if(i+1==pathlen)
205           break;
206         System.arraycopy(path, i+1, path, i, path.length-(i+1));
207         pathlen--;
208         continue;
209       }
210       i++;
211     }
212     if(pathlen==path.length)return _path;
213     byte[] foo=new byte[pathlen];
214     System.arraycopy(path, 0, foo, 0, pathlen);
215     return new String JavaDoc(foo);
216   }
217
218   private static String JavaDoc[] chars={
219     "0","1","2","3","4","5","6","7","8","9", "a","b","c","d","e","f"
220   };
221   static String JavaDoc getFingerPrint(HASH hash, byte[] data){
222     try{
223       hash.init();
224       hash.update(data, 0, data.length);
225       byte[] foo=hash.digest();
226       StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
227       int bar;
228       for(int i=0; i<foo.length;i++){
229         bar=foo[i]&0xff;
230         sb.append(chars[(bar>>>4)&0xf]);
231         sb.append(chars[(bar)&0xf]);
232         if(i+1<foo.length)
233           sb.append(":");
234       }
235       return sb.toString();
236     }
237     catch(Exception JavaDoc e){
238       return "???";
239     }
240   }
241   static boolean array_equals(byte[] foo, byte bar[]){
242     int i=foo.length;
243     if(i!=bar.length) return false;
244     for(int j=0; j<i; j++){ if(foo[j]!=bar[j]) return false; }
245     //try{while(true){i--; if(foo[i]!=bar[i])return false;}}catch(Exception e){}
246
return true;
247   }
248   static Socket JavaDoc createSocket(String JavaDoc host, int port, int timeout) throws JSchException{
249     Socket JavaDoc socket=null;
250     if(timeout==0){
251       try{
252         socket=new Socket JavaDoc(host, port);
253         return socket;
254       }
255       catch(Exception JavaDoc e){
256         String JavaDoc message=e.toString();
257         if(e instanceof Throwable JavaDoc)
258           throw new JSchException(message, (Throwable JavaDoc)e);
259         throw new JSchException(message);
260       }
261     }
262     final String JavaDoc _host=host;
263     final int _port=port;
264     final Socket JavaDoc[] sockp=new Socket JavaDoc[1];
265     final Exception JavaDoc[] ee=new Exception JavaDoc[1];
266     String JavaDoc message="";
267     Thread JavaDoc tmp=new Thread JavaDoc(new Runnable JavaDoc(){
268         public void run(){
269           sockp[0]=null;
270           try{
271             sockp[0]=new Socket JavaDoc(_host, _port);
272           }
273           catch(Exception JavaDoc e){
274             ee[0]=e;
275             if(sockp[0]!=null && sockp[0].isConnected()){
276               try{
277                 sockp[0].close();
278               }
279               catch(Exception JavaDoc eee){}
280             }
281             sockp[0]=null;
282           }
283         }
284       });
285     tmp.setName("Opening Socket "+host);
286     tmp.start();
287     try{
288       tmp.join(timeout);
289       message="timeout: ";
290     }
291     catch(java.lang.InterruptedException JavaDoc eee){
292     }
293     if(sockp[0]!=null && sockp[0].isConnected()){
294       socket=sockp[0];
295     }
296     else{
297       message+="socket is not established";
298       if(ee[0]!=null){
299         message=ee[0].toString();
300       }
301       tmp.interrupt();
302       tmp=null;
303       throw new JSchException(message);
304     }
305     return socket;
306   }
307
308   static byte[] str2byte(String JavaDoc str){
309     if(str==null)
310       return null;
311     try{ return str.getBytes("UTF-8"); }
312     catch(java.io.UnsupportedEncodingException JavaDoc e){
313       return str.getBytes();
314     }
315   }
316   static String JavaDoc byte2str(byte[] str){
317     try{ return new String JavaDoc(str, "UTF-8"); }
318     catch(java.io.UnsupportedEncodingException JavaDoc e){
319       return new String JavaDoc(str);
320     }
321   }
322
323   /*
324   static byte[] char2byte(char[] foo){
325     int len=0;
326     for(int i=0; i<foo.length; i++){
327       if((foo[i]&0xff00)==0) len++;
328       else len+=2;
329     }
330     byte[] bar=new byte[len];
331     for(int i=0, j=0; i<foo.length; i++){
332       if((foo[i]&0xff00)==0){
333         bar[j++]=(byte)foo[i];
334       }
335       else{
336         bar[j++]=(byte)(foo[i]>>>8);
337         bar[j++]=(byte)foo[i];
338       }
339     }
340     return bar;
341   }
342   */

343   static void bzero(byte[] foo){
344     if(foo==null)
345       return;
346     for(int i=0; i<foo.length; i++)
347       foo[i]=0;
348   }
349 }
350
Popular Tags