forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase64EncodingOutputBuffer.java
More file actions
141 lines (110 loc) · 4.8 KB
/
Copy pathBase64EncodingOutputBuffer.java
File metadata and controls
141 lines (110 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*-*- mode: Java; tab-width:8 -*-*/
/*
* Based on Base64.java:
*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package php.java.bridge;
class Base64EncodingOutputBuffer extends HexOutputBuffer {
static private final int LOOKUPLENGTH = 64;
static private final int TWENTYFOURBITGROUP = 24;
static private final int EIGHTBIT = 8;
static private final int SIXTEENBIT = 16;
static private final int SIGN = -128;
static private final char PAD = '=';
static final private char [] lookUpBase64Alphabet = new char[LOOKUPLENGTH];
static {
for (int i = 0; i<=25; i++)
lookUpBase64Alphabet[i] = (char)('A'+i);
for (int i = 26, j = 0; i<=51; i++, j++)
lookUpBase64Alphabet[i] = (char)('a'+ j);
for (int i = 52, j = 0; i<=61; i++, j++)
lookUpBase64Alphabet[i] = (char)('0' + j);
lookUpBase64Alphabet[62] = (char)'+';
lookUpBase64Alphabet[63] = (char)'/';
}
Base64EncodingOutputBuffer(JavaBridge bridge) {
super(bridge);
}
void appendBase64(byte binaryData[]) {
if (binaryData == null)
return ;
int lengthDataBits = binaryData.length*EIGHTBIT;
if (lengthDataBits == 0) {
return ;
}
int fewerThan24bits = lengthDataBits%TWENTYFOURBITGROUP;
int numberTriplets = lengthDataBits/TWENTYFOURBITGROUP;
int numberQuartet = fewerThan24bits != 0 ? numberTriplets+1 : numberTriplets;
int numberLines = (numberQuartet-1)/19+1;
byte k=0, l=0, b1=0,b2=0,b3=0;
int dataIndex = 0;
int i = 0;
for (int line = 0; line < numberLines-1; line++) {
for (int quartet = 0; quartet < 19; quartet++) {
b1 = binaryData[dataIndex++];
b2 = binaryData[dataIndex++];
b3 = binaryData[dataIndex++];
l = (byte)(b2 & 0x0f);
k = (byte)(b1 & 0x03);
byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0);
byte val3 = ((b3 & SIGN)==0)?(byte)(b3>>6):(byte)((b3)>>6^0xfc);
write(lookUpBase64Alphabet[ val1 ]);
write(lookUpBase64Alphabet[ val2 | ( k<<4 )]);
write(lookUpBase64Alphabet[ (l <<2 ) | val3 ]);
write(lookUpBase64Alphabet[ b3 & 0x3f ]);
i++;
}
write(0xa);
}
for (; i<numberTriplets; i++) {
b1 = binaryData[dataIndex++];
b2 = binaryData[dataIndex++];
b3 = binaryData[dataIndex++];
l = (byte)(b2 & 0x0f);
k = (byte)(b1 & 0x03);
byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0);
byte val3 = ((b3 & SIGN)==0)?(byte)(b3>>6):(byte)((b3)>>6^0xfc);
write(lookUpBase64Alphabet[ val1 ]);
write(lookUpBase64Alphabet[ val2 | ( k<<4 )]);
write(lookUpBase64Alphabet[ (l <<2 ) | val3 ]);
write(lookUpBase64Alphabet[ b3 & 0x3f ]);
}
// form integral number of 6-bit groups
if (fewerThan24bits == EIGHTBIT) {
b1 = binaryData[dataIndex];
k = (byte) ( b1 &0x03 );
byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
write(lookUpBase64Alphabet[ val1 ]);
write(lookUpBase64Alphabet[ k<<4 ]);
write(PAD);
write(PAD);
} else if (fewerThan24bits == SIXTEENBIT) {
b1 = binaryData[dataIndex];
b2 = binaryData[dataIndex +1 ];
l = ( byte ) ( b2 &0x0f );
k = ( byte ) ( b1 &0x03 );
byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0);
write(lookUpBase64Alphabet[ val1 ]);
write(lookUpBase64Alphabet[ val2 | ( k<<4 )]);
write(lookUpBase64Alphabet[ l<<2 ]);
write(PAD);
}
write(0xa);
}
}