Source Code Cross Referenced for ByteArrayWriter.java in  » Net » j2ssh » com » sshtools » j2ssh » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » j2ssh » com.sshtools.j2ssh.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.