1 // **********************************************************************
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
8 // **********************************************************************
10 package IceUtilInternal
;
16 encode(byte[] plainSeq
)
18 if(plainSeq
== null || plainSeq
.length
== 0)
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
);
37 for(int i
= 0; i
< plainSeq
.length
; i
+= 3)
39 by1
= plainSeq
[i
] & 0xff;
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;
58 retval
.append(encode((byte)by4
));
59 retval
.append(encode((byte)by5
));
61 if((i
+ 1) < plainSeq
.length
)
63 retval
.append(encode((byte)by6
));
70 if((i
+ 2) < plainSeq
.length
)
72 retval
.append(encode((byte)by7
));
80 StringBuilder outString
= new StringBuilder(totalBytes
);
83 while((retval
.length() - iter
) > 76)
85 outString
.append(retval
.substring(iter
, iter
+ 76));
86 outString
.append("\r\n");
90 outString
.append(retval
.substring(iter
));
92 return outString
.toString();
98 StringBuilder newStr
= new StringBuilder(str
.length());
100 for(int j
= 0; j
< str
.length(); j
++)
102 char c
= str
.charAt(j
);
109 if(newStr
.length() == 0)
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
);
132 for(int i
= 0; i
< newStr
.length(); i
+= 4)
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)));
166 retval
.put((byte)(((by2
& 0xf) << 4) | (by3
>> 2)));
172 retval
.put((byte)(((by3
& 0x3) << 6) | by4
));
177 byte[] arr
= new byte[pos
];
178 System
.arraycopy(retval
.array(), 0, arr
, 0, pos
);
182 public static boolean
185 if(c
>= 'A' && c
<= 'Z')
190 if(c
>= 'a' && c
<= 'z')
195 if(c
>= '0' && c
<= '9')
223 return (char)('A' + uc
);
228 return (char)('a' + (uc
- 26));
233 return (char)('0' + (uc
- 52));
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);