comparison src/com/trilead/ssh2/packets/TypesWriter.java @ 0:0ce5cc452d02

initial version
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 10:41:19 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:0ce5cc452d02
1
2 package com.trilead.ssh2.packets;
3
4 import java.io.UnsupportedEncodingException;
5 import java.math.BigInteger;
6
7 /**
8 * TypesWriter.
9 *
10 * @author Christian Plattner, plattner@trilead.com
11 * @version $Id: TypesWriter.java,v 1.2 2008/04/01 12:38:09 cplattne Exp $
12 */
13 public class TypesWriter {
14 byte arr[];
15 int pos;
16
17 public TypesWriter() {
18 arr = new byte[256];
19 pos = 0;
20 }
21
22 private void resize(int len) {
23 byte new_arr[] = new byte[len];
24 System.arraycopy(arr, 0, new_arr, 0, arr.length);
25 arr = new_arr;
26 }
27
28 public int length() {
29 return pos;
30 }
31
32 public byte[] getBytes() {
33 byte[] dst = new byte[pos];
34 System.arraycopy(arr, 0, dst, 0, pos);
35 return dst;
36 }
37
38 public void getBytes(byte dst[]) {
39 System.arraycopy(arr, 0, dst, 0, pos);
40 }
41
42 public void writeUINT32(int val, int off) {
43 if ((off + 4) > arr.length)
44 resize(off + 32);
45
46 arr[off++] = (byte)(val >> 24);
47 arr[off++] = (byte)(val >> 16);
48 arr[off++] = (byte)(val >> 8);
49 arr[off++] = (byte) val;
50 }
51
52 public void writeUINT32(int val) {
53 writeUINT32(val, pos);
54 pos += 4;
55 }
56
57 public void writeUINT64(long val) {
58 if ((pos + 8) > arr.length)
59 resize(arr.length + 32);
60
61 arr[pos++] = (byte)(val >> 56);
62 arr[pos++] = (byte)(val >> 48);
63 arr[pos++] = (byte)(val >> 40);
64 arr[pos++] = (byte)(val >> 32);
65 arr[pos++] = (byte)(val >> 24);
66 arr[pos++] = (byte)(val >> 16);
67 arr[pos++] = (byte)(val >> 8);
68 arr[pos++] = (byte) val;
69 }
70
71 public void writeBoolean(boolean v) {
72 if ((pos + 1) > arr.length)
73 resize(arr.length + 32);
74
75 arr[pos++] = v ? (byte) 1 : (byte) 0;
76 }
77
78 public void writeByte(int v, int off) {
79 if ((off + 1) > arr.length)
80 resize(off + 32);
81
82 arr[off] = (byte) v;
83 }
84
85 public void writeByte(int v) {
86 writeByte(v, pos);
87 pos++;
88 }
89
90 public void writeMPInt(BigInteger b) {
91 byte raw[] = b.toByteArray();
92
93 if ((raw.length == 1) && (raw[0] == 0))
94 writeUINT32(0); /* String with zero bytes of data */
95 else
96 writeString(raw, 0, raw.length);
97 }
98
99 public void writeBytes(byte[] buff) {
100 writeBytes(buff, 0, buff.length);
101 }
102
103 public void writeBytes(byte[] buff, int off, int len) {
104 if ((pos + len) > arr.length)
105 resize(arr.length + len + 32);
106
107 System.arraycopy(buff, off, arr, pos, len);
108 pos += len;
109 }
110
111 public void writeString(byte[] buff, int off, int len) {
112 writeUINT32(len);
113 writeBytes(buff, off, len);
114 }
115
116 public void writeString(String v) {
117 byte[] b;
118
119 try {
120 /* All Java JVMs must support ISO-8859-1 */
121 b = v.getBytes("ISO-8859-1");
122 }
123 catch (UnsupportedEncodingException ignore) {
124 b = v.getBytes();
125 }
126
127 writeUINT32(b.length);
128 writeBytes(b, 0, b.length);
129 }
130
131 public void writeString(String v, String charsetName) throws UnsupportedEncodingException {
132 byte[] b = (charsetName == null) ? v.getBytes() : v.getBytes(charsetName);
133 writeUINT32(b.length);
134 writeBytes(b, 0, b.length);
135 }
136
137 public void writeNameList(String v[]) {
138 StringBuffer sb = new StringBuffer();
139
140 for (int i = 0; i < v.length; i++) {
141 if (i > 0)
142 sb.append(',');
143
144 sb.append(v[i]);
145 }
146
147 writeString(sb.toString());
148 }
149 }