ICE 3.4.2
[php5-ice-freebsdport.git] / java / src / IceUtilInternal / Base64.java
blobc4df90123243ec9ebdc26dc28fc5f6fb37a0f95a
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 package IceUtilInternal;
12 public class Base64
15 public static String
16 encode(byte[] plainSeq)
18 if(plainSeq == null || plainSeq.length == 0)
20 return "";
23 int base64Bytes = (((plainSeq.length * 4) / 3) + 1);
24 int newlineBytes = (((base64Bytes * 2) / 76) + 1);
25 int totalBytes = base64Bytes + newlineBytes;
27 StringBuilder retval = new StringBuilder(totalBytes);
29 int by1;
30 int by2;
31 int by3;
32 int by4;
33 int by5;
34 int by6;
35 int by7;
37 for(int i = 0; i < plainSeq.length; i += 3)
39 by1 = plainSeq[i] & 0xff;
40 by2 = 0;
41 by3 = 0;
43 if((i + 1) < plainSeq.length)
45 by2 = plainSeq[i+1] & 0xff;
48 if((i + 2) < plainSeq.length)
50 by3 = plainSeq[i+2] & 0xff;
53 by4 = (by1 >> 2) & 0xff;
54 by5 = (((by1 & 0x3) << 4) | (by2 >> 4)) & 0xff;
55 by6 = (((by2 & 0xf) << 2) | (by3 >> 6)) & 0xff;
56 by7 = by3 & 0x3f;
58 retval.append(encode((byte)by4));
59 retval.append(encode((byte)by5));
61 if((i + 1) < plainSeq.length)
63 retval.append(encode((byte)by6));
65 else
67 retval.append('=');
70 if((i + 2) < plainSeq.length)
72 retval.append(encode((byte)by7));
74 else
76 retval.append('=');
80 StringBuilder outString = new StringBuilder(totalBytes);
81 int iter = 0;
83 while((retval.length() - iter) > 76)
85 outString.append(retval.substring(iter, iter + 76));
86 outString.append("\r\n");
87 iter += 76;
90 outString.append(retval.substring(iter));
92 return outString.toString();
95 public static byte[]
96 decode(String str)
98 StringBuilder newStr = new StringBuilder(str.length());
100 for(int j = 0; j < str.length(); j++)
102 char c = str.charAt(j);
103 if(isBase64(c))
105 newStr.append(c);
109 if(newStr.length() == 0)
111 return null;
114 // Note: This is how we were previously computing the size of the return
115 // sequence. The method below is more efficient (and correct).
116 // size_t lines = str.size() / 78;
117 // size_t totalBytes = (lines * 76) + (((str.size() - (lines * 78)) * 3) / 4);
119 // Figure out how long the final sequence is going to be.
120 int totalBytes = (newStr.length() * 3 / 4) + 1;
122 java.nio.ByteBuffer retval = java.nio.ByteBuffer.allocate(totalBytes);
124 int by1;
125 int by2;
126 int by3;
127 int by4;
129 char c1, c2, c3, c4;
131 int pos = 0;
132 for(int i = 0; i < newStr.length(); i += 4)
134 c1 = 'A';
135 c2 = 'A';
136 c3 = 'A';
137 c4 = 'A';
139 c1 = newStr.charAt(i);
141 if((i + 1) < newStr.length())
143 c2 = newStr.charAt(i + 1);
146 if((i + 2) < newStr.length())
148 c3 = newStr.charAt(i + 2);
151 if((i + 3) < newStr.length())
153 c4 = newStr.charAt(i + 3);
156 by1 = decode(c1) & 0xff;
157 by2 = decode(c2) & 0xff;
158 by3 = decode(c3) & 0xff;
159 by4 = decode(c4) & 0xff;
161 retval.put((byte)((by1 << 2) | (by2 >> 4)));
162 ++pos;
164 if(c3 != '=')
166 retval.put((byte)(((by2 & 0xf) << 4) | (by3 >> 2)));
167 ++pos;
170 if(c4 != '=')
172 retval.put((byte)(((by3 & 0x3) << 6) | by4));
173 ++pos;
177 byte[] arr = new byte[pos];
178 System.arraycopy(retval.array(), 0, arr, 0, pos);
179 return arr;
182 public static boolean
183 isBase64(char c)
185 if(c >= 'A' && c <= 'Z')
187 return true;
190 if(c >= 'a' && c <= 'z')
192 return true;
195 if(c >= '0' && c <= '9')
197 return true;
200 if(c == '+')
202 return true;
205 if(c == '/')
207 return true;
210 if(c == '=')
212 return true;
215 return false;
218 private static char
219 encode(byte uc)
221 if(uc < 26)
223 return (char)('A' + uc);
226 if(uc < 52)
228 return (char)('a' + (uc - 26));
231 if(uc < 62)
233 return (char)('0' + (uc - 52));
236 if(uc == 62)
238 return '+';
241 return '/';
244 private static byte
245 decode(char c)
247 if(c >= 'A' && c <= 'Z')
249 return (byte)(c - 'A');
252 if(c >= 'a' && c <= 'z')
254 return (byte)(c - 'a' + 26);
257 if(c >= '0' && c <= '9')
259 return (byte)(c - '0' + 52);
262 if(c == '+')
264 return 62;
267 return 63;