Source Code Cross Referenced for SVNBase64.java in  » Source-Control » tmatesoft-SVN » org » tmatesoft » svn » core » internal » 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 » Source Control » tmatesoft SVN » org.tmatesoft.svn.core.internal.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * ====================================================================
003:         * Copyright (c) 2004-2008 TMate Software Ltd.  All rights reserved.
004:         *
005:         * This software is licensed as described in the file COPYING, which
006:         * you should have received as part of this distribution.  The terms
007:         * are also available at http://svnkit.com/license.html
008:         * If newer versions of this license are posted there, you may use a
009:         * newer version instead, at your option.
010:         * ====================================================================
011:         */
012:
013:        package org.tmatesoft.svn.core.internal.util;
014:
015:        /**
016:         * @version 1.1.1
017:         * @author  TMate Software Ltd.
018:         */
019:        public class SVNBase64 {
020:
021:            public static String byteArrayToBase64(byte[] a) {
022:                return byteArrayToBase64(a, false);
023:            }
024:
025:            public static String byteArrayToAltBase64(byte[] a) {
026:                return byteArrayToBase64(a, true);
027:            }
028:
029:            private static String byteArrayToBase64(byte[] a, boolean alternate) {
030:                int aLen = a.length;
031:                int numFullGroups = aLen / 3;
032:                int numBytesInPartialGroup = aLen - 3 * numFullGroups;
033:                int resultLen = 4 * ((aLen + 2) / 3);
034:                StringBuffer result = new StringBuffer(resultLen);
035:                char[] intToAlpha = (alternate ? intToAltBase64 : intToBase64);
036:
037:                // Translate all full groups from byte array elements to SVNBase64
038:                int inCursor = 0;
039:                for (int i = 0; i < numFullGroups; i++) {
040:                    int byte0 = a[inCursor++] & 0xff;
041:                    int byte1 = a[inCursor++] & 0xff;
042:                    int byte2 = a[inCursor++] & 0xff;
043:                    result.append(intToAlpha[byte0 >> 2]);
044:                    result
045:                            .append(intToAlpha[(byte0 << 4) & 0x3f
046:                                    | (byte1 >> 4)]);
047:                    result
048:                            .append(intToAlpha[(byte1 << 2) & 0x3f
049:                                    | (byte2 >> 6)]);
050:                    result.append(intToAlpha[byte2 & 0x3f]);
051:                }
052:
053:                // Translate partial group if present
054:                if (numBytesInPartialGroup != 0) {
055:                    int byte0 = a[inCursor++] & 0xff;
056:                    result.append(intToAlpha[byte0 >> 2]);
057:                    if (numBytesInPartialGroup == 1) {
058:                        result.append(intToAlpha[(byte0 << 4) & 0x3f]);
059:                        result.append("==");
060:                    } else {
061:                        // assert numBytesInPartialGroup == 2;
062:                        int byte1 = a[inCursor++] & 0xff;
063:                        result.append(intToAlpha[(byte0 << 4) & 0x3f
064:                                | (byte1 >> 4)]);
065:                        result.append(intToAlpha[(byte1 << 2) & 0x3f]);
066:                        result.append('=');
067:                    }
068:                }
069:                // assert inCursor == a.length;
070:                // assert result.length() == resultLen;
071:                return result.toString();
072:            }
073:
074:            private static final char intToBase64[] = { 'A', 'B', 'C', 'D',
075:                    'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
076:                    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
077:                    'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
078:                    'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
079:                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
080:
081:            private static final char intToAltBase64[] = { '!', '"', '#', '$',
082:                    '%', '&', '\'', '(', ')', ',', '-', '.', ':', ';', '<',
083:                    '>', '@', '[', ']', '^', '`', '_', '{', '|', '}', '~', 'a',
084:                    'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
085:                    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
086:                    'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+',
087:                    '?' };
088:
089:            /**
090:             * Translates the specified SVNBase64 string (as per Preferences.get(byte[]))
091:             * into a byte array.
092:             * 
093:             * @throws IllegalArgumentException
094:             *             if <tt>s</tt> is not a valid SVNBase64 string.
095:             */
096:            public static int base64ToByteArray(StringBuffer s, byte[] buffer) {
097:                return base64ToByteArray(s, buffer, false);
098:            }
099:
100:            private static int base64ToByteArray(StringBuffer s, byte[] result,
101:                    boolean alternate) {
102:                byte[] alphaToInt = (alternate ? altBase64ToInt : base64ToInt);
103:                int sLen = s.length();
104:                int numGroups = sLen / 4;
105:                if (4 * numGroups != sLen) {
106:                    for (int i = 0; i < 4 - (sLen % 4); i++) {
107:                        s = s.append('=');
108:                    }
109:                    numGroups++;
110:                    sLen = 4 * numGroups;
111:                }
112:                int missingBytesInLastGroup = 0;
113:                int numFullGroups = numGroups;
114:                if (sLen != 0) {
115:                    if (s.charAt(sLen - 1) == '=') {
116:                        missingBytesInLastGroup++;
117:                        numFullGroups--;
118:                    }
119:                    if (s.charAt(sLen - 2) == '=')
120:                        missingBytesInLastGroup++;
121:                }
122:                int resultLength = 3 * numGroups - missingBytesInLastGroup;
123:
124:                // Translate all full groups from base64 to byte array elements
125:                int inCursor = 0, outCursor = 0;
126:                for (int i = 0; i < numFullGroups; i++) {
127:                    int ch0 = alphaToInt[s.charAt(inCursor++)];
128:                    int ch1 = alphaToInt[s.charAt(inCursor++)];
129:                    int ch2 = alphaToInt[s.charAt(inCursor++)];
130:                    int ch3 = alphaToInt[s.charAt(inCursor++)];
131:                    result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
132:                    result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
133:                    result[outCursor++] = (byte) ((ch2 << 6) | ch3);
134:                }
135:
136:                // Translate partial group, if present
137:                if (missingBytesInLastGroup != 0) {
138:                    int ch0 = alphaToInt[s.charAt(inCursor++)];
139:                    int ch1 = alphaToInt[s.charAt(inCursor++)];
140:                    result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
141:
142:                    if (missingBytesInLastGroup == 1) {
143:                        int ch2 = alphaToInt[s.charAt(inCursor++)];
144:                        result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
145:                    }
146:                }
147:                return resultLength;
148:            }
149:
150:            /**
151:             * Translates the specified character, which is assumed to be in the "Base
152:             * 64 Alphabet" into its equivalent 6-bit positive integer.
153:             * 
154:             * @throws IllegalArgumentException
155:             *             or ArrayOutOfBoundsException if c is not in the SVNBase64
156:             *             Alphabet.
157:             */
158:            /*
159:             * private static int base64toInt(char c, byte[] alphaToInt) { int result =
160:             * alphaToInt[c]; if (result < 0) throw new
161:             * IllegalArgumentException("Illegal character " + c); return result; }
162:             */
163:
164:            /**
165:             * This array is a lookup table that translates unicode characters drawn
166:             * from the "SVNBase64 Alphabet" (as specified in Table 1 of RFC 2045) into
167:             * their 6-bit positive integer equivalents. Characters that are not in the
168:             * SVNBase64 alphabet but fall within the bounds of the array are translated to
169:             * -1.
170:             */
171:            private static final byte base64ToInt[] = { -1, -1, -1, -1, -1, -1,
172:                    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
173:                    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
174:                    -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54,
175:                    55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0,
176:                    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
177:                    18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26,
178:                    27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
179:                    42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };
180:
181:            /**
182:             * This array is the analogue of base64ToInt, but for the nonstandard
183:             * variant that avoids the use of uppercase alphabetic characters.
184:             */
185:            private static final byte altBase64ToInt[] = { -1, -1, -1, -1, -1,
186:                    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
187:                    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1,
188:                    2, 3, 4, 5, 6, 7, 8, -1, 62, 9, 10, 11, -1, 52, 53, 54, 55,
189:                    56, 57, 58, 59, 60, 61, 12, 13, 14, -1, 15, 63, 16, -1, -1,
190:                    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
191:                    -1, -1, -1, -1, -1, -1, -1, -1, -1, 17, -1, 18, 19, 21, 20,
192:                    26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
193:                    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 22, 23, 24, 25 };
194:
195:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.