KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > ssh2 > packets > TypesWriter


1
2 package ch.ethz.ssh2.packets;
3
4 import java.io.UnsupportedEncodingException JavaDoc;
5 import java.math.BigInteger JavaDoc;
6
7 /**
8  * TypesWriter.
9  *
10  * @author Christian Plattner, plattner@inf.ethz.ch
11  * @version $Id: TypesWriter.java,v 1.6 2006/08/31 20:04:29 cplattne Exp $
12  */

13 public class TypesWriter
14 {
15     byte arr[];
16     int pos;
17
18     public TypesWriter()
19     {
20         arr = new byte[256];
21         pos = 0;
22     }
23
24     private void resize(int len)
25     {
26         byte new_arr[] = new byte[len];
27         System.arraycopy(arr, 0, new_arr, 0, arr.length);
28         arr = new_arr;
29     }
30
31     public int length()
32     {
33         return pos;
34     }
35
36     public byte[] getBytes()
37     {
38         byte[] dst = new byte[pos];
39         System.arraycopy(arr, 0, dst, 0, pos);
40         return dst;
41     }
42     
43     public void getBytes(byte dst[])
44     {
45         System.arraycopy(arr, 0, dst, 0, pos);
46     }
47
48     public void writeUINT32(int val, int off)
49     {
50         if ((off + 4) > arr.length)
51             resize(off + 32);
52
53         arr[off++] = (byte) (val >> 24);
54         arr[off++] = (byte) (val >> 16);
55         arr[off++] = (byte) (val >> 8);
56         arr[off++] = (byte) val;
57     }
58
59     public void writeUINT32(int val)
60     {
61         writeUINT32(val, pos);
62         pos += 4;
63     }
64
65     public void writeUINT64(long val)
66     {
67         if ((pos + 8) > arr.length)
68             resize(arr.length + 32);
69
70         arr[pos++] = (byte) (val >> 56);
71         arr[pos++] = (byte) (val >> 48);
72         arr[pos++] = (byte) (val >> 40);
73         arr[pos++] = (byte) (val >> 32);
74         arr[pos++] = (byte) (val >> 24);
75         arr[pos++] = (byte) (val >> 16);
76         arr[pos++] = (byte) (val >> 8);
77         arr[pos++] = (byte) val;
78     }
79
80     public void writeBoolean(boolean v)
81     {
82         if ((pos + 1) > arr.length)
83             resize(arr.length + 32);
84
85         arr[pos++] = v ? (byte) 1 : (byte) 0;
86     }
87
88     public void writeByte(int v, int off)
89     {
90         if ((off + 1) > arr.length)
91             resize(off + 32);
92
93         arr[off] = (byte) v;
94     }
95
96     public void writeByte(int v)
97     {
98         writeByte(v, pos);
99         pos++;
100     }
101
102     public void writeMPInt(BigInteger JavaDoc b)
103     {
104         byte raw[] = b.toByteArray();
105
106         if ((raw.length == 1) && (raw[0] == 0))
107             writeUINT32(0); /* String with zero bytes of data */
108         else
109             writeString(raw, 0, raw.length);
110     }
111
112     public void writeBytes(byte[] buff)
113     {
114         writeBytes(buff, 0, buff.length);
115     }
116     
117     public void writeBytes(byte[] buff, int off, int len)
118     {
119         if ((pos + len) > arr.length)
120             resize(arr.length + len + 32);
121
122         System.arraycopy(buff, off, arr, pos, len);
123         pos += len;
124     }
125
126     public void writeString(byte[] buff, int off, int len)
127     {
128         writeUINT32(len);
129         writeBytes(buff, off, len);
130     }
131
132     public void writeString(String JavaDoc v)
133     {
134         byte[] b = v.getBytes();
135
136         writeUINT32(b.length);
137         writeBytes(b, 0, b.length);
138     }
139     
140     public void writeString(String JavaDoc v, String JavaDoc charsetName) throws UnsupportedEncodingException JavaDoc
141     {
142         byte[] b = (charsetName == null) ? v.getBytes() : v.getBytes(charsetName);
143         
144         writeUINT32(b.length);
145         writeBytes(b, 0, b.length);
146     }
147
148     public void writeNameList(String JavaDoc v[])
149     {
150         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
151         for (int i = 0; i < v.length; i++)
152         {
153             if (i > 0)
154                 sb.append(',');
155             sb.append(v[i]);
156         }
157         writeString(sb.toString());
158     }
159 }
160
Popular Tags