Source Code Cross Referenced for Base64.java in  » Net » j2ssh » com » sshtools » j2ssh » util » 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.util 
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.util;
027:
028:        /**
029:         *
030:         *
031:         * @author $author$
032:         * @version $Revision: 1.15 $
033:         */
034:        public class Base64 {
035:            /**  */
036:            public final static boolean ENCODE = true;
037:
038:            /**  */
039:            public final static boolean DECODE = false;
040:            private final static int MAX_LINE_LENGTH = 76;
041:            private final static byte EQUALS_SIGN = (byte) '=';
042:            private final static byte NEW_LINE = (byte) '\n';
043:            private final static byte[] ALPHABET = { (byte) 'A', (byte) 'B',
044:                    (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G',
045:                    (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L',
046:                    (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q',
047:                    (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V',
048:                    (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a',
049:                    (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',
050:                    (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k',
051:                    (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p',
052:                    (byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u',
053:                    (byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z',
054:                    (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4',
055:                    (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9',
056:                    (byte) '+', (byte) '/' };
057:            private final static byte[] DECODABET = { -9, -9, -9, -9, -9, -9,
058:                    -9, -9, -9, -5, -5, -9, -9, -5, -9, -9, -9, -9, -9, -9, -9,
059:                    -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -5, -9, -9, -9,
060:                    -9, -9, -9, -9,
061:                    -9,
062:                    -9,
063:                    -9,
064:                    // Decimal 33 - 42
065:                    62,
066:                    -9,
067:                    -9,
068:                    -9,
069:                    // Decimal 44 - 46
070:                    63,
071:                    // Slash at decimal 47
072:                    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -9, -9, -9, -1, -9,
073:                    -9,
074:                    -9,
075:                    // Decimal 62 - 64
076:                    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
077:                    13,
078:                    // Letters 'A' through 'N'
079:                    14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -9, -9, -9,
080:                    -9, -9, -9,
081:
082:                    // Decimal 91 - 96
083:                    26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
084:                    // Letters 'a' through 'm'
085:                    39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -9, -9,
086:                    -9, -9 };
087:            private final static byte BAD_ENCODING = -9;
088:
089:            // Indicates error in encoding
090:            private final static byte white_SPACE_ENC = -5;
091:
092:            // Indicates white space in encoding
093:            private final static byte EQUALS_SIGN_ENC = -1;
094:
095:            // Indicates equals sign in encoding
096:            private Base64() {
097:            }
098:
099:            /**
100:             *
101:             *
102:             * @param s
103:             *
104:             * @return
105:             */
106:            public static byte[] decode(String s) {
107:                byte[] bytes = s.getBytes();
108:
109:                return decode(bytes, 0, bytes.length);
110:            }
111:
112:            // end decode
113:            public static byte[] decode(byte[] source, int off, int len) {
114:                int len34 = (len * 3) / 4;
115:                byte[] outBuff = new byte[len34];
116:
117:                // Upper limit on size of output
118:                int outBuffPosn = 0;
119:                byte[] b4 = new byte[4];
120:                int b4Posn = 0;
121:                int i = 0;
122:                byte sbiCrop = 0;
123:                byte sbiDecode = 0;
124:
125:                for (i = 0; i < len; i++) {
126:                    sbiCrop = (byte) (source[i] & 0x7f);
127:
128:                    // Only the low seven bits
129:                    sbiDecode = DECODABET[sbiCrop];
130:
131:                    if (sbiDecode >= white_SPACE_ENC) {
132:                        // White space, Equals sign or better
133:                        if (sbiDecode >= EQUALS_SIGN_ENC) {
134:                            b4[b4Posn++] = sbiCrop;
135:
136:                            if (b4Posn > 3) {
137:                                outBuffPosn += decode4to3(b4, 0, outBuff,
138:                                        outBuffPosn);
139:                                b4Posn = 0;
140:
141:                                // If that was the equals sign, break out of 'for' loop
142:                                if (sbiCrop == EQUALS_SIGN) {
143:                                    break;
144:                                }
145:                            }
146:
147:                            // end if: quartet built
148:                        }
149:
150:                        // end if: equals sign or better
151:                    }
152:                    // end if: white space, equals sign or better
153:                    else {
154:                        System.err.println("Bad Base64 input character at " + i
155:                                + ": " + source[i] + "(decimal)");
156:
157:                        return null;
158:                    }
159:
160:                    // end else:
161:                }
162:
163:                // each input character
164:                byte[] out = new byte[outBuffPosn];
165:                System.arraycopy(outBuff, 0, out, 0, outBuffPosn);
166:
167:                return out;
168:            }
169:
170:            // end decode
171:            public static Object decodeToObject(String encodedObject) {
172:                byte[] objBytes = decode(encodedObject);
173:                java.io.ByteArrayInputStream bais = null;
174:                java.io.ObjectInputStream ois = null;
175:
176:                try {
177:                    bais = new java.io.ByteArrayInputStream(objBytes);
178:                    ois = new java.io.ObjectInputStream(bais);
179:
180:                    return ois.readObject();
181:                }
182:                // end try
183:                catch (java.io.IOException e) {
184:                    e.printStackTrace();
185:
186:                    return null;
187:                }
188:                // end catch
189:                catch (java.lang.ClassNotFoundException e) {
190:                    e.printStackTrace();
191:
192:                    return null;
193:                }
194:                // end catch
195:                finally {
196:                    try {
197:                        bais.close();
198:                    } catch (Exception e) {
199:                    }
200:
201:                    try {
202:                        ois.close();
203:                    } catch (Exception e) {
204:                    }
205:                }
206:
207:                // end finally
208:            }
209:
210:            // end decodeObject
211:            public static String decodeToString(String s) {
212:                return new String(decode(s));
213:            }
214:
215:            // end decodeToString
216:            public static String encodeBytes(byte[] source,
217:                    boolean ignoreMaxLineLength) {
218:                return encodeBytes(source, 0, source.length,
219:                        ignoreMaxLineLength);
220:            }
221:
222:            // end encodeBytes
223:            public static String encodeBytes(byte[] source, int off, int len,
224:                    boolean ignoreMaxLineLength) {
225:                int len43 = (len * 4) / 3;
226:                byte[] outBuff = new byte[(len43) + (((len % 3) > 0) ? 4 : 0)
227:                        + (len43 / MAX_LINE_LENGTH)];
228:
229:                // New lines
230:                int d = 0;
231:                int e = 0;
232:                int len2 = len - 2;
233:                int lineLength = 0;
234:
235:                for (; d < len2; d += 3, e += 4) {
236:                    encode3to4(source, d + off, 3, outBuff, e);
237:                    lineLength += 4;
238:
239:                    if (!ignoreMaxLineLength) {
240:                        if (lineLength == MAX_LINE_LENGTH) {
241:                            outBuff[e + 4] = NEW_LINE;
242:                            e++;
243:                            lineLength = 0;
244:                        }
245:
246:                        // end if: end of line
247:                    }
248:                }
249:
250:                // en dfor: each piece of array
251:                if (d < len) {
252:                    encode3to4(source, d + off, len - d, outBuff, e);
253:                    e += 4;
254:                }
255:
256:                // end if: some padding needed
257:                return new String(outBuff, 0, e);
258:            }
259:
260:            // end encodeBytes
261:            public static String encodeObject(
262:                    java.io.Serializable serializableObject) {
263:                java.io.ByteArrayOutputStream baos = null;
264:                java.io.OutputStream b64os = null;
265:                java.io.ObjectOutputStream oos = null;
266:
267:                try {
268:                    baos = new java.io.ByteArrayOutputStream();
269:                    b64os = new Base64.OutputStream(baos, Base64.ENCODE);
270:                    oos = new java.io.ObjectOutputStream(b64os);
271:                    oos.writeObject(serializableObject);
272:                }
273:                // end try
274:                catch (java.io.IOException e) {
275:                    e.printStackTrace();
276:
277:                    return null;
278:                }
279:                // end catch
280:                finally {
281:                    try {
282:                        oos.close();
283:                    } catch (Exception e) {
284:                    }
285:
286:                    try {
287:                        b64os.close();
288:                    } catch (Exception e) {
289:                    }
290:
291:                    try {
292:                        baos.close();
293:                    } catch (Exception e) {
294:                    }
295:                }
296:
297:                // end finally
298:                return new String(baos.toByteArray());
299:            }
300:
301:            // end encode
302:            public static String encodeString(String s,
303:                    boolean ignoreMaxLineLength) {
304:                return encodeBytes(s.getBytes(), ignoreMaxLineLength);
305:            }
306:
307:            // end encodeString
308:            public static void main(String[] args) {
309:                String s = "P2/56wAAAgoAAAAmZGwtbW9kcHtzaWdue2RzYS1uaXN0LXNoYTF9LGRoe3Bs"
310:                        + "YWlufX0AAAAIM2Rlcy1jYmMAAAHIifTc7/X/swWj4OHVWX9RsUxWh4citAMwGzv6X9mUG6a"
311:                        + "mh5/2f6IiQ3lOeHFd5J0EAOeGNuLqE/RWJ/fFaZAzD6YTr1GZ5hflMzvRu3jbgZoLRz2TaT"
312:                        + "qeRs1yWrQoqANE2nBx6uDNrRahduqalLg2P/ezRCLGpqbw3HFgXmiZvzhd/rdEgZur7ZPnm"
313:                        + "EK7t4Ldypk/7xcK192JTbBXLDSKOEAqfYQb9CzW8MgEXde0DpMRZ9Fgm0KWPfz4CCJ0F9dd"
314:                        + "zcWl1nuGibL3klLKQANgecTurFlrxkBaHgxgl9nIvf24wH3nscvmD/uFOzacT/LzFaD03HFj"
315:                        + "/QHCiTezxVyyuJ39d3e6BBegV26vEFoGbrZ2mMf08C2MBmLmZELYdBRJ4kLpT5EZkzR8L4rT"
316:                        + "GxNiWkb4dGT42gHH41p2ad053lctyFWp/uQJnvJEiEm3BMURVY7k1S7zgv2FHgHE0LssXvBHx"
317:                        + "n/wnft0ne2NOqEXfs/Y4I39Nd7eDIupSVy/ZFfMmNPIhzKyC5lFMkjIMxPXNk548ZoP9Tnga"
318:                        + "4NPhHNKtcMinVvO2HT6dnIKMNb/NuXooULHIMVISpslRzXiVlTcN9vL/jhJhn9S";
319:                byte[] buf = Base64.decode(s);
320:                System.out.println(new String(buf).toString());
321:                s = "P2/56wAAAgIAAAAmZGwtbW9kcHtzaWdue2RzYS1uaXN0LXNoYTF9LGRoe3BsYWlufX0A"
322:                        + "AAAEbm9uZQAAAcQAAAHAAAAAAAAABACwV9/fUska/ZHWa4YzXIPFJ4RbcFqPVV0vmyWkFekw"
323:                        + "wKE1mA0GVDRq9X0HTjA2DZWcQ46suVP/8mLwpnjTKNiRdFvXWGkxEpavLp+bjPa/NXsEsjZL"
324:                        + "aeO5iPZ11Xw5lx7uor8q/Ewwo9IcYOXzuOWN1EPCpdRv5OOaO3PCMq6QSQAAA/9j/IrSUDtf"
325:                        + "BLBlzPHBrzboKjIMXv9O8CIRtSqnV7GV9wllgh3Cm+Eh+rd5CB698JGQD9tMdBn4s8bj/BDL"
326:                        + "4qsxbQbAsZOIin6fqDKNLDxFo357eXM06I5569PgC6cuBoJXOyQTg+sLrjT8/b3/1N4TjdZN"
327:                        + "JiKiSiuOzkn03tNSbgAAAKDJhI2ZNNvzOXhp+VFuoY//D9GvHwAAA/9NkAROm4wF7NCPsBXd"
328:                        + "/+QfNV3NM/FSpOonZZDg2AVnCCGdLOXCWEj60EVHWEf5FbOjJ1KynbbdZA6q5JtVDIYuU9wH"
329:                        + "BCsT5iCexGD5j2HYNcUXT4VG5a6qzqloR2JizlZOcjiEM2j0/hydFUei0VYmJNY5L//AprO6"
330:                        + "1UJL2OGFEQAAAJ0Ts+KlcAYmJjJWODOG3mYuiTgv7A==";
331:                buf = Base64.decode(s);
332:                System.out.println(new String(buf).toString());
333:                buf = Base64
334:                        .decode("P2/56wAAAi4AAAA3aWYtbW9kbntzaWdue3JzYS1wa2NzMS1zaGExfSxlbmNyeXB0e3JzYS1wa2NzMXYyLW9hZXB9fQAAAARub25lAAAB3wAAAdsAAAARAQABAAAD/iJ48YNIqLobasqkyRAD6Ejzhe0bK0Nd12iq0X9xG7M5xyVJns5SH3oAPwsa/V63omsQnm/ERG5lCnFGymTSCTpz0jGYLAh81S4XGbZEJRltP75LiM4J1OIfQkF7Zxd/mYFAYpu50fOLTrk+EwOCyQJK63uXzxQHCU1JKzt61m05AAAEALhvA6F1Ffhf/HLPKe3mp/CdTYQyioHzdL2ur6jyvh+b5wb8WuiaL+xu08vA7/Q763M/TXLX3jMWKOfV3HFn656hBCjnePwXp+uJNIQ4+oxg5H7nr8yo2Tc3Umt9fzgajoLDSd488iozmlSgKeRoVy7hKAGuveGtFqqruNAYArNfAAACALXcpb2stcqNdyTGUPIK/uUBkEeEJGgomqFPZbkMNHqZqEPLa7cJdHIl6wiol3ziQKvvUm/8ya4y7tR9Mzay/cIAAAIAwU2/rquz6oQ1GVJVRsO47Ibes2Hcl8tZRC9cBDy5vIPhzPhsD3pxxXnc1gEUybWqkuO6q1XilE/qN/eAKFSDuQAAAgD0QMX768Ucuv2Eu/ZVkebKBBV7jo4seZyd+hKloFotU4mReU7kNq+oYG19pL07n1TN4SodVoykXPSLBowCKCvX");
335:                System.out.println(new String(buf).toString());
336:                buf = Base64
337:                        .decode("P2/56wAAAjsAAAA3aWYtbW9kbntzaWdue3JzYS1wa2NzMS1zaGExfSxlbmNyeXB0e3JzYS1wa2NzMXYyLW9hZXB9fQAAAAgzZGVzLWNiYwAAAegYJSJacx5dZo5rvtyJEp5qFyBXDOkcGH/H4/dJuny1cWnP5eXOaYt1hwc6ZEUIq4bUISGuXSzmRb+mpXZdkAPPt2RLhy66FnwnERnbItyWsNHrMxT5/oug/TW1l+rh0m/46edQhkla+qpgt3ZCJfBRzwihKAAeQJIt18e7XmvVT5g14Xu5fulXPfKT/cPu6Ox1pwRrOTv2ooM8alM2+K+5uCaP9C3qhEhFcyZOsKoigJt8oIZJD7TBrb2adVfzjyNWXZLw5Lq+liWmGTePvf9Mkx+MgFAyIOT4gV391+Rit8ZjSQaJ5jtsSaqw/MgqtTCWz6aXAaLnxP579a+tVubfVQrGLAa6ztGjI/0DmzEH+OvOLfXljeaEPKXhOxTf2O7Pwn8MDBStJHPXPLZZnsoUyTCajnzxw/ohqxOtgE9nqqO1QFVF6Cd74yZlhQSScRKkBcUlqcenxtruEOvvZXgAc8T5UtfvF8AooI22zltyKZDFJx3vJD6TEoFQSq4zu8H4Eipr42HPpUvIFuVAJFlZepI/RVirsU6sDjh8do0vj9ZGdhdBaD8kR7lrPHAJkmROHljJhEI97YWUJZNXS9i63gVvplsi9/x6uEWjn8eNu08IXID82X+LbvEdmTWOhuaSIqyNjyVe7g==");
338:                System.out.println(new String(buf).toString());
339:            }
340:
341:            /*
342:             *  ********  D E C O D I N G   M E T H O D S  ********
343:             */
344:            private static byte[] decode4to3(byte[] fourBytes) {
345:                byte[] outBuff1 = new byte[3];
346:                int count = decode4to3(fourBytes, 0, outBuff1, 0);
347:                byte[] outBuff2 = new byte[count];
348:
349:                for (int i = 0; i < count; i++) {
350:                    outBuff2[i] = outBuff1[i];
351:                }
352:
353:                return outBuff2;
354:            }
355:
356:            private static int decode4to3(byte[] source, int srcOffset,
357:                    byte[] destination, int destOffset) {
358:                // Example: Dk==
359:                if (source[srcOffset + 2] == EQUALS_SIGN) {
360:                    int outBuff = ((DECODABET[source[srcOffset]] << 24) >>> 6)
361:                            | ((DECODABET[source[srcOffset + 1]] << 24) >>> 12);
362:                    destination[destOffset] = (byte) (outBuff >>> 16);
363:
364:                    return 1;
365:                }
366:                // Example: DkL=
367:                else if (source[srcOffset + 3] == EQUALS_SIGN) {
368:                    int outBuff = ((DECODABET[source[srcOffset]] << 24) >>> 6)
369:                            | ((DECODABET[source[srcOffset + 1]] << 24) >>> 12)
370:                            | ((DECODABET[source[srcOffset + 2]] << 24) >>> 18);
371:                    destination[destOffset] = (byte) (outBuff >>> 16);
372:                    destination[destOffset + 1] = (byte) (outBuff >>> 8);
373:
374:                    return 2;
375:                }
376:                // Example: DkLE
377:                else {
378:                    int outBuff = ((DECODABET[source[srcOffset]] << 24) >>> 6)
379:                            | ((DECODABET[source[srcOffset + 1]] << 24) >>> 12)
380:                            | ((DECODABET[source[srcOffset + 2]] << 24) >>> 18)
381:                            | ((DECODABET[source[srcOffset + 3]] << 24) >>> 24);
382:                    destination[destOffset] = (byte) (outBuff >> 16);
383:                    destination[destOffset + 1] = (byte) (outBuff >> 8);
384:                    destination[destOffset + 2] = (byte) (outBuff);
385:
386:                    return 3;
387:                }
388:            }
389:
390:            // end decodeToBytes
391:
392:            /*
393:             *  ********  E N C O D I N G   M E T H O D S  ********
394:             */
395:            private static byte[] encode3to4(byte[] threeBytes) {
396:                return encode3to4(threeBytes, 3);
397:            }
398:
399:            // end encodeToBytes
400:            private static byte[] encode3to4(byte[] threeBytes, int numSigBytes) {
401:                byte[] dest = new byte[4];
402:                encode3to4(threeBytes, 0, numSigBytes, dest, 0);
403:
404:                return dest;
405:            }
406:
407:            private static byte[] encode3to4(byte[] source, int srcOffset,
408:                    int numSigBytes, byte[] destination, int destOffset) {
409:                //           1         2         3
410:                // 01234567890123456789012345678901 Bit position
411:                // --------000000001111111122222222 Array position from threeBytes
412:                // --------|    ||    ||    ||    | Six bit groups to index ALPHABET
413:                //          >>18  >>12  >> 6  >> 0  Right shift necessary
414:                //                0x3f  0x3f  0x3f  Additional AND
415:                // Create buffer with zero-padding if there are only one or two
416:                // significant bytes passed in the array.
417:                // We have to shift left 24 in order to flush out the 1's that appear
418:                // when Java treats a value as negative that is cast from a byte to an int.
419:                int inBuff = ((numSigBytes > 0) ? ((source[srcOffset] << 24) >>> 8)
420:                        : 0)
421:                        | ((numSigBytes > 1) ? ((source[srcOffset + 1] << 24) >>> 16)
422:                                : 0)
423:                        | ((numSigBytes > 2) ? ((source[srcOffset + 2] << 24) >>> 24)
424:                                : 0);
425:
426:                switch (numSigBytes) {
427:                case 3:
428:                    destination[destOffset] = ALPHABET[(inBuff >>> 18)];
429:                    destination[destOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3f];
430:                    destination[destOffset + 2] = ALPHABET[(inBuff >>> 6) & 0x3f];
431:                    destination[destOffset + 3] = ALPHABET[(inBuff) & 0x3f];
432:
433:                    return destination;
434:
435:                case 2:
436:                    destination[destOffset] = ALPHABET[(inBuff >>> 18)];
437:                    destination[destOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3f];
438:                    destination[destOffset + 2] = ALPHABET[(inBuff >>> 6) & 0x3f];
439:                    destination[destOffset + 3] = EQUALS_SIGN;
440:
441:                    return destination;
442:
443:                case 1:
444:                    destination[destOffset] = ALPHABET[(inBuff >>> 18)];
445:                    destination[destOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3f];
446:                    destination[destOffset + 2] = EQUALS_SIGN;
447:                    destination[destOffset + 3] = EQUALS_SIGN;
448:
449:                    return destination;
450:
451:                default:
452:                    return destination;
453:                }
454:
455:                // end switch
456:            }
457:
458:            // end encode3to4
459:
460:            /*
461:             *  ********  I N N E R   C L A S S   I N P U T S T R E A M  ********
462:             */
463:            public static class InputStream extends java.io.FilterInputStream {
464:                private byte[] buffer;
465:
466:                // Small buffer holding converted data
467:                private boolean encode;
468:
469:                // Encoding or decoding
470:                private int bufferLength;
471:
472:                // Length of buffer (3 or 4)
473:                private int numSigBytes;
474:
475:                // Number of meaningful bytes in the buffer
476:                private int position;
477:
478:                // Current position in the buffer
479:                public InputStream(java.io.InputStream in) {
480:                    this (in, Base64.DECODE);
481:                }
482:
483:                // end constructor
484:                public InputStream(java.io.InputStream in, boolean encode) {
485:                    super (in);
486:                    this .encode = encode;
487:                    this .bufferLength = encode ? 4 : 3;
488:                    this .buffer = new byte[bufferLength];
489:                    this .position = -1;
490:                }
491:
492:                // end constructor
493:                public int read() throws java.io.IOException {
494:                    // Do we need to get data?
495:                    if (position < 0) {
496:                        if (encode) {
497:                            byte[] b3 = new byte[3];
498:                            numSigBytes = 0;
499:
500:                            for (int i = 0; i < 3; i++) {
501:                                try {
502:                                    int b = in.read();
503:
504:                                    // If end of stream, b is -1.
505:                                    if (b >= 0) {
506:                                        b3[i] = (byte) b;
507:                                        numSigBytes++;
508:                                    }
509:
510:                                    // end if: not end of stream
511:                                }
512:                                // end try: read
513:                                catch (java.io.IOException e) {
514:                                    // Only a problem if we got no data at all.
515:                                    if (i == 0) {
516:                                        throw e;
517:                                    }
518:                                }
519:
520:                                // end catch
521:                            }
522:
523:                            // end for: each needed input byte
524:                            if (numSigBytes > 0) {
525:                                encode3to4(b3, 0, numSigBytes, buffer, 0);
526:                                position = 0;
527:                            }
528:
529:                            // end if: got data
530:                        }
531:                        // end if: encoding
532:                        // Else decoding
533:                        else {
534:                            byte[] b4 = new byte[4];
535:                            int i = 0;
536:
537:                            for (i = 0; i < 4; i++) {
538:                                int b = 0;
539:
540:                                do {
541:                                    b = in.read();
542:                                } while ((b >= 0)
543:                                        && (DECODABET[b & 0x7f] < white_SPACE_ENC));
544:
545:                                if (b < 0) {
546:                                    break;
547:
548:                                    // Reads a -1 if end of stream
549:                                }
550:
551:                                b4[i] = (byte) b;
552:                            }
553:
554:                            // end for: each needed input byte
555:                            if (i == 4) {
556:                                numSigBytes = decode4to3(b4, 0, buffer, 0);
557:                                position = 0;
558:                            }
559:
560:                            // end if: got four characters
561:                        }
562:
563:                        // end else: decode
564:                    }
565:
566:                    // end else: get data
567:                    // Got data?
568:                    if (position >= 0) {
569:                        // End of relevant data?
570:                        if (!encode && (position >= numSigBytes)) {
571:                            return -1;
572:                        }
573:
574:                        int b = buffer[position++];
575:
576:                        if (position >= bufferLength) {
577:                            position = -1;
578:                        }
579:
580:                        return b;
581:                    }
582:                    // end if: position >= 0
583:                    // Else error
584:                    else {
585:                        return -1;
586:                    }
587:                }
588:
589:                // end read
590:                public int read(byte[] dest, int off, int len)
591:                        throws java.io.IOException {
592:                    int i;
593:                    int b;
594:
595:                    for (i = 0; i < len; i++) {
596:                        b = read();
597:
598:                        if (b < 0) {
599:                            return -1;
600:                        }
601:
602:                        dest[off + i] = (byte) b;
603:                    }
604:
605:                    // end for: each byte read
606:                    return i;
607:                }
608:
609:                // end read
610:            }
611:
612:            // end inner class InputStream
613:
614:            /*
615:             *  ********  I N N E R   C L A S S   O U T P U T S T R E A M  ********
616:             */
617:            public static class OutputStream extends java.io.FilterOutputStream {
618:                private byte[] buffer;
619:                private boolean encode;
620:                private int bufferLength;
621:                private int lineLength;
622:                private int position;
623:
624:                public OutputStream(java.io.OutputStream out) {
625:                    this (out, Base64.ENCODE);
626:                }
627:
628:                // end constructor
629:                public OutputStream(java.io.OutputStream out, boolean encode) {
630:                    super (out);
631:                    this .encode = encode;
632:                    this .bufferLength = encode ? 3 : 4;
633:                    this .buffer = new byte[bufferLength];
634:                    this .position = 0;
635:                    this .lineLength = 0;
636:                }
637:
638:                // end constructor
639:                public void close() throws java.io.IOException {
640:                    this .flush();
641:                    super .close();
642:                    out.close();
643:                    buffer = null;
644:                    out = null;
645:                }
646:
647:                // end close
648:                public void flush() throws java.io.IOException {
649:                    if (position > 0) {
650:                        if (encode) {
651:                            out.write(Base64.encode3to4(buffer, position));
652:                        }
653:                        // end if: encoding
654:                        else {
655:                            throw new java.io.IOException(
656:                                    "Base64 input not properly padded.");
657:                        }
658:
659:                        // end else: decoding
660:                    }
661:
662:                    // end if: buffer partially full
663:                    super .flush();
664:                    out.flush();
665:                }
666:
667:                // end flush
668:                public void write(int theByte) throws java.io.IOException {
669:                    buffer[position++] = (byte) theByte;
670:
671:                    if (position >= bufferLength) {
672:                        if (encode) {
673:                            out.write(Base64.encode3to4(buffer, bufferLength));
674:                            lineLength += 4;
675:
676:                            if (lineLength >= MAX_LINE_LENGTH) {
677:                                out.write(NEW_LINE);
678:                                lineLength = 0;
679:                            }
680:
681:                            // end if: end o fline
682:                        }
683:                        // end if: encoding
684:                        else {
685:                            out.write(Base64.decode4to3(buffer));
686:                        }
687:
688:                        position = 0;
689:                    }
690:
691:                    // end if: convert and flush
692:                }
693:
694:                // end write
695:                public void write(byte[] theBytes, int off, int len)
696:                        throws java.io.IOException {
697:                    for (int i = 0; i < len; i++) {
698:                        write(theBytes[off + i]);
699:                    }
700:
701:                    // end for: each byte written
702:                }
703:
704:                // end write
705:            }
706:
707:            // end inner class OutputStream
708:        }
709:
710:        // end class Base64
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.