first commit

This commit is contained in:
MaddoScientisto 2026-03-14 20:04:39 +01:00
commit 4d332ef662
27586 changed files with 3281783 additions and 0 deletions

View file

@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
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.

View file

@ -0,0 +1,11 @@
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.1
Created-By: 1.4.2_04-b05 (Sun Microsystems Inc.)
Extension-Name: org.apache.commons.codec
Specification-Title: Jakarta Commons Codec
Specification-Vendor: Apache Software Foundation
Specification-Version: 1.3
Implementation-Title: org.apache.commons.codec
Implementation-Vendor: Apache Software Foundation
Implementation-Version: 1.3

View file

@ -0,0 +1,5 @@
package org.apache.commons.codec;
public interface BinaryDecoder extends Decoder {
byte[] decode(byte[] paramArrayOfbyte) throws DecoderException;
}

View file

@ -0,0 +1,5 @@
package org.apache.commons.codec;
public interface BinaryEncoder extends Encoder {
byte[] encode(byte[] paramArrayOfbyte) throws EncoderException;
}

View file

@ -0,0 +1,5 @@
package org.apache.commons.codec;
public interface Decoder {
Object decode(Object paramObject) throws DecoderException;
}

View file

@ -0,0 +1,7 @@
package org.apache.commons.codec;
public class DecoderException extends Exception {
public DecoderException(String pMessage) {
super(pMessage);
}
}

View file

@ -0,0 +1,5 @@
package org.apache.commons.codec;
public interface Encoder {
Object encode(Object paramObject) throws EncoderException;
}

View file

@ -0,0 +1,7 @@
package org.apache.commons.codec;
public class EncoderException extends Exception {
public EncoderException(String pMessage) {
super(pMessage);
}
}

View file

@ -0,0 +1,5 @@
package org.apache.commons.codec;
public interface StringDecoder extends Decoder {
String decode(String paramString) throws DecoderException;
}

View file

@ -0,0 +1,5 @@
package org.apache.commons.codec;
public interface StringEncoder extends Encoder {
String encode(String paramString) throws EncoderException;
}

View file

@ -0,0 +1,25 @@
package org.apache.commons.codec;
import java.util.Comparator;
public class StringEncoderComparator implements Comparator {
private StringEncoder stringEncoder;
public StringEncoderComparator() {}
public StringEncoderComparator(StringEncoder stringEncoder) {
this.stringEncoder = stringEncoder;
}
public int compare(Object o1, Object o2) {
int compareCode = 0;
try {
Comparable s1 = (Comparable)this.stringEncoder.encode(o1);
Comparable s2 = (Comparable)this.stringEncoder.encode(o2);
compareCode = s1.compareTo(s2);
} catch (EncoderException ee) {
compareCode = 0;
}
return compareCode;
}
}

View file

@ -0,0 +1,245 @@
package org.apache.commons.codec.binary;
import org.apache.commons.codec.BinaryDecoder;
import org.apache.commons.codec.BinaryEncoder;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
public class Base64 implements BinaryEncoder, BinaryDecoder {
static final int CHUNK_SIZE = 76;
static final byte[] CHUNK_SEPARATOR = "\r\n".getBytes();
static final int BASELENGTH = 255;
static final int LOOKUPLENGTH = 64;
static final int EIGHTBIT = 8;
static final int SIXTEENBIT = 16;
static final int TWENTYFOURBITGROUP = 24;
static final int FOURBYTE = 4;
static final int SIGN = -128;
static final byte PAD = 61;
private static byte[] base64Alphabet = new byte[255];
private static byte[] lookUpBase64Alphabet = new byte[64];
static {
for (int i3 = 0; i3 < 255; i3++)
base64Alphabet[i3] = -1;
for (int i2 = 90; i2 >= 65; i2--)
base64Alphabet[i2] = (byte)(i2 - 65);
for (int i1 = 122; i1 >= 97; i1--)
base64Alphabet[i1] = (byte)(i1 - 97 + 26);
for (int n = 57; n >= 48; n--)
base64Alphabet[n] = (byte)(n - 48 + 52);
base64Alphabet[43] = 62;
base64Alphabet[47] = 63;
for (int m = 0; m <= 25; m++)
lookUpBase64Alphabet[m] = (byte)(65 + m);
for (int k = 26, i4 = 0; k <= 51; k++, i4++)
lookUpBase64Alphabet[k] = (byte)(97 + i4);
for (int i = 52, j = 0; i <= 61; i++, j++)
lookUpBase64Alphabet[i] = (byte)(48 + j);
lookUpBase64Alphabet[62] = 43;
lookUpBase64Alphabet[63] = 47;
}
private static boolean isBase64(byte octect) {
if (octect == 61)
return true;
if (base64Alphabet[octect] == -1)
return false;
return true;
}
public static boolean isArrayByteBase64(byte[] arrayOctect) {
arrayOctect = discardWhitespace(arrayOctect);
int length = arrayOctect.length;
if (length == 0)
return true;
for (int i = 0; i < length; i++) {
if (!isBase64(arrayOctect[i]))
return false;
}
return true;
}
public static byte[] encodeBase64(byte[] binaryData) {
return encodeBase64(binaryData, false);
}
public static byte[] encodeBase64Chunked(byte[] binaryData) {
return encodeBase64(binaryData, true);
}
public Object decode(Object pObject) throws DecoderException {
if (!(pObject instanceof byte[]))
throw new DecoderException("Parameter supplied to Base64 decode is not a byte[]");
return decode((byte[])pObject);
}
public byte[] decode(byte[] pArray) {
return decodeBase64(pArray);
}
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) {
int lengthDataBits = binaryData.length * 8;
int fewerThan24bits = lengthDataBits % 24;
int numberTriplets = lengthDataBits / 24;
byte[] encodedData = null;
int encodedDataLength = 0;
int nbrChunks = 0;
if (fewerThan24bits != 0) {
encodedDataLength = (numberTriplets + 1) * 4;
} else {
encodedDataLength = numberTriplets * 4;
}
if (isChunked) {
nbrChunks = (CHUNK_SEPARATOR.length == 0) ? 0 : (int)Math.ceil((double)((float)encodedDataLength / 76.0F));
encodedDataLength += nbrChunks * CHUNK_SEPARATOR.length;
}
encodedData = new byte[encodedDataLength];
byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;
int encodedIndex = 0;
int dataIndex = 0;
int i = 0;
int nextSeparatorIndex = 76;
int chunksSoFar = 0;
for (i = 0; i < numberTriplets; i++) {
dataIndex = i * 3;
b1 = binaryData[dataIndex];
b2 = binaryData[dataIndex + 1];
b3 = binaryData[dataIndex + 2];
l = (byte)(b2 & 0xF);
k = (byte)(b1 & 0x3);
byte val1 = ((b1 & Byte.MIN_VALUE) == 0) ? (byte)(b1 >> 2) : (byte)(b1 >> 2 ^ 0xC0);
byte val2 = ((b2 & Byte.MIN_VALUE) == 0) ? (byte)(b2 >> 4) : (byte)(b2 >> 4 ^ 0xF0);
byte val3 = ((b3 & Byte.MIN_VALUE) == 0) ? (byte)(b3 >> 6) : (byte)(b3 >> 6 ^ 0xFC);
encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2 | k << 4];
encodedData[encodedIndex + 2] = lookUpBase64Alphabet[l << 2 | val3];
encodedData[encodedIndex + 3] = lookUpBase64Alphabet[b3 & 0x3F];
encodedIndex += 4;
if (isChunked)
if (encodedIndex == nextSeparatorIndex) {
System.arraycopy(CHUNK_SEPARATOR, 0, encodedData, encodedIndex, CHUNK_SEPARATOR.length);
chunksSoFar++;
nextSeparatorIndex = 76 * (chunksSoFar + 1) + chunksSoFar * CHUNK_SEPARATOR.length;
encodedIndex += CHUNK_SEPARATOR.length;
}
}
dataIndex = i * 3;
if (fewerThan24bits == 8) {
b1 = binaryData[dataIndex];
k = (byte)(b1 & 0x3);
byte val1 = ((b1 & Byte.MIN_VALUE) == 0) ? (byte)(b1 >> 2) : (byte)(b1 >> 2 ^ 0xC0);
encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
encodedData[encodedIndex + 1] = lookUpBase64Alphabet[k << 4];
encodedData[encodedIndex + 2] = 61;
encodedData[encodedIndex + 3] = 61;
} else if (fewerThan24bits == 16) {
b1 = binaryData[dataIndex];
b2 = binaryData[dataIndex + 1];
l = (byte)(b2 & 0xF);
k = (byte)(b1 & 0x3);
byte val1 = ((b1 & Byte.MIN_VALUE) == 0) ? (byte)(b1 >> 2) : (byte)(b1 >> 2 ^ 0xC0);
byte val2 = ((b2 & Byte.MIN_VALUE) == 0) ? (byte)(b2 >> 4) : (byte)(b2 >> 4 ^ 0xF0);
encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2 | k << 4];
encodedData[encodedIndex + 2] = lookUpBase64Alphabet[l << 2];
encodedData[encodedIndex + 3] = 61;
}
if (isChunked)
if (chunksSoFar < nbrChunks)
System.arraycopy(CHUNK_SEPARATOR, 0, encodedData, encodedDataLength - CHUNK_SEPARATOR.length, CHUNK_SEPARATOR.length);
return encodedData;
}
public static byte[] decodeBase64(byte[] base64Data) {
base64Data = discardNonBase64(base64Data);
if (base64Data.length == 0)
return new byte[0];
int numberQuadruple = base64Data.length / 4;
byte[] decodedData = null;
byte b1 = 0, b2 = 0, b3 = 0, b4 = 0, marker0 = 0, marker1 = 0;
int encodedIndex = 0;
int dataIndex = 0;
int lastData = base64Data.length;
while (base64Data[lastData - 1] == 61) {
if (--lastData == 0)
return new byte[0];
}
decodedData = new byte[lastData - numberQuadruple];
for (int i = 0; i < numberQuadruple; i++) {
dataIndex = i * 4;
marker0 = base64Data[dataIndex + 2];
marker1 = base64Data[dataIndex + 3];
b1 = base64Alphabet[base64Data[dataIndex]];
b2 = base64Alphabet[base64Data[dataIndex + 1]];
if (marker0 != 61 && marker1 != 61) {
b3 = base64Alphabet[marker0];
b4 = base64Alphabet[marker1];
decodedData[encodedIndex] = (byte)(b1 << 2 | b2 >> 4);
decodedData[encodedIndex + 1] = (byte)((b2 & 0xF) << 4 | b3 >> 2 & 0xF);
decodedData[encodedIndex + 2] = (byte)(b3 << 6 | b4);
} else if (marker0 == 61) {
decodedData[encodedIndex] = (byte)(b1 << 2 | b2 >> 4);
} else if (marker1 == 61) {
b3 = base64Alphabet[marker0];
decodedData[encodedIndex] = (byte)(b1 << 2 | b2 >> 4);
decodedData[encodedIndex + 1] = (byte)((b2 & 0xF) << 4 | b3 >> 2 & 0xF);
}
encodedIndex += 3;
}
return decodedData;
}
static byte[] discardWhitespace(byte[] data) {
byte[] groomedData = new byte[data.length];
int bytesCopied = 0;
for (int i = 0; i < data.length; i++) {
switch (data[i]) {
case 9:
case 10:
case 13:
case 32:
break;
default:
groomedData[bytesCopied++] = data[i];
break;
}
}
byte[] packedData = new byte[bytesCopied];
System.arraycopy(groomedData, 0, packedData, 0, bytesCopied);
return packedData;
}
static byte[] discardNonBase64(byte[] data) {
byte[] groomedData = new byte[data.length];
int bytesCopied = 0;
for (int i = 0; i < data.length; i++) {
if (isBase64(data[i]))
groomedData[bytesCopied++] = data[i];
}
byte[] packedData = new byte[bytesCopied];
System.arraycopy(groomedData, 0, packedData, 0, bytesCopied);
return packedData;
}
public Object encode(Object pObject) throws EncoderException {
if (!(pObject instanceof byte[]))
throw new EncoderException("Parameter supplied to Base64 encode is not a byte[]");
return encode((byte[])pObject);
}
public byte[] encode(byte[] pArray) {
return encodeBase64(pArray, false);
}
}

View file

@ -0,0 +1,124 @@
package org.apache.commons.codec.binary;
import org.apache.commons.codec.BinaryDecoder;
import org.apache.commons.codec.BinaryEncoder;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
public class BinaryCodec implements BinaryDecoder, BinaryEncoder {
private static final char[] EMPTY_CHAR_ARRAY = new char[0];
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
private static final int BIT_0 = 1;
private static final int BIT_1 = 2;
private static final int BIT_2 = 4;
private static final int BIT_3 = 8;
private static final int BIT_4 = 16;
private static final int BIT_5 = 32;
private static final int BIT_6 = 64;
private static final int BIT_7 = 128;
private static final int[] BITS = new int[] { 1, 2, 4, 8, 16, 32, 64, 128 };
public byte[] encode(byte[] raw) {
return toAsciiBytes(raw);
}
public Object encode(Object raw) throws EncoderException {
if (!(raw instanceof byte[]))
throw new EncoderException("argument not a byte array");
return toAsciiChars((byte[])raw);
}
public Object decode(Object ascii) throws DecoderException {
if (ascii == null)
return EMPTY_BYTE_ARRAY;
if (ascii instanceof byte[])
return fromAscii((byte[])ascii);
if (ascii instanceof char[])
return fromAscii((char[])ascii);
if (ascii instanceof String)
return fromAscii(((String)ascii).toCharArray());
throw new DecoderException("argument not a byte array");
}
public byte[] decode(byte[] ascii) {
return fromAscii(ascii);
}
public byte[] toByteArray(String ascii) {
if (ascii == null)
return EMPTY_BYTE_ARRAY;
return fromAscii(ascii.toCharArray());
}
public static byte[] fromAscii(char[] ascii) {
if (ascii == null || ascii.length == 0)
return EMPTY_BYTE_ARRAY;
byte[] l_raw = new byte[ascii.length >> 3];
for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) {
for (int bits = 0; bits < BITS.length; bits++) {
if (ascii[jj - bits] == '1')
l_raw[ii] = (byte)(l_raw[ii] | BITS[bits]);
}
}
return l_raw;
}
public static byte[] fromAscii(byte[] ascii) {
if (ascii == null || ascii.length == 0)
return EMPTY_BYTE_ARRAY;
byte[] l_raw = new byte[ascii.length >> 3];
for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) {
for (int bits = 0; bits < BITS.length; bits++) {
if (ascii[jj - bits] == 49)
l_raw[ii] = (byte)(l_raw[ii] | BITS[bits]);
}
}
return l_raw;
}
public static byte[] toAsciiBytes(byte[] raw) {
if (raw == null || raw.length == 0)
return EMPTY_BYTE_ARRAY;
byte[] l_ascii = new byte[raw.length << 3];
for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) {
for (int bits = 0; bits < BITS.length; bits++) {
if ((raw[ii] & BITS[bits]) == 0) {
l_ascii[jj - bits] = 48;
} else {
l_ascii[jj - bits] = 49;
}
}
}
return l_ascii;
}
public static char[] toAsciiChars(byte[] raw) {
if (raw == null || raw.length == 0)
return EMPTY_CHAR_ARRAY;
char[] l_ascii = new char[raw.length << 3];
for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) {
for (int bits = 0; bits < BITS.length; bits++) {
if ((raw[ii] & BITS[bits]) == 0) {
l_ascii[jj - bits] = '0';
} else {
l_ascii[jj - bits] = '1';
}
}
}
return l_ascii;
}
public static String toAsciiString(byte[] raw) {
return new String(toAsciiChars(raw));
}
}

View file

@ -0,0 +1,70 @@
package org.apache.commons.codec.binary;
import org.apache.commons.codec.BinaryDecoder;
import org.apache.commons.codec.BinaryEncoder;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
public class Hex implements BinaryEncoder, BinaryDecoder {
private static final char[] DIGITS = new char[] {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
public static byte[] decodeHex(char[] data) throws DecoderException {
int len = data.length;
if ((len & 0x1) != 0)
throw new DecoderException("Odd number of characters.");
byte[] out = new byte[len >> 1];
for (int i = 0, j = 0; j < len; i++) {
int f = toDigit(data[j], j) << 4;
j++;
f |= toDigit(data[j], j);
j++;
out[i] = (byte)(f & 0xFF);
}
return out;
}
protected static int toDigit(char ch, int index) throws DecoderException {
int digit = Character.digit(ch, 16);
if (digit == -1)
throw new DecoderException("Illegal hexadecimal charcter " + ch + " at index " + index);
return digit;
}
public static char[] encodeHex(byte[] data) {
int l = data.length;
char[] out = new char[l << 1];
for (int i = 0, j = 0; i < l; i++) {
out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
out[j++] = DIGITS[0xF & data[i]];
}
return out;
}
public byte[] decode(byte[] array) throws DecoderException {
return decodeHex(new String(array).toCharArray());
}
public Object decode(Object object) throws DecoderException {
try {
char[] charArray = (object instanceof String) ? ((String)object).toCharArray() : (char[])object;
return decodeHex(charArray);
} catch (ClassCastException e) {
throw new DecoderException(e.getMessage());
}
}
public byte[] encode(byte[] array) {
return new String(encodeHex(array)).getBytes();
}
public Object encode(Object object) throws EncoderException {
try {
byte[] byteArray = (object instanceof String) ? ((String)object).getBytes() : (byte[])object;
return encodeHex(byteArray);
} catch (ClassCastException e) {
throw new EncoderException(e.getMessage());
}
}
}

View file

@ -0,0 +1,20 @@
<!--
Copyright 2003-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.
-->
<html>
<body>
Base64, Binary, and Hexadecimal String encoding and decoding.
</body>
</html>

View file

@ -0,0 +1,55 @@
package org.apache.commons.codec.digest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.binary.Hex;
public class DigestUtils {
static MessageDigest getDigest(String algorithm) {
try {
return MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e.getMessage());
}
}
private static MessageDigest getMd5Digest() {
return getDigest("MD5");
}
private static MessageDigest getShaDigest() {
return getDigest("SHA");
}
public static byte[] md5(byte[] data) {
return getMd5Digest().digest(data);
}
public static byte[] md5(String data) {
return md5(data.getBytes());
}
public static String md5Hex(byte[] data) {
return new String(Hex.encodeHex(md5(data)));
}
public static String md5Hex(String data) {
return new String(Hex.encodeHex(md5(data)));
}
public static byte[] sha(byte[] data) {
return getShaDigest().digest(data);
}
public static byte[] sha(String data) {
return sha(data.getBytes());
}
public static String shaHex(byte[] data) {
return new String(Hex.encodeHex(sha(data)));
}
public static String shaHex(String data) {
return new String(Hex.encodeHex(sha(data)));
}
}

View file

@ -0,0 +1,20 @@
<!--
Copyright 2003-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.
-->
<html>
<body>
Operations to simplifiy common <code>MessageDigest</code> tasks.
</body>
</html>

View file

@ -0,0 +1,706 @@
package org.apache.commons.codec.language;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringEncoder;
public class DoubleMetaphone implements StringEncoder {
private static final String VOWELS = "AEIOUY";
private static final String[] SILENT_START = new String[] { "GN", "KN", "PN", "WR", "PS" };
private static final String[] L_R_N_M_B_H_F_V_W_SPACE = new String[] { "L", "R", "N", "M", "B", "H", "F", "V", "W", " " };
private static final String[] ES_EP_EB_EL_EY_IB_IL_IN_IE_EI_ER = new String[] {
"ES", "EP", "EB", "EL", "EY", "IB", "IL", "IN", "IE", "EI",
"ER" };
private static final String[] L_T_K_S_N_M_B_Z = new String[] { "L", "T", "K", "S", "N", "M", "B", "Z" };
protected int maxCodeLen = 4;
public String doubleMetaphone(String value) {
return doubleMetaphone(value, false);
}
public String doubleMetaphone(String value, boolean alternate) {
value = cleanInput(value);
if (value == null)
return null;
boolean slavoGermanic = isSlavoGermanic(value);
int index = isSilentStart(value) ? 1 : 0;
DoubleMetaphoneResult result = new DoubleMetaphoneResult(this, getMaxCodeLen());
while (!result.isComplete() && index <= value.length() - 1) {
switch (value.charAt(index)) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'Y':
index = handleAEIOUY(value, result, index);
continue;
case 'B':
result.append('P');
index = (charAt(value, index + 1) == 'B') ? (index + 2) : (index + 1);
continue;
case 'Ç':
result.append('S');
index++;
continue;
case 'C':
index = handleC(value, result, index);
continue;
case 'D':
index = handleD(value, result, index);
continue;
case 'F':
result.append('F');
index = (charAt(value, index + 1) == 'F') ? (index + 2) : (index + 1);
continue;
case 'G':
index = handleG(value, result, index, slavoGermanic);
continue;
case 'H':
index = handleH(value, result, index);
continue;
case 'J':
index = handleJ(value, result, index, slavoGermanic);
continue;
case 'K':
result.append('K');
index = (charAt(value, index + 1) == 'K') ? (index + 2) : (index + 1);
continue;
case 'L':
index = handleL(value, result, index);
continue;
case 'M':
result.append('M');
index = conditionM0(value, index) ? (index + 2) : (index + 1);
continue;
case 'N':
result.append('N');
index = (charAt(value, index + 1) == 'N') ? (index + 2) : (index + 1);
continue;
case 'Ñ':
result.append('N');
index++;
continue;
case 'P':
index = handleP(value, result, index);
continue;
case 'Q':
result.append('K');
index = (charAt(value, index + 1) == 'Q') ? (index + 2) : (index + 1);
continue;
case 'R':
index = handleR(value, result, index, slavoGermanic);
continue;
case 'S':
index = handleS(value, result, index, slavoGermanic);
continue;
case 'T':
index = handleT(value, result, index);
continue;
case 'V':
result.append('F');
index = (charAt(value, index + 1) == 'V') ? (index + 2) : (index + 1);
continue;
case 'W':
index = handleW(value, result, index);
continue;
case 'X':
index = handleX(value, result, index);
continue;
case 'Z':
index = handleZ(value, result, index, slavoGermanic);
continue;
}
index++;
}
return alternate ? result.getAlternate() : result.getPrimary();
}
public Object encode(Object obj) throws EncoderException {
if (!(obj instanceof String))
throw new EncoderException("DoubleMetaphone encode parameter is not of type String");
return doubleMetaphone((String)obj);
}
public String encode(String value) {
return doubleMetaphone(value);
}
public boolean isDoubleMetaphoneEqual(String value1, String value2) {
return isDoubleMetaphoneEqual(value1, value2, false);
}
public boolean isDoubleMetaphoneEqual(String value1, String value2, boolean alternate) {
return doubleMetaphone(value1, alternate).equals(doubleMetaphone(value2, alternate));
}
public int getMaxCodeLen() {
return this.maxCodeLen;
}
public void setMaxCodeLen(int maxCodeLen) {
this.maxCodeLen = maxCodeLen;
}
private int handleAEIOUY(String value, DoubleMetaphoneResult result, int index) {
if (index == 0)
result.append('A');
return index + 1;
}
private int handleC(String value, DoubleMetaphoneResult result, int index) {
if (conditionC0(value, index)) {
result.append('K');
index += 2;
} else if (index == 0 && contains(value, index, 6, "CAESAR")) {
result.append('S');
index += 2;
} else if (contains(value, index, 2, "CH")) {
index = handleCH(value, result, index);
} else if (contains(value, index, 2, "CZ") && !contains(value, index - 2, 4, "WICZ")) {
result.append('S', 'X');
index += 2;
} else if (contains(value, index + 1, 3, "CIA")) {
result.append('X');
index += 3;
} else {
if (contains(value, index, 2, "CC") && (index != 1 || charAt(value, 0) != 'M'))
return handleCC(value, result, index);
if (contains(value, index, 2, "CK", "CG", "CQ")) {
result.append('K');
index += 2;
} else if (contains(value, index, 2, "CI", "CE", "CY")) {
if (contains(value, index, 3, "CIO", "CIE", "CIA")) {
result.append('S', 'X');
} else {
result.append('S');
}
index += 2;
} else {
result.append('K');
if (contains(value, index + 1, 2, " C", " Q", " G")) {
index += 3;
} else if (contains(value, index + 1, 1, "C", "K", "Q") && !contains(value, index + 1, 2, "CE", "CI")) {
index += 2;
} else {
index++;
}
}
}
return index;
}
private int handleCC(String value, DoubleMetaphoneResult result, int index) {
if (contains(value, index + 2, 1, "I", "E", "H") && !contains(value, index + 2, 2, "HU")) {
if ((index == 1 && charAt(value, index - 1) == 'A') || contains(value, index - 1, 5, "UCCEE", "UCCES")) {
result.append("KS");
} else {
result.append('X');
}
index += 3;
} else {
result.append('K');
index += 2;
}
return index;
}
private int handleCH(String value, DoubleMetaphoneResult result, int index) {
if (index > 0 && contains(value, index, 4, "CHAE")) {
result.append('K', 'X');
return index + 2;
}
if (conditionCH0(value, index)) {
result.append('K');
return index + 2;
}
if (conditionCH1(value, index)) {
result.append('K');
return index + 2;
}
if (index > 0) {
if (contains(value, 0, 2, "MC")) {
result.append('K');
} else {
result.append('X', 'K');
}
} else {
result.append('X');
}
return index + 2;
}
private int handleD(String value, DoubleMetaphoneResult result, int index) {
if (contains(value, index, 2, "DG")) {
if (contains(value, index + 2, 1, "I", "E", "Y")) {
result.append('J');
index += 3;
} else {
result.append("TK");
index += 2;
}
} else if (contains(value, index, 2, "DT", "DD")) {
result.append('T');
index += 2;
} else {
result.append('T');
index++;
}
return index;
}
private int handleG(String value, DoubleMetaphoneResult result, int index, boolean slavoGermanic) {
if (charAt(value, index + 1) == 'H') {
index = handleGH(value, result, index);
} else if (charAt(value, index + 1) == 'N') {
if (index == 1 && isVowel(charAt(value, 0)) && !slavoGermanic) {
result.append("KN", "N");
} else if (!contains(value, index + 2, 2, "EY") && charAt(value, index + 1) != 'Y' && !slavoGermanic) {
result.append("N", "KN");
} else {
result.append("KN");
}
index += 2;
} else if (contains(value, index + 1, 2, "LI") && !slavoGermanic) {
result.append("KL", "L");
index += 2;
} else if (index == 0 && (charAt(value, index + 1) == 'Y' || contains(value, index + 1, 2, ES_EP_EB_EL_EY_IB_IL_IN_IE_EI_ER))) {
result.append('K', 'J');
index += 2;
} else if ((contains(value, index + 1, 2, "ER") || charAt(value, index + 1) == 'Y') && !contains(value, 0, 6, "DANGER", "RANGER", "MANGER") && !contains(value, index - 1, 1, "E", "I") && !contains(value, index - 1, 3, "RGY", "OGY")) {
result.append('K', 'J');
index += 2;
} else if (contains(value, index + 1, 1, "E", "I", "Y") || contains(value, index - 1, 4, "AGGI", "OGGI")) {
if (contains(value, 0, 4, "VAN ", "VON ") || contains(value, 0, 3, "SCH") || contains(value, index + 1, 2, "ET")) {
result.append('K');
} else if (contains(value, index + 1, 4, "IER")) {
result.append('J');
} else {
result.append('J', 'K');
}
index += 2;
} else if (charAt(value, index + 1) == 'G') {
index += 2;
result.append('K');
} else {
index++;
result.append('K');
}
return index;
}
private int handleGH(String value, DoubleMetaphoneResult result, int index) {
if (index > 0 && !isVowel(charAt(value, index - 1))) {
result.append('K');
index += 2;
} else if (index == 0) {
if (charAt(value, index + 2) == 'I') {
result.append('J');
} else {
result.append('K');
}
index += 2;
} else if ((index > 1 && contains(value, index - 2, 1, "B", "H", "D")) || (index > 2 && contains(value, index - 3, 1, "B", "H", "D")) || (index > 3 && contains(value, index - 4, 1, "B", "H"))) {
index += 2;
} else {
if (index > 2 && charAt(value, index - 1) == 'U' && contains(value, index - 3, 1, "C", "G", "L", "R", "T")) {
result.append('F');
} else if (index > 0 && charAt(value, index - 1) != 'I') {
result.append('K');
}
index += 2;
}
return index;
}
private int handleH(String value, DoubleMetaphoneResult result, int index) {
if ((index == 0 || isVowel(charAt(value, index - 1))) && isVowel(charAt(value, index + 1))) {
result.append('H');
index += 2;
} else {
index++;
}
return index;
}
private int handleJ(String value, DoubleMetaphoneResult result, int index, boolean slavoGermanic) {
if (contains(value, index, 4, "JOSE") || contains(value, 0, 4, "SAN ")) {
if ((index == 0 && charAt(value, index + 4) == ' ') || value.length() == 4 || contains(value, 0, 4, "SAN ")) {
result.append('H');
} else {
result.append('J', 'H');
}
index++;
} else {
if (index == 0 && !contains(value, index, 4, "JOSE")) {
result.append('J', 'A');
} else if (isVowel(charAt(value, index - 1)) && !slavoGermanic && (charAt(value, index + 1) == 'A' || charAt(value, index + 1) == 'O')) {
result.append('J', 'H');
} else if (index == value.length() - 1) {
result.append('J', ' ');
} else if (!contains(value, index + 1, 1, L_T_K_S_N_M_B_Z) && !contains(value, index - 1, 1, "S", "K", "L")) {
result.append('J');
}
if (charAt(value, index + 1) == 'J') {
index += 2;
} else {
index++;
}
}
return index;
}
private int handleL(String value, DoubleMetaphoneResult result, int index) {
result.append('L');
if (charAt(value, index + 1) == 'L') {
if (conditionL0(value, index))
result.appendAlternate(' ');
index += 2;
} else {
index++;
}
return index;
}
private int handleP(String value, DoubleMetaphoneResult result, int index) {
if (charAt(value, index + 1) == 'H') {
result.append('F');
index += 2;
} else {
result.append('P');
index = contains(value, index + 1, 1, "P", "B") ? (index + 2) : (index + 1);
}
return index;
}
private int handleR(String value, DoubleMetaphoneResult result, int index, boolean slavoGermanic) {
if (index == value.length() - 1 && !slavoGermanic && contains(value, index - 2, 2, "IE") && !contains(value, index - 4, 2, "ME", "MA")) {
result.appendAlternate('R');
} else {
result.append('R');
}
return (charAt(value, index + 1) == 'R') ? (index + 2) : (index + 1);
}
private int handleS(String value, DoubleMetaphoneResult result, int index, boolean slavoGermanic) {
if (contains(value, index - 1, 3, "ISL", "YSL")) {
index++;
} else if (index == 0 && contains(value, index, 5, "SUGAR")) {
result.append('X', 'S');
index++;
} else if (contains(value, index, 2, "SH")) {
if (contains(value, index + 1, 4, "HEIM", "HOEK", "HOLM", "HOLZ")) {
result.append('S');
} else {
result.append('X');
}
index += 2;
} else if (contains(value, index, 3, "SIO", "SIA") || contains(value, index, 4, "SIAN")) {
if (slavoGermanic) {
result.append('S');
} else {
result.append('S', 'X');
}
index += 3;
} else if ((index == 0 && contains(value, index + 1, 1, "M", "N", "L", "W")) || contains(value, index + 1, 1, "Z")) {
result.append('S', 'X');
index = contains(value, index + 1, 1, "Z") ? (index + 2) : (index + 1);
} else if (contains(value, index, 2, "SC")) {
index = handleSC(value, result, index);
} else {
if (index == value.length() - 1 && contains(value, index - 2, 2, "AI", "OI")) {
result.appendAlternate('S');
} else {
result.append('S');
}
index = contains(value, index + 1, 1, "S", "Z") ? (index + 2) : (index + 1);
}
return index;
}
private int handleSC(String value, DoubleMetaphoneResult result, int index) {
if (charAt(value, index + 2) == 'H') {
if (contains(value, index + 3, 2, "OO", "ER", "EN", "UY", "ED", "EM")) {
if (contains(value, index + 3, 2, "ER", "EN")) {
result.append("X", "SK");
} else {
result.append("SK");
}
} else if (index == 0 && !isVowel(charAt(value, 3)) && charAt(value, 3) != 'W') {
result.append('X', 'S');
} else {
result.append('X');
}
} else if (contains(value, index + 2, 1, "I", "E", "Y")) {
result.append('S');
} else {
result.append("SK");
}
return index + 3;
}
private int handleT(String value, DoubleMetaphoneResult result, int index) {
if (contains(value, index, 4, "TION")) {
result.append('X');
index += 3;
} else if (contains(value, index, 3, "TIA", "TCH")) {
result.append('X');
index += 3;
} else if (contains(value, index, 2, "TH") || contains(value, index, 3, "TTH")) {
if (contains(value, index + 2, 2, "OM", "AM") || contains(value, 0, 4, "VAN ", "VON ") || contains(value, 0, 3, "SCH")) {
result.append('T');
} else {
result.append('0', 'T');
}
index += 2;
} else {
result.append('T');
index = contains(value, index + 1, 1, "T", "D") ? (index + 2) : (index + 1);
}
return index;
}
private int handleW(String value, DoubleMetaphoneResult result, int index) {
if (contains(value, index, 2, "WR")) {
result.append('R');
index += 2;
} else if (index == 0 && (isVowel(charAt(value, index + 1)) || contains(value, index, 2, "WH"))) {
if (isVowel(charAt(value, index + 1))) {
result.append('A', 'F');
} else {
result.append('A');
}
index++;
} else if ((index == value.length() - 1 && isVowel(charAt(value, index - 1))) || contains(value, index - 1, 5, "EWSKI", "EWSKY", "OWSKI", "OWSKY") || contains(value, 0, 3, "SCH")) {
result.appendAlternate('F');
index++;
} else if (contains(value, index, 4, "WICZ", "WITZ")) {
result.append("TS", "FX");
index += 4;
} else {
index++;
}
return index;
}
private int handleX(String value, DoubleMetaphoneResult result, int index) {
if (index == 0) {
result.append('S');
index++;
} else {
if (index != value.length() - 1 || (!contains(value, index - 3, 3, "IAU", "EAU") && !contains(value, index - 2, 2, "AU", "OU")))
result.append("KS");
index = contains(value, index + 1, 1, "C", "X") ? (index + 2) : (index + 1);
}
return index;
}
private int handleZ(String value, DoubleMetaphoneResult result, int index, boolean slavoGermanic) {
if (charAt(value, index + 1) == 'H') {
result.append('J');
index += 2;
} else {
if (contains(value, index + 1, 2, "ZO", "ZI", "ZA") || (slavoGermanic && index > 0 && charAt(value, index - 1) != 'T')) {
result.append("S", "TS");
} else {
result.append('S');
}
index = (charAt(value, index + 1) == 'Z') ? (index + 2) : (index + 1);
}
return index;
}
private boolean conditionC0(String value, int index) {
if (contains(value, index, 4, "CHIA"))
return true;
if (index <= 1)
return false;
if (isVowel(charAt(value, index - 2)))
return false;
if (!contains(value, index - 1, 3, "ACH"))
return false;
char c = charAt(value, index + 2);
return ((c != 'I' && c != 'E') || contains(value, index - 2, 6, "BACHER", "MACHER"));
}
private boolean conditionCH0(String value, int index) {
if (index != 0)
return false;
if (!contains(value, index + 1, 5, "HARAC", "HARIS") && !contains(value, index + 1, 3, "HOR", "HYM", "HIA", "HEM"))
return false;
if (contains(value, 0, 5, "CHORE"))
return false;
return true;
}
private boolean conditionCH1(String value, int index) {
return (contains(value, 0, 4, "VAN ", "VON ") || contains(value, 0, 3, "SCH") || contains(value, index - 2, 6, "ORCHES", "ARCHIT", "ORCHID") || contains(value, index + 2, 1, "T", "S") || ((contains(value, index - 1, 1, "A", "O", "U", "E") || index == 0) && (contains(value, index + 2, 1, L_R_N_M_B_H_F_V_W_SPACE) || index + 1 == value.length() - 1)));
}
private boolean conditionL0(String value, int index) {
if (index == value.length() - 3 && contains(value, index - 1, 4, "ILLO", "ILLA", "ALLE"))
return true;
if ((contains(value, index - 1, 2, "AS", "OS") || contains(value, value.length() - 1, 1, "A", "O")) && contains(value, index - 1, 4, "ALLE"))
return true;
return false;
}
private boolean conditionM0(String value, int index) {
if (charAt(value, index + 1) == 'M')
return true;
return (contains(value, index - 1, 3, "UMB") && (index + 1 == value.length() - 1 || contains(value, index + 2, 2, "ER")));
}
private boolean isSlavoGermanic(String value) {
return (value.indexOf('W') > -1 || value.indexOf('K') > -1 || value.indexOf("CZ") > -1 || value.indexOf("WITZ") > -1);
}
private boolean isVowel(char ch) {
return ("AEIOUY".indexOf(ch) != -1);
}
private boolean isSilentStart(String value) {
boolean result = false;
for (int i = 0; i < SILENT_START.length; i++) {
if (value.startsWith(SILENT_START[i])) {
result = true;
break;
}
}
return result;
}
private String cleanInput(String input) {
if (input == null)
return null;
input = input.trim();
if (input.length() == 0)
return null;
return input.toUpperCase();
}
protected char charAt(String value, int index) {
if (index < 0 || index >= value.length())
return '\000';
return value.charAt(index);
}
private static boolean contains(String value, int start, int length, String criteria) {
return contains(value, start, length, new String[] { criteria });
}
private static boolean contains(String value, int start, int length, String criteria1, String criteria2) {
return contains(value, start, length, new String[] { criteria1, criteria2 });
}
private static boolean contains(String value, int start, int length, String criteria1, String criteria2, String criteria3) {
return contains(value, start, length, new String[] { criteria1, criteria2, criteria3 });
}
private static boolean contains(String value, int start, int length, String criteria1, String criteria2, String criteria3, String criteria4) {
return contains(value, start, length, new String[] { criteria1, criteria2, criteria3, criteria4 });
}
private static boolean contains(String value, int start, int length, String criteria1, String criteria2, String criteria3, String criteria4, String criteria5) {
return contains(value, start, length, new String[] { criteria1, criteria2, criteria3, criteria4, criteria5 });
}
private static boolean contains(String value, int start, int length, String criteria1, String criteria2, String criteria3, String criteria4, String criteria5, String criteria6) {
return contains(value, start, length, new String[] { criteria1, criteria2, criteria3, criteria4, criteria5, criteria6 });
}
protected static boolean contains(String value, int start, int length, String[] criteria) {
boolean result = false;
if (start >= 0 && start + length <= value.length()) {
String target = value.substring(start, start + length);
for (int i = 0; i < criteria.length; i++) {
if (target.equals(criteria[i])) {
result = true;
break;
}
}
}
return result;
}
public class DoubleMetaphoneResult {
private StringBuffer primary;
private StringBuffer alternate;
private int maxLength;
private final DoubleMetaphone this$0;
public DoubleMetaphoneResult(DoubleMetaphone this$0, int maxLength) {
this.this$0 = this$0;
this.primary = new StringBuffer(this.this$0.getMaxCodeLen());
this.alternate = new StringBuffer(this.this$0.getMaxCodeLen());
this.maxLength = maxLength;
}
public void append(char value) {
appendPrimary(value);
appendAlternate(value);
}
public void append(char primary, char alternate) {
appendPrimary(primary);
appendAlternate(alternate);
}
public void appendPrimary(char value) {
if (this.primary.length() < this.maxLength)
this.primary.append(value);
}
public void appendAlternate(char value) {
if (this.alternate.length() < this.maxLength)
this.alternate.append(value);
}
public void append(String value) {
appendPrimary(value);
appendAlternate(value);
}
public void append(String primary, String alternate) {
appendPrimary(primary);
appendAlternate(alternate);
}
public void appendPrimary(String value) {
int addChars = this.maxLength - this.primary.length();
if (value.length() <= addChars) {
this.primary.append(value);
} else {
this.primary.append(value.substring(0, addChars));
}
}
public void appendAlternate(String value) {
int addChars = this.maxLength - this.alternate.length();
if (value.length() <= addChars) {
this.alternate.append(value);
} else {
this.alternate.append(value.substring(0, addChars));
}
}
public String getPrimary() {
return this.primary.toString();
}
public String getAlternate() {
return this.alternate.toString();
}
public boolean isComplete() {
return (this.primary.length() >= this.maxLength && this.alternate.length() >= this.maxLength);
}
}
}

View file

@ -0,0 +1,261 @@
package org.apache.commons.codec.language;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringEncoder;
public class Metaphone implements StringEncoder {
private String vowels = "AEIOU";
private String frontv = "EIY";
private String varson = "CSPTG";
private int maxCodeLen = 4;
public String metaphone(String txt) {
boolean hard = false;
if (txt == null || txt.length() == 0)
return "";
if (txt.length() == 1)
return txt.toUpperCase();
char[] inwd = txt.toUpperCase().toCharArray();
StringBuffer local = new StringBuffer(40);
StringBuffer code = new StringBuffer(10);
switch (inwd[0]) {
case 'G':
case 'K':
case 'P':
if (inwd[1] == 'N') {
local.append(inwd, 1, inwd.length - 1);
} else {
local.append(inwd);
}
break;
case 'A':
if (inwd[1] == 'E') {
local.append(inwd, 1, inwd.length - 1);
} else {
local.append(inwd);
}
break;
case 'W':
if (inwd[1] == 'R') {
local.append(inwd, 1, inwd.length - 1);
} else if (inwd[1] == 'H') {
local.append(inwd, 1, inwd.length - 1);
local.setCharAt(0, 'W');
} else {
local.append(inwd);
}
break;
case 'X':
inwd[0] = 'S';
local.append(inwd);
break;
default:
local.append(inwd);
break;
}
int wdsz = local.length();
int n = 0;
while (code.length() < getMaxCodeLen() && n < wdsz) {
char symb = local.charAt(n);
if (symb != 'C' && isPreviousChar(local, n, symb)) {
n++;
} else {
switch (symb) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
if (n == 0)
code.append(symb);
break;
case 'B':
if (isPreviousChar(local, n, 'M') && isLastChar(wdsz, n))
break;
code.append(symb);
break;
case 'C':
if (isPreviousChar(local, n, 'S') && !isLastChar(wdsz, n) && this.frontv.indexOf(local.charAt(n + 1)) >= 0)
break;
if (regionMatch(local, n, "CIA")) {
code.append('X');
break;
}
if (!isLastChar(wdsz, n) && this.frontv.indexOf(local.charAt(n + 1)) >= 0) {
code.append('S');
break;
}
if (isPreviousChar(local, n, 'S') && isNextChar(local, n, 'H')) {
code.append('K');
break;
}
if (isNextChar(local, n, 'H')) {
if (n == 0 && wdsz >= 3 && isVowel(local, 2)) {
code.append('K');
} else {
code.append('X');
}
break;
}
code.append('K');
break;
case 'D':
if (!isLastChar(wdsz, n + 1) && isNextChar(local, n, 'G') && this.frontv.indexOf(local.charAt(n + 2)) >= 0) {
code.append('J');
n += 2;
} else {
code.append('T');
}
break;
case 'G':
if (isLastChar(wdsz, n + 1) && isNextChar(local, n, 'H'))
break;
if (!isLastChar(wdsz, n + 1) && isNextChar(local, n, 'H') && !isVowel(local, n + 2))
break;
if (n > 0 && (regionMatch(local, n, "GN") || regionMatch(local, n, "GNED")))
break;
if (isPreviousChar(local, n, 'G')) {
hard = true;
} else {
hard = false;
}
if (!isLastChar(wdsz, n) && this.frontv.indexOf(local.charAt(n + 1)) >= 0 && !hard) {
code.append('J');
} else {
code.append('K');
}
break;
case 'H':
if (isLastChar(wdsz, n))
break;
if (n > 0 && this.varson.indexOf(local.charAt(n - 1)) >= 0)
break;
if (isVowel(local, n + 1))
code.append('H');
break;
case 'F':
case 'J':
case 'L':
case 'M':
case 'N':
case 'R':
code.append(symb);
break;
case 'K':
if (n > 0) {
if (!isPreviousChar(local, n, 'C'))
code.append(symb);
break;
}
code.append(symb);
break;
case 'P':
if (isNextChar(local, n, 'H')) {
code.append('F');
} else {
code.append(symb);
}
break;
case 'Q':
code.append('K');
break;
case 'S':
if (regionMatch(local, n, "SH") || regionMatch(local, n, "SIO") || regionMatch(local, n, "SIA")) {
code.append('X');
} else {
code.append('S');
}
break;
case 'T':
if (regionMatch(local, n, "TIA") || regionMatch(local, n, "TIO")) {
code.append('X');
break;
}
if (regionMatch(local, n, "TCH"))
break;
if (regionMatch(local, n, "TH")) {
code.append('0');
} else {
code.append('T');
}
break;
case 'V':
code.append('F');
break;
case 'W':
case 'Y':
if (!isLastChar(wdsz, n) && isVowel(local, n + 1))
code.append(symb);
break;
case 'X':
code.append('K');
code.append('S');
break;
case 'Z':
code.append('S');
break;
}
n++;
}
if (code.length() > getMaxCodeLen())
code.setLength(getMaxCodeLen());
}
return code.toString();
}
private boolean isVowel(StringBuffer string, int index) {
return (this.vowels.indexOf(string.charAt(index)) >= 0);
}
private boolean isPreviousChar(StringBuffer string, int index, char c) {
boolean matches = false;
if (index > 0 && index < string.length())
matches = (string.charAt(index - 1) == c);
return matches;
}
private boolean isNextChar(StringBuffer string, int index, char c) {
boolean matches = false;
if (index >= 0 && index < string.length() - 1)
matches = (string.charAt(index + 1) == c);
return matches;
}
private boolean regionMatch(StringBuffer string, int index, String test) {
boolean matches = false;
if (index >= 0 && index + test.length() - 1 < string.length()) {
String substring = string.substring(index, index + test.length());
matches = substring.equals(test);
}
return matches;
}
private boolean isLastChar(int wdsz, int n) {
return (n + 1 == wdsz);
}
public Object encode(Object pObject) throws EncoderException {
if (!(pObject instanceof String))
throw new EncoderException("Parameter supplied to Metaphone encode is not of type java.lang.String");
return metaphone((String)pObject);
}
public String encode(String pString) {
return metaphone(pString);
}
public boolean isMetaphoneEqual(String str1, String str2) {
return metaphone(str1).equals(metaphone(str2));
}
public int getMaxCodeLen() {
return this.maxCodeLen;
}
public void setMaxCodeLen(int maxCodeLen) {
this.maxCodeLen = maxCodeLen;
}
}

View file

@ -0,0 +1,60 @@
package org.apache.commons.codec.language;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringEncoder;
public class RefinedSoundex implements StringEncoder {
public static final RefinedSoundex US_ENGLISH = new RefinedSoundex();
public static final char[] US_ENGLISH_MAPPING = "01360240043788015936020505".toCharArray();
private char[] soundexMapping;
public RefinedSoundex() {
this(US_ENGLISH_MAPPING);
}
public RefinedSoundex(char[] mapping) {
this.soundexMapping = mapping;
}
public int difference(String s1, String s2) throws EncoderException {
return SoundexUtils.difference(this, s1, s2);
}
public Object encode(Object pObject) throws EncoderException {
if (!(pObject instanceof String))
throw new EncoderException("Parameter supplied to RefinedSoundex encode is not of type java.lang.String");
return soundex((String)pObject);
}
public String encode(String pString) {
return soundex(pString);
}
char getMappingCode(char c) {
if (!Character.isLetter(c))
return '\000';
return this.soundexMapping[Character.toUpperCase(c) - 65];
}
public String soundex(String str) {
if (str == null)
return null;
str = SoundexUtils.clean(str);
if (str.length() == 0)
return str;
StringBuffer sBuf = new StringBuffer();
sBuf.append(str.charAt(0));
char last = '*';
for (int i = 0; i < str.length(); i++) {
char current = getMappingCode(str.charAt(i));
if (current != last) {
if (current != '\000')
sBuf.append(current);
last = current;
}
}
return sBuf.toString();
}
}

View file

@ -0,0 +1,96 @@
package org.apache.commons.codec.language;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringEncoder;
public class Soundex implements StringEncoder {
public static final Soundex US_ENGLISH = new Soundex();
public static final String US_ENGLISH_MAPPING_STRING = "01230120022455012623010202";
public static final char[] US_ENGLISH_MAPPING = "01230120022455012623010202".toCharArray();
public int difference(String s1, String s2) throws EncoderException {
return SoundexUtils.difference(this, s1, s2);
}
private int maxLength = 4;
private char[] soundexMapping;
public Soundex() {
this(US_ENGLISH_MAPPING);
}
public Soundex(char[] mapping) {
setSoundexMapping(mapping);
}
public Object encode(Object pObject) throws EncoderException {
if (!(pObject instanceof String))
throw new EncoderException("Parameter supplied to Soundex encode is not of type java.lang.String");
return soundex((String)pObject);
}
public String encode(String pString) {
return soundex(pString);
}
private char getMappingCode(String str, int index) {
char mappedChar = map(str.charAt(index));
if (index > 1 && mappedChar != '0') {
char hwChar = str.charAt(index - 1);
if ('H' == hwChar || 'W' == hwChar) {
char preHWChar = str.charAt(index - 2);
char firstCode = map(preHWChar);
if (firstCode == mappedChar || 'H' == preHWChar || 'W' == preHWChar)
return '\000';
}
}
return mappedChar;
}
public int getMaxLength() {
return this.maxLength;
}
private char[] getSoundexMapping() {
return this.soundexMapping;
}
private char map(char ch) {
int index = ch - 65;
if (index < 0 || index >= (getSoundexMapping()).length)
throw new IllegalArgumentException("The character is not mapped: " + ch);
return getSoundexMapping()[index];
}
public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
private void setSoundexMapping(char[] soundexMapping) {
this.soundexMapping = soundexMapping;
}
public String soundex(String str) {
if (str == null)
return null;
str = SoundexUtils.clean(str);
if (str.length() == 0)
return str;
char[] out = { '0', '0', '0', '0' };
int incount = 1, count = 1;
out[0] = str.charAt(0);
char last = getMappingCode(str, 0);
while (incount < str.length() && count < out.length) {
char mapped = getMappingCode(str, incount++);
if (mapped != '\000') {
if (mapped != '0' && mapped != last)
out[count++] = mapped;
last = mapped;
}
}
return new String(out);
}
}

View file

@ -0,0 +1,37 @@
package org.apache.commons.codec.language;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringEncoder;
final class SoundexUtils {
static String clean(String str) {
if (str == null || str.length() == 0)
return str;
int len = str.length();
char[] chars = new char[len];
int count = 0;
for (int i = 0; i < len; i++) {
if (Character.isLetter(str.charAt(i)))
chars[count++] = str.charAt(i);
}
if (count == len)
return str.toUpperCase();
return new String(chars, 0, count).toUpperCase();
}
static int difference(StringEncoder encoder, String s1, String s2) throws EncoderException {
return differenceEncoded(encoder.encode(s1), encoder.encode(s2));
}
static int differenceEncoded(String es1, String es2) {
if (es1 == null || es2 == null)
return 0;
int lengthToMatch = Math.min(es1.length(), es2.length());
int diff = 0;
for (int i = 0; i < lengthToMatch; i++) {
if (es1.charAt(i) == es2.charAt(i))
diff++;
}
return diff;
}
}

View file

@ -0,0 +1,20 @@
<!--
Copyright 2003-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.
-->
<html>
<body>
Language and phonetic encoders.
</body>
</html>

View file

@ -0,0 +1,80 @@
package org.apache.commons.codec.net;
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringDecoder;
import org.apache.commons.codec.StringEncoder;
import org.apache.commons.codec.binary.Base64;
public class BCodec extends RFC1522Codec implements StringEncoder, StringDecoder {
private String charset = "UTF-8";
public BCodec() {}
public BCodec(String charset) {
this.charset = charset;
}
protected String getEncoding() {
return "B";
}
protected byte[] doEncoding(byte[] bytes) throws EncoderException {
if (bytes == null)
return null;
return Base64.encodeBase64(bytes);
}
protected byte[] doDecoding(byte[] bytes) throws DecoderException {
if (bytes == null)
return null;
return Base64.decodeBase64(bytes);
}
public String encode(String value, String charset) throws EncoderException {
if (value == null)
return null;
try {
return encodeText(value, charset);
} catch (UnsupportedEncodingException e) {
throw new EncoderException(e.getMessage());
}
}
public String encode(String value) throws EncoderException {
if (value == null)
return null;
return encode(value, getDefaultCharset());
}
public String decode(String value) throws DecoderException {
if (value == null)
return null;
try {
return decodeText(value);
} catch (UnsupportedEncodingException e) {
throw new DecoderException(e.getMessage());
}
}
public Object encode(Object value) throws EncoderException {
if (value == null)
return null;
if (value instanceof String)
return encode((String)value);
throw new EncoderException("Objects of type " + value.getClass().getName() + " cannot be encoded using BCodec");
}
public Object decode(Object value) throws DecoderException {
if (value == null)
return null;
if (value instanceof String)
return decode((String)value);
throw new DecoderException("Objects of type " + value.getClass().getName() + " cannot be decoded using BCodec");
}
public String getDefaultCharset() {
return this.charset;
}
}

View file

@ -0,0 +1,160 @@
package org.apache.commons.codec.net;
import java.io.UnsupportedEncodingException;
import java.util.BitSet;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringDecoder;
import org.apache.commons.codec.StringEncoder;
public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder {
private String charset = "UTF-8";
private static final BitSet PRINTABLE_CHARS = new BitSet(256);
static {
PRINTABLE_CHARS.set(32);
PRINTABLE_CHARS.set(33);
PRINTABLE_CHARS.set(34);
PRINTABLE_CHARS.set(35);
PRINTABLE_CHARS.set(36);
PRINTABLE_CHARS.set(37);
PRINTABLE_CHARS.set(38);
PRINTABLE_CHARS.set(39);
PRINTABLE_CHARS.set(40);
PRINTABLE_CHARS.set(41);
PRINTABLE_CHARS.set(42);
PRINTABLE_CHARS.set(43);
PRINTABLE_CHARS.set(44);
PRINTABLE_CHARS.set(45);
PRINTABLE_CHARS.set(46);
PRINTABLE_CHARS.set(47);
for (int k = 48; k <= 57; k++)
PRINTABLE_CHARS.set(k);
PRINTABLE_CHARS.set(58);
PRINTABLE_CHARS.set(59);
PRINTABLE_CHARS.set(60);
PRINTABLE_CHARS.set(62);
PRINTABLE_CHARS.set(64);
for (int j = 65; j <= 90; j++)
PRINTABLE_CHARS.set(j);
PRINTABLE_CHARS.set(91);
PRINTABLE_CHARS.set(92);
PRINTABLE_CHARS.set(93);
PRINTABLE_CHARS.set(94);
PRINTABLE_CHARS.set(96);
for (int i = 97; i <= 122; i++)
PRINTABLE_CHARS.set(i);
PRINTABLE_CHARS.set(123);
PRINTABLE_CHARS.set(124);
PRINTABLE_CHARS.set(125);
PRINTABLE_CHARS.set(126);
}
private static byte BLANK = 32;
private static byte UNDERSCORE = 95;
private boolean encodeBlanks = false;
public QCodec(String charset) {
this.charset = charset;
}
protected String getEncoding() {
return "Q";
}
protected byte[] doEncoding(byte[] bytes) throws EncoderException {
if (bytes == null)
return null;
byte[] data = QuotedPrintableCodec.encodeQuotedPrintable(PRINTABLE_CHARS, bytes);
if (this.encodeBlanks)
for (int i = 0; i < data.length; i++) {
if (data[i] == BLANK)
data[i] = UNDERSCORE;
}
return data;
}
protected byte[] doDecoding(byte[] bytes) throws DecoderException {
if (bytes == null)
return null;
boolean hasUnderscores = false;
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] == UNDERSCORE) {
hasUnderscores = true;
break;
}
}
if (hasUnderscores) {
byte[] tmp = new byte[bytes.length];
for (int j = 0; j < bytes.length; j++) {
byte b = bytes[j];
if (b != UNDERSCORE) {
tmp[j] = b;
} else {
tmp[j] = BLANK;
}
}
return QuotedPrintableCodec.decodeQuotedPrintable(tmp);
}
return QuotedPrintableCodec.decodeQuotedPrintable(bytes);
}
public String encode(String pString, String charset) throws EncoderException {
if (pString == null)
return null;
try {
return encodeText(pString, charset);
} catch (UnsupportedEncodingException e) {
throw new EncoderException(e.getMessage());
}
}
public String encode(String pString) throws EncoderException {
if (pString == null)
return null;
return encode(pString, getDefaultCharset());
}
public String decode(String pString) throws DecoderException {
if (pString == null)
return null;
try {
return decodeText(pString);
} catch (UnsupportedEncodingException e) {
throw new DecoderException(e.getMessage());
}
}
public Object encode(Object pObject) throws EncoderException {
if (pObject == null)
return null;
if (pObject instanceof String)
return encode((String)pObject);
throw new EncoderException("Objects of type " + pObject.getClass().getName() + " cannot be encoded using Q codec");
}
public Object decode(Object pObject) throws DecoderException {
if (pObject == null)
return null;
if (pObject instanceof String)
return decode((String)pObject);
throw new DecoderException("Objects of type " + pObject.getClass().getName() + " cannot be decoded using Q codec");
}
public String getDefaultCharset() {
return this.charset;
}
public boolean isEncodeBlanks() {
return this.encodeBlanks;
}
public void setEncodeBlanks(boolean b) {
this.encodeBlanks = b;
}
public QCodec() {}
}

View file

@ -0,0 +1,152 @@
package org.apache.commons.codec.net;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.BitSet;
import org.apache.commons.codec.BinaryDecoder;
import org.apache.commons.codec.BinaryEncoder;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringDecoder;
import org.apache.commons.codec.StringEncoder;
public class QuotedPrintableCodec implements BinaryEncoder, BinaryDecoder, StringEncoder, StringDecoder {
private String charset = "UTF-8";
private static final BitSet PRINTABLE_CHARS = new BitSet(256);
private static byte ESCAPE_CHAR = 61;
private static byte TAB = 9;
private static byte SPACE = 32;
static {
for (int j = 33; j <= 60; j++)
PRINTABLE_CHARS.set(j);
for (int i = 62; i <= 126; i++)
PRINTABLE_CHARS.set(i);
PRINTABLE_CHARS.set(TAB);
PRINTABLE_CHARS.set(SPACE);
}
public QuotedPrintableCodec(String charset) {
this.charset = charset;
}
private static final void encodeQuotedPrintable(int b, ByteArrayOutputStream buffer) {
buffer.write(ESCAPE_CHAR);
char hex1 = Character.toUpperCase(Character.forDigit(b >> 4 & 0xF, 16));
char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
buffer.write(hex1);
buffer.write(hex2);
}
public static final byte[] encodeQuotedPrintable(BitSet printable, byte[] bytes) {
if (bytes == null)
return null;
if (printable == null)
printable = PRINTABLE_CHARS;
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i];
if (b < 0)
b = 256 + b;
if (printable.get(b)) {
buffer.write(b);
} else {
encodeQuotedPrintable(b, buffer);
}
}
return buffer.toByteArray();
}
public static final byte[] decodeQuotedPrintable(byte[] bytes) throws DecoderException {
if (bytes == null)
return null;
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i];
if (b == ESCAPE_CHAR) {
try {
int u = Character.digit((char)bytes[++i], 16);
int l = Character.digit((char)bytes[++i], 16);
if (u == -1 || l == -1)
throw new DecoderException("Invalid quoted-printable encoding");
buffer.write((char)((u << 4) + l));
} catch (ArrayIndexOutOfBoundsException e) {
throw new DecoderException("Invalid quoted-printable encoding");
}
} else {
buffer.write(b);
}
}
return buffer.toByteArray();
}
public byte[] encode(byte[] bytes) {
return encodeQuotedPrintable(PRINTABLE_CHARS, bytes);
}
public byte[] decode(byte[] bytes) throws DecoderException {
return decodeQuotedPrintable(bytes);
}
public String encode(String pString) throws EncoderException {
if (pString == null)
return null;
try {
return encode(pString, getDefaultCharset());
} catch (UnsupportedEncodingException e) {
throw new EncoderException(e.getMessage());
}
}
public String decode(String pString, String charset) throws DecoderException, UnsupportedEncodingException {
if (pString == null)
return null;
return new String(decode(pString.getBytes("US-ASCII")), charset);
}
public String decode(String pString) throws DecoderException {
if (pString == null)
return null;
try {
return decode(pString, getDefaultCharset());
} catch (UnsupportedEncodingException e) {
throw new DecoderException(e.getMessage());
}
}
public Object encode(Object pObject) throws EncoderException {
if (pObject == null)
return null;
if (pObject instanceof byte[])
return encode((byte[])pObject);
if (pObject instanceof String)
return encode((String)pObject);
throw new EncoderException("Objects of type " + pObject.getClass().getName() + " cannot be quoted-printable encoded");
}
public Object decode(Object pObject) throws DecoderException {
if (pObject == null)
return null;
if (pObject instanceof byte[])
return decode((byte[])pObject);
if (pObject instanceof String)
return decode((String)pObject);
throw new DecoderException("Objects of type " + pObject.getClass().getName() + " cannot be quoted-printable decoded");
}
public String getDefaultCharset() {
return this.charset;
}
public String encode(String pString, String charset) throws UnsupportedEncodingException {
if (pString == null)
return null;
return new String(encode(pString.getBytes(charset)), "US-ASCII");
}
public QuotedPrintableCodec() {}
}

View file

@ -0,0 +1,55 @@
package org.apache.commons.codec.net;
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
abstract class RFC1522Codec {
protected String encodeText(String text, String charset) throws EncoderException, UnsupportedEncodingException {
if (text == null)
return null;
StringBuffer buffer = new StringBuffer();
buffer.append("=?");
buffer.append(charset);
buffer.append('?');
buffer.append(getEncoding());
buffer.append('?');
byte[] rawdata = doEncoding(text.getBytes(charset));
buffer.append(new String(rawdata, "US-ASCII"));
buffer.append("?=");
return buffer.toString();
}
protected String decodeText(String text) throws DecoderException, UnsupportedEncodingException {
if (text == null)
return null;
if (!text.startsWith("=?") || !text.endsWith("?="))
throw new DecoderException("RFC 1522 violation: malformed encoded content");
int termnator = text.length() - 2;
int from = 2;
int to = text.indexOf("?", from);
if (to == -1 || to == termnator)
throw new DecoderException("RFC 1522 violation: charset token not found");
String charset = text.substring(from, to);
if (charset.equals(""))
throw new DecoderException("RFC 1522 violation: charset not specified");
from = to + 1;
to = text.indexOf("?", from);
if (to == -1 || to == termnator)
throw new DecoderException("RFC 1522 violation: encoding token not found");
String encoding = text.substring(from, to);
if (!getEncoding().equalsIgnoreCase(encoding))
throw new DecoderException("This codec cannot decode " + encoding + " encoded content");
from = to + 1;
to = text.indexOf("?", from);
byte[] data = text.substring(from, to).getBytes("US-ASCII");
data = doDecoding(data);
return new String(data, charset);
}
protected abstract String getEncoding();
protected abstract byte[] doEncoding(byte[] paramArrayOfbyte) throws EncoderException;
protected abstract byte[] doDecoding(byte[] paramArrayOfbyte) throws DecoderException;
}

View file

@ -0,0 +1,7 @@
package org.apache.commons.codec.net;
interface StringEncodings {
public static final String US_ASCII = "US-ASCII";
public static final String UTF8 = "UTF-8";
}

View file

@ -0,0 +1,157 @@
package org.apache.commons.codec.net;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.BitSet;
import org.apache.commons.codec.BinaryDecoder;
import org.apache.commons.codec.BinaryEncoder;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringDecoder;
import org.apache.commons.codec.StringEncoder;
public class URLCodec implements BinaryEncoder, BinaryDecoder, StringEncoder, StringDecoder {
protected String charset = "UTF-8";
protected static byte ESCAPE_CHAR = 37;
protected static final BitSet WWW_FORM_URL = new BitSet(256);
static {
for (int k = 97; k <= 122; k++)
WWW_FORM_URL.set(k);
for (int j = 65; j <= 90; j++)
WWW_FORM_URL.set(j);
for (int i = 48; i <= 57; i++)
WWW_FORM_URL.set(i);
WWW_FORM_URL.set(45);
WWW_FORM_URL.set(95);
WWW_FORM_URL.set(46);
WWW_FORM_URL.set(42);
WWW_FORM_URL.set(32);
}
public URLCodec(String charset) {
this.charset = charset;
}
public static final byte[] encodeUrl(BitSet urlsafe, byte[] bytes) {
if (bytes == null)
return null;
if (urlsafe == null)
urlsafe = WWW_FORM_URL;
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i];
if (b < 0)
b = 256 + b;
if (urlsafe.get(b)) {
if (b == 32)
b = 43;
buffer.write(b);
} else {
buffer.write(37);
char hex1 = Character.toUpperCase(Character.forDigit(b >> 4 & 0xF, 16));
char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
buffer.write(hex1);
buffer.write(hex2);
}
}
return buffer.toByteArray();
}
public static final byte[] decodeUrl(byte[] bytes) throws DecoderException {
if (bytes == null)
return null;
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i];
if (b == 43) {
buffer.write(32);
} else if (b == 37) {
try {
int u = Character.digit((char)bytes[++i], 16);
int l = Character.digit((char)bytes[++i], 16);
if (u == -1 || l == -1)
throw new DecoderException("Invalid URL encoding");
buffer.write((char)((u << 4) + l));
} catch (ArrayIndexOutOfBoundsException e) {
throw new DecoderException("Invalid URL encoding");
}
} else {
buffer.write(b);
}
}
return buffer.toByteArray();
}
public byte[] encode(byte[] bytes) {
return encodeUrl(WWW_FORM_URL, bytes);
}
public byte[] decode(byte[] bytes) throws DecoderException {
return decodeUrl(bytes);
}
public String encode(String pString, String charset) throws UnsupportedEncodingException {
if (pString == null)
return null;
return new String(encode(pString.getBytes(charset)), "US-ASCII");
}
public String encode(String pString) throws EncoderException {
if (pString == null)
return null;
try {
return encode(pString, getDefaultCharset());
} catch (UnsupportedEncodingException e) {
throw new EncoderException(e.getMessage());
}
}
public String decode(String pString, String charset) throws DecoderException, UnsupportedEncodingException {
if (pString == null)
return null;
return new String(decode(pString.getBytes("US-ASCII")), charset);
}
public String decode(String pString) throws DecoderException {
if (pString == null)
return null;
try {
return decode(pString, getDefaultCharset());
} catch (UnsupportedEncodingException e) {
throw new DecoderException(e.getMessage());
}
}
public Object encode(Object pObject) throws EncoderException {
if (pObject == null)
return null;
if (pObject instanceof byte[])
return encode((byte[])pObject);
if (pObject instanceof String)
return encode((String)pObject);
throw new EncoderException("Objects of type " + pObject.getClass().getName() + " cannot be URL encoded");
}
public Object decode(Object pObject) throws DecoderException {
if (pObject == null)
return null;
if (pObject instanceof byte[])
return decode((byte[])pObject);
if (pObject instanceof String)
return decode((String)pObject);
throw new DecoderException("Objects of type " + pObject.getClass().getName() + " cannot be URL decoded");
}
public String getEncoding() {
return this.charset;
}
public String getDefaultCharset() {
return this.charset;
}
public URLCodec() {}
}

View file

@ -0,0 +1,22 @@
<!--
Copyright 2003-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.
-->
<html>
<body>
<p>
Network related encoding and decoding.
</p>
</body>
</html>

View file

@ -0,0 +1,28 @@
<!--
Copyright 2003-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.
-->
<!-- $Id: overview.html,v 1.6 2004/05/17 17:06:10 ggregory Exp $ -->
<html>
<body>
<p>
This document is the API specification for the Apache Jakarta Commons Codec Library, version 1.3.
</p>
<p>
This library requires a JRE version of 1.2.2 or greater.
The hypertext links originating from this document point to Sun's version 1.3 API as the 1.2.2 API documentation
is no longer on-line.
</p>
</body>
</html>

View file

@ -0,0 +1,99 @@
<!--
Copyright 2003-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.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<p>A small set of interfaces used by
the various implementations in the sub-packages.</p>
<p>Definitive implementations of commonly used encoders and decoders.</p>
<p>Codec is currently comprised of a modest set of utilities and a
simple framework for String encoding and decoding in three categories:
Binary Encoders, Language Encoders, and Network Encoders. </p>
<h4><a name="Common Encoders">Binary Encoders</a></h4>
<table border="1" width="100%" cellspacing="2" cellpadding="3">
<tbody>
<tr>
<td>
<a href="binary/Base64.html">
org.apache.commons.codec.binary.Base64</a>
</td>
<td>
Provides Base64 content-transfer-encoding as defined in
<a href="http://www.ietf.org/rfc/rfc2045.txt"> RFC 2045</a>
</td>
<td>Production</td>
</tr>
<tr>
<td>
<a href="binary/Hex.html">
org.apache.commons.codec.binary.Hex</a>
</td>
<td>
Converts an array of bytes into an array of characters
representing the hexidecimal values of each byte in order
</td>
<td>Production</td>
</tr>
</tbody>
</table>
<h4>
<a name="Language Encoders">Language Encoders</a>
</h4>
<p>
Codec contains a number of commonly used language and phonetic
encoders
</p>
<table border="1" width="100%" cellspacing="2" cellpadding="3">
<tbody>
<tr>
<td>
<a href="#">org.apache.commons.codec.language.Soundex</a>
</td>
<td>Implementation of the Soundex algorithm.</td>
<td>Production</td>
</tr>
<tr>
<td>
<a href="#">org.apache.commons.codec.language.Metaphone</a>
</td>
<td>Implementation of the Metaphone algorithm.</td>
<td>Production</td>
</tr>
</tbody>
</table>
<h4><a name="Network_Encoders">Network Encoders</a></h4>
<h4> </h4>
<p> Codec contains network related encoders </p>
<table border="1" width="100%" cellspacing="2" cellpadding="3">
<tbody>
<tr>
<td>
<a href="#">org.apache.commons.codec.net.URLCodec</a>
</td>
<td>Implements the 'www-form-urlencoded' encoding scheme.</td>
<td>Production</td>
</tr>
</tbody>
</table>
<br>
</body>
</html>