ohair@286: /* mkos@384: * Copyright (c) 2004, 2013, 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: ohair@286: import java.io.EOFException; ohair@286: import java.io.IOException; ohair@286: import java.io.InputStream; ohair@286: import java.io.OutputStream; ohair@286: import java.nio.CharBuffer; ohair@286: import java.util.ArrayList; ohair@286: import java.util.List; ohair@286: ohair@286: import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException; ohair@286: import com.sun.xml.internal.fastinfoset.CommonResourceBundle; ohair@286: ohair@286: ohair@286: /** ohair@286: * ohair@286: * An encoder for handling boolean values. Suppports the builtin BOOLEAN encoder. ohair@286: * ohair@286: * @author Alan Hudson ohair@286: * @author Paul Sandoz ohair@286: * ohair@286: */ ohair@286: public class BooleanEncodingAlgorithm extends BuiltInEncodingAlgorithm { ohair@286: ohair@286: /** Table for setting a particular bit of a byte */ ohair@286: private static final int[] BIT_TABLE = { ohair@286: 1 << 7, ohair@286: 1 << 6, ohair@286: 1 << 5, ohair@286: 1 << 4, ohair@286: 1 << 3, ohair@286: 1 << 2, ohair@286: 1 << 1, ohair@286: 1 << 0}; ohair@286: ohair@286: public int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException { ohair@286: // Cannot determine the number of boolean values from just the octet length ohair@286: throw new UnsupportedOperationException(); ohair@286: } ohair@286: ohair@286: public int getOctetLengthFromPrimitiveLength(int primitiveLength) { ohair@286: if (primitiveLength < 5) { ohair@286: return 1; ohair@286: } else { ohair@286: final int div = primitiveLength / 8; ohair@286: return (div == 0) ? 2 : 1 + div; ohair@286: } ohair@286: } ohair@286: ohair@286: public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException { ohair@286: final int blength = getPrimtiveLengthFromOctetLength(length, b[start]); ohair@286: boolean[] data = new boolean[blength]; ohair@286: ohair@286: decodeFromBytesToBooleanArray(data, 0, blength, b, start, length); ohair@286: return data; ohair@286: } ohair@286: ohair@286: public final Object decodeFromInputStream(InputStream s) throws IOException { ohair@286: final List booleanList = new ArrayList(); ohair@286: ohair@286: int value = s.read(); ohair@286: if (value == -1) { ohair@286: throw new EOFException(); ohair@286: } ohair@286: final int unusedBits = (value >> 4) & 0xFF; ohair@286: ohair@286: int bitPosition = 4; ohair@286: int bitPositionEnd = 8; ohair@286: int valueNext = 0; ohair@286: do { ohair@286: valueNext = s.read(); ohair@286: if (valueNext == -1) { ohair@286: bitPositionEnd -= unusedBits; ohair@286: } ohair@286: ohair@286: while(bitPosition < bitPositionEnd) { ohair@286: booleanList.add( ohair@286: Boolean.valueOf((value & BIT_TABLE[bitPosition++]) > 0)); ohair@286: } ohair@286: ohair@286: value = valueNext; ohair@286: } while (value != -1); ohair@286: ohair@286: return generateArrayFromList(booleanList); ohair@286: } ohair@286: ohair@286: public void encodeToOutputStream(Object data, OutputStream s) throws IOException { ohair@286: if (!(data instanceof boolean[])) { ohair@286: throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotBoolean")); ohair@286: } ohair@286: ohair@286: boolean array[] = (boolean[])data; ohair@286: final int alength = array.length; ohair@286: ohair@286: final int mod = (alength + 4) % 8; ohair@286: final int unusedBits = (mod == 0) ? 0 : 8 - mod; ohair@286: ohair@286: int bitPosition = 4; ohair@286: int value = unusedBits << 4; ohair@286: int astart = 0; ohair@286: while (astart < alength) { ohair@286: if (array[astart++]) { ohair@286: value |= BIT_TABLE[bitPosition]; ohair@286: } ohair@286: ohair@286: if (++bitPosition == 8) { ohair@286: s.write(value); ohair@286: bitPosition = value = 0; ohair@286: } ohair@286: } ohair@286: ohair@286: if (bitPosition != 8) { ohair@286: s.write(value); ohair@286: } 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 boolean[0]; ohair@286: } ohair@286: ohair@286: final CharBuffer cb = CharBuffer.wrap(ch, start, length); ohair@286: final List booleanList = new ArrayList(); ohair@286: ohair@286: matchWhiteSpaceDelimnatedWords(cb, ohair@286: new WordListener() { ohair@286: public void word(int start, int end) { ohair@286: if (cb.charAt(start) == 't') { ohair@286: booleanList.add(Boolean.TRUE); ohair@286: } else { ohair@286: booleanList.add(Boolean.FALSE); ohair@286: } ohair@286: } ohair@286: } ohair@286: ); ohair@286: ohair@286: return generateArrayFromList(booleanList); 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: ohair@286: final boolean[] value = (boolean[]) data; ohair@286: if (value.length == 0) { ohair@286: return; ohair@286: } ohair@286: ohair@286: // Insure conservately as all false ohair@286: s.ensureCapacity(value.length * 5); ohair@286: ohair@286: final int end = value.length - 1; ohair@286: for (int i = 0; i <= end; i++) { ohair@286: if (value[i]) { ohair@286: s.append("true"); ohair@286: } else { ohair@286: s.append("false"); ohair@286: } ohair@286: if (i != end) { ohair@286: s.append(' '); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: public int getPrimtiveLengthFromOctetLength(int octetLength, int firstOctet) throws EncodingAlgorithmException { ohair@286: final int unusedBits = (firstOctet >> 4) & 0xFF; ohair@286: if (octetLength == 1) { ohair@286: if (unusedBits > 3) { ohair@286: throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.unusedBits4")); ohair@286: } ohair@286: return 4 - unusedBits; ohair@286: } else { ohair@286: if (unusedBits > 7) { ohair@286: throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.unusedBits8")); ohair@286: } ohair@286: return octetLength * 8 - 4 - unusedBits; ohair@286: } ohair@286: } ohair@286: ohair@286: public final void decodeFromBytesToBooleanArray(boolean[] bdata, int bstart, int blength, byte[] b, int start, int length) { ohair@286: int value = b[start++] & 0xFF; ohair@286: int bitPosition = 4; ohair@286: final int bend = bstart + blength; ohair@286: while (bstart < bend) { ohair@286: if (bitPosition == 8) { ohair@286: value = b[start++] & 0xFF; ohair@286: bitPosition = 0; ohair@286: } ohair@286: ohair@286: bdata[bstart++] = (value & BIT_TABLE[bitPosition++]) > 0; ohair@286: } ohair@286: } ohair@286: ohair@286: public void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) { ohair@286: if (!(array instanceof boolean[])) { ohair@286: throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotBoolean")); ohair@286: } ohair@286: ohair@286: encodeToBytesFromBooleanArray((boolean[])array, astart, alength, b, start); ohair@286: } ohair@286: ohair@286: public void encodeToBytesFromBooleanArray(boolean[] array, int astart, int alength, byte[] b, int start) { ohair@286: final int mod = (alength + 4) % 8; ohair@286: final int unusedBits = (mod == 0) ? 0 : 8 - mod; ohair@286: ohair@286: int bitPosition = 4; ohair@286: int value = unusedBits << 4; ohair@286: final int aend = astart + alength; ohair@286: while (astart < aend) { ohair@286: if (array[astart++]) { ohair@286: value |= BIT_TABLE[bitPosition]; ohair@286: } ohair@286: ohair@286: if (++bitPosition == 8) { ohair@286: b[start++] = (byte)value; ohair@286: bitPosition = value = 0; ohair@286: } ohair@286: } ohair@286: ohair@286: if (bitPosition > 0) { ohair@286: b[start] = (byte)value; ohair@286: } ohair@286: } ohair@286: ohair@286: ohair@286: /** ohair@286: * ohair@286: * Generate a boolean array from a list of Booleans. ohair@286: * ohair@286: * @param array The array ohair@286: * ohair@286: */ mkos@384: private boolean[] generateArrayFromList(List array) { ohair@286: boolean[] bdata = new boolean[array.size()]; ohair@286: for (int i = 0; i < bdata.length; i++) { ohair@286: bdata[i] = ((Boolean)array.get(i)).booleanValue(); ohair@286: } ohair@286: ohair@286: return bdata; ohair@286: } ohair@286: ohair@286: }