325N/A/*
325N/A * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A *
325N/A * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.fastinfoset.algorithm;
325N/A
325N/Aimport java.io.IOException;
325N/Aimport java.io.InputStream;
325N/Aimport java.io.OutputStream;
325N/Aimport com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException;
325N/Aimport com.sun.xml.internal.fastinfoset.CommonResourceBundle;
325N/A
325N/Apublic class HexadecimalEncodingAlgorithm extends BuiltInEncodingAlgorithm {
325N/A private static final char NIBBLE_TO_HEXADECIMAL_TABLE[] =
325N/A { '0','1','2','3','4','5','6','7',
325N/A '8','9','A','B','C','D','E','F' };
325N/A
325N/A private static final int HEXADECIMAL_TO_NIBBLE_TABLE[] = {
325N/A /*'0'*/ 0,
325N/A /*'1'*/ 1,
325N/A /*'2'*/ 2,
325N/A /*'3'*/ 3,
325N/A /*'4'*/ 4,
325N/A /*'5'*/ 5,
325N/A /*'6'*/ 6,
325N/A /*'7'*/ 7,
325N/A /*'8'*/ 8,
325N/A /*'9'*/ 9, -1, -1, -1, -1, -1, -1, -1,
325N/A /*'A'*/ 10,
325N/A /*'B'*/ 11,
325N/A /*'C'*/ 12,
325N/A /*'D'*/ 13,
325N/A /*'E'*/ 14,
325N/A /*'F'*/ 15,
325N/A /*'G'-'Z'*/-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
325N/A /*'[' - '`'*/ -1, -1, -1, -1, -1, -1,
325N/A /*'a'*/ 10,
325N/A /*'b'*/ 11,
325N/A /*'c'*/ 12,
325N/A /*'d'*/ 13,
325N/A /*'e'*/ 14,
325N/A /*'f'*/ 15 };
325N/A
325N/A public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException {
325N/A final byte[] data = new byte[length];
325N/A System.arraycopy(b, start, data, 0, length);
325N/A return data;
325N/A }
325N/A
325N/A public final Object decodeFromInputStream(InputStream s) throws IOException {
325N/A throw new UnsupportedOperationException(CommonResourceBundle.getInstance().getString("message.notImplemented"));
325N/A }
325N/A
325N/A
325N/A public void encodeToOutputStream(Object data, OutputStream s) throws IOException {
325N/A if (!(data instanceof byte[])) {
325N/A throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotByteArray"));
325N/A }
325N/A
325N/A s.write((byte[])data);
325N/A }
325N/A
325N/A public final Object convertFromCharacters(char[] ch, int start, int length) {
325N/A if (length == 0) {
325N/A return new byte[0];
325N/A }
325N/A
325N/A StringBuffer encodedValue = removeWhitespace(ch, start, length);
325N/A int encodedLength = encodedValue.length();
325N/A if (encodedLength == 0) {
325N/A return new byte[0];
325N/A }
325N/A
325N/A int valueLength = encodedValue.length() / 2;
325N/A byte[] value = new byte[valueLength];
325N/A
325N/A int encodedIdx = 0;
325N/A for (int i = 0; i < valueLength; ++i) {
325N/A int nibble1 = HEXADECIMAL_TO_NIBBLE_TABLE[encodedValue.charAt(encodedIdx++) - '0'];
325N/A int nibble2 = HEXADECIMAL_TO_NIBBLE_TABLE[encodedValue.charAt(encodedIdx++) - '0'];
325N/A value[i] = (byte) ((nibble1 << 4) | nibble2);
325N/A }
325N/A
325N/A return value;
325N/A }
325N/A
325N/A public final void convertToCharacters(Object data, StringBuffer s) {
325N/A if (data == null) {
325N/A return;
325N/A }
325N/A final byte[] value = (byte[]) data;
325N/A if (value.length == 0) {
325N/A return;
325N/A }
325N/A
325N/A s.ensureCapacity(value.length * 2);
325N/A for (int i = 0; i < value.length; ++i) {
325N/A s.append(NIBBLE_TO_HEXADECIMAL_TABLE[(value[i] >>> 4) & 0xf]);
325N/A s.append(NIBBLE_TO_HEXADECIMAL_TABLE[value[i] & 0xf]);
325N/A }
325N/A }
325N/A
325N/A
325N/A
325N/A public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
325N/A return octetLength * 2;
325N/A }
325N/A
325N/A public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
325N/A return primitiveLength / 2;
325N/A }
325N/A
325N/A public final void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) {
325N/A System.arraycopy((byte[])array, astart, b, start, alength);
325N/A }
325N/A}