Repository navigation
Expand file tree
/
Copy pathUtils.java
More file actions
282 lines (265 loc) · 8.27 KB
/
Copy pathUtils.java
File metadata and controls
282 lines (265 loc) · 8.27 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.jute;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* Various utility functions for Hadoop record I/O runtime.
*/
public class Utils {
/** Cannot create a new instance of Utils */
private Utils() {
super();
}
/**
* equals function that actually compares two buffers.
*
* @param onearray First buffer
* @param twoarray Second buffer
* @return true if one and two contain exactly the same content, else false.
*/
public static boolean bufEquals(byte onearray[], byte twoarray[] ) {
if (onearray == twoarray) return true;
boolean ret = (onearray.length == twoarray.length);
if (!ret) {
return ret;
}
for (int idx = 0; idx < onearray.length; idx++) {
if (onearray[idx] != twoarray[idx]) {
return false;
}
}
return true;
}
private static final char[] hexchars = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'A', 'B',
'C', 'D', 'E', 'F' };
/**
*
* @param s
* @return
*/
static String toXMLString(String s) {
if (s == null)
return "";
StringBuilder sb = new StringBuilder();
for (int idx = 0; idx < s.length(); idx++) {
char ch = s.charAt(idx);
if (ch == '<') {
sb.append("<");
} else if (ch == '&') {
sb.append("&");
} else if (ch == '%') {
sb.append("%25");
} else if (ch < 0x20) {
sb.append("%");
sb.append(hexchars[ch/16]);
sb.append(hexchars[ch%16]);
} else {
sb.append(ch);
}
}
return sb.toString();
}
static private int h2c(char ch) {
if (ch >= '0' && ch <= '9') {
return ch - '0';
} else if (ch >= 'A' && ch <= 'F') {
return ch - 'A';
} else if (ch >= 'a' && ch <= 'f') {
return ch - 'a';
}
return 0;
}
/**
*
* @param s
* @return
*/
static String fromXMLString(String s) {
StringBuilder sb = new StringBuilder();
for (int idx = 0; idx < s.length();) {
char ch = s.charAt(idx++);
if (ch == '%') {
char ch1 = s.charAt(idx++);
char ch2 = s.charAt(idx++);
char res = (char)(h2c(ch1)*16 + h2c(ch2));
sb.append(res);
} else {
sb.append(ch);
}
}
return sb.toString();
}
/**
*
* @param s
* @return
*/
static String toCSVString(String s) {
if (s == null)
return "";
StringBuilder sb = new StringBuilder(s.length()+1);
sb.append('\'');
int len = s.length();
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
switch(c) {
case '\0':
sb.append("%00");
break;
case '\n':
sb.append("%0A");
break;
case '\r':
sb.append("%0D");
break;
case ',':
sb.append("%2C");
break;
case '}':
sb.append("%7D");
break;
case '%':
sb.append("%25");
break;
default:
sb.append(c);
}
}
return sb.toString();
}
/**
*
* @param s
* @throws java.io.IOException
* @return
*/
static String fromCSVString(String s) throws IOException {
if (s.charAt(0) != '\'') {
throw new IOException("Error deserializing string.");
}
int len = s.length();
StringBuilder sb = new StringBuilder(len-1);
for (int i = 1; i < len; i++) {
char c = s.charAt(i);
if (c == '%') {
char ch1 = s.charAt(i+1);
char ch2 = s.charAt(i+2);
i += 2;
if (ch1 == '0' && ch2 == '0') { sb.append('\0'); }
else if (ch1 == '0' && ch2 == 'A') { sb.append('\n'); }
else if (ch1 == '0' && ch2 == 'D') { sb.append('\r'); }
else if (ch1 == '2' && ch2 == 'C') { sb.append(','); }
else if (ch1 == '7' && ch2 == 'D') { sb.append('}'); }
else if (ch1 == '2' && ch2 == '5') { sb.append('%'); }
else {throw new IOException("Error deserializing string.");}
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
*
* @param s
* @return
*/
static String toXMLBuffer(byte barr[]) {
if (barr == null || barr.length == 0) {
return "";
}
StringBuilder sb = new StringBuilder(2*barr.length);
for (int idx = 0; idx < barr.length; idx++) {
sb.append(Integer.toHexString(barr[idx]));
}
return sb.toString();
}
/**
*
* @param s
* @throws java.io.IOException
* @return
*/
static byte[] fromXMLBuffer(String s)
throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
if (s.length() == 0) { return stream.toByteArray(); }
int blen = s.length()/2;
byte[] barr = new byte[blen];
for (int idx = 0; idx < blen; idx++) {
char c1 = s.charAt(2*idx);
char c2 = s.charAt(2*idx+1);
barr[idx] = Byte.parseByte(""+c1+c2, 16);
}
stream.write(barr);
return stream.toByteArray();
}
/**
*
* @param buf
* @return
*/
static String toCSVBuffer(byte barr[]) {
if (barr == null || barr.length == 0) {
return "";
}
StringBuilder sb = new StringBuilder(barr.length + 1);
sb.append('#');
for(int idx = 0; idx < barr.length; idx++) {
sb.append(Integer.toHexString(barr[idx]));
}
return sb.toString();
}
/**
* Converts a CSV-serialized representation of buffer to a new
* ByteArrayOutputStream.
* @param s CSV-serialized representation of buffer
* @throws java.io.IOException
* @return Deserialized ByteArrayOutputStream
*/
static byte[] fromCSVBuffer(String s)
throws IOException {
if (s.charAt(0) != '#') {
throw new IOException("Error deserializing buffer.");
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
if (s.length() == 1) { return stream.toByteArray(); }
int blen = (s.length()-1)/2;
byte[] barr = new byte[blen];
for (int idx = 0; idx < blen; idx++) {
char c1 = s.charAt(2*idx+1);
char c2 = s.charAt(2*idx+2);
barr[idx] = Byte.parseByte(""+c1+c2, 16);
}
stream.write(barr);
return stream.toByteArray();
}
public static int compareBytes(byte b1[], int off1, int len1, byte b2[], int off2, int len2) {
int i;
for(i=0; i < len1 && i < len2; i++) {
if (b1[off1+i] != b2[off2+i]) {
return b1[off1+i] < b2[off2+i] ? -1 : 1;
}
}
if (len1 != len2) {
return len1 < len2 ? -1 : 1;
}
return 0;
}
}