comparison app/src/main/java/ch/ethz/ssh2/packets/TypesWriter.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/ch/ethz/ssh2/packets/TypesWriter.java@071eccdff8ea
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
1 /*
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3 * Please refer to the LICENSE.txt for licensing details.
4 */
5 package ch.ethz.ssh2.packets;
6
7 import java.io.UnsupportedEncodingException;
8 import java.math.BigInteger;
9
10 import ch.ethz.ssh2.util.StringEncoder;
11
12 /**
13 * @author Christian Plattner
14 * @version $Id: TypesWriter.java 160 2014-05-01 14:30:26Z dkocher@sudo.ch $
15 */
16 public final class TypesWriter {
17 byte arr[];
18 int pos;
19
20 public TypesWriter() {
21 arr = new byte[256];
22 pos = 0;
23 }
24
25 private void resize(int len) {
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 return pos;
33 }
34
35 public byte[] getBytes() {
36 byte[] dst = new byte[pos];
37 System.arraycopy(arr, 0, dst, 0, pos);
38 return dst;
39 }
40
41 public void getBytes(byte dst[]) {
42 System.arraycopy(arr, 0, dst, 0, pos);
43 }
44
45 public void writeUINT32(int val, int off) {
46 if ((off + 4) > arr.length) {
47 resize(off + 32);
48 }
49
50 arr[off++] = (byte)(val >> 24);
51 arr[off++] = (byte)(val >> 16);
52 arr[off++] = (byte)(val >> 8);
53 arr[off++] = (byte) val;
54 }
55
56 public void writeUINT32(int val) {
57 writeUINT32(val, pos);
58 pos += 4;
59 }
60
61 public void writeUINT64(long val) {
62 if ((pos + 8) > arr.length) {
63 resize(arr.length + 32);
64 }
65
66 arr[pos++] = (byte)(val >> 56);
67 arr[pos++] = (byte)(val >> 48);
68 arr[pos++] = (byte)(val >> 40);
69 arr[pos++] = (byte)(val >> 32);
70 arr[pos++] = (byte)(val >> 24);
71 arr[pos++] = (byte)(val >> 16);
72 arr[pos++] = (byte)(val >> 8);
73 arr[pos++] = (byte) val;
74 }
75
76 public void writeBoolean(boolean v) {
77 if ((pos + 1) > arr.length) {
78 resize(arr.length + 32);
79 }
80
81 arr[pos++] = v ? (byte) 1 : (byte) 0;
82 }
83
84 public void writeByte(int v, int off) {
85 if ((off + 1) > arr.length) {
86 resize(off + 32);
87 }
88
89 arr[off] = (byte) v;
90 }
91
92 public void writeByte(int v) {
93 writeByte(v, pos);
94 pos++;
95 }
96
97 public void writeMPInt(BigInteger b) {
98 byte raw[] = b.toByteArray();
99
100 if ((raw.length == 1) && (raw[0] == 0)) {
101 writeUINT32(0); /* String with zero bytes of data */
102 }
103 else {
104 writeString(raw, 0, raw.length);
105 }
106 }
107
108 public void writeBytes(byte[] buff) {
109 writeBytes(buff, 0, buff.length);
110 }
111
112 public void writeBytes(byte[] buff, int off, int len) {
113 if ((pos + len) > arr.length) {
114 resize(arr.length + len + 32);
115 }
116
117 System.arraycopy(buff, off, arr, pos, len);
118 pos += len;
119 }
120
121 public void writeString(byte[] buff, int off, int len) {
122 writeUINT32(len);
123 writeBytes(buff, off, len);
124 }
125
126 public void writeString(String v) {
127 byte[] b = StringEncoder.GetBytes(v);
128 writeUINT32(b.length);
129 writeBytes(b, 0, b.length);
130 }
131
132 public void writeString(String v, String charsetName) throws UnsupportedEncodingException {
133 byte[] b = (charsetName == null) ? StringEncoder.GetBytes(v) : v.getBytes(charsetName);
134 writeUINT32(b.length);
135 writeBytes(b, 0, b.length);
136 }
137
138 public void writeNameList(String v[]) {
139 StringBuilder sb = new StringBuilder();
140
141 for (int i = 0; i < v.length; i++) {
142 if (i > 0) {
143 sb.append(',');
144 }
145
146 sb.append(v[i]);
147 }
148
149 writeString(sb.toString());
150 }
151 }