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