001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.j2ssh.io;
027:
028: import java.io.ByteArrayOutputStream;
029: import java.io.IOException;
030:
031: import java.math.BigInteger;
032:
033: /**
034: *
035: *
036: * @author $author$
037: * @version $Revision: 1.18 $
038: */
039: public class ByteArrayWriter extends ByteArrayOutputStream {
040: /**
041: * Creates a new ByteArrayWriter object.
042: */
043: public ByteArrayWriter() {
044: }
045:
046: /**
047: *
048: *
049: * @param bi
050: *
051: * @throws IOException
052: */
053: public void writeBigInteger(BigInteger bi) throws IOException {
054: byte[] raw = bi.toByteArray();
055: writeInt(raw.length);
056: write(raw);
057: }
058:
059: /**
060: *
061: *
062: * @param b
063: *
064: * @throws IOException
065: */
066: public void writeBoolean(boolean b) throws IOException {
067: write(b ? 1 : 0);
068: }
069:
070: /**
071: *
072: *
073: * @param data
074: *
075: * @throws IOException
076: */
077: public void writeBinaryString(byte[] data) throws IOException {
078: writeInt(data.length);
079: write(data);
080: }
081:
082: /**
083: *
084: *
085: * @param i
086: *
087: * @throws IOException
088: */
089: public void writeInt(long i) throws IOException {
090: byte[] raw = new byte[4];
091: raw[0] = (byte) (i >> 24);
092: raw[1] = (byte) (i >> 16);
093: raw[2] = (byte) (i >> 8);
094: raw[3] = (byte) (i);
095: write(raw);
096: }
097:
098: /**
099: *
100: *
101: * @param i
102: *
103: * @throws IOException
104: */
105: public void writeInt(int i) throws IOException {
106: byte[] raw = new byte[4];
107: raw[0] = (byte) (i >> 24);
108: raw[1] = (byte) (i >> 16);
109: raw[2] = (byte) (i >> 8);
110: raw[3] = (byte) (i);
111: write(raw);
112: }
113:
114: /**
115: *
116: *
117: * @param i
118: *
119: * @return
120: */
121: public static byte[] encodeInt(int i) {
122: byte[] raw = new byte[4];
123: raw[0] = (byte) (i >> 24);
124: raw[1] = (byte) (i >> 16);
125: raw[2] = (byte) (i >> 8);
126: raw[3] = (byte) (i);
127:
128: return raw;
129: }
130:
131: /**
132: *
133: *
134: * @param value
135: *
136: * @throws IOException
137: */
138: public void writeUINT32(UnsignedInteger32 value) throws IOException {
139: writeInt(value.longValue());
140: }
141:
142: /**
143: *
144: *
145: * @param value
146: *
147: * @throws IOException
148: */
149: public void writeUINT64(UnsignedInteger64 value) throws IOException {
150: byte[] raw = new byte[8];
151: byte[] bi = value.bigIntValue().toByteArray();
152: System.arraycopy(bi, 0, raw, raw.length - bi.length, bi.length);
153:
154: // Pad the raw data
155: write(raw);
156: }
157:
158: /**
159: *
160: *
161: * @param array
162: * @param pos
163: * @param value
164: *
165: * @throws IOException
166: */
167: public static void writeIntToArray(byte[] array, int pos, int value)
168: throws IOException {
169: if ((array.length - pos) < 4) {
170: throw new IOException(
171: "Not enough data in array to write integer at position "
172: + String.valueOf(pos));
173: }
174:
175: array[pos] = (byte) (value >> 24);
176: array[pos + 1] = (byte) (value >> 16);
177: array[pos + 2] = (byte) (value >> 8);
178: array[pos + 3] = (byte) (value);
179: }
180:
181: /**
182: *
183: *
184: * @param str
185: *
186: * @throws IOException
187: */
188: public void writeString(String str) throws IOException {
189: if (str == null) {
190: writeInt(0);
191: } else {
192: /*
193: writeInt(str.length());
194: // don't use US-ASCII by default!
195: write(str.getBytes());
196: */
197: // patch as of version 0.2.9
198: // for UTF-8 length of string is not necessarily
199: // equal to number of bytes
200: byte[] strBytes = str.getBytes();
201: writeInt(strBytes.length);
202: write(strBytes);
203: }
204: }
205: }
|