001: /*
002: * Fast Infoset ver. 0.1 software ("Software")
003: *
004: * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Software is licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License. You may
008: * obtain a copy of the License at:
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations.
016: *
017: * Sun supports and benefits from the global community of open source
018: * developers, and thanks the community for its important contributions and
019: * open standards-based technology, which Sun has adopted into many of its
020: * products.
021: *
022: * Please note that portions of Software may be provided with notices and
023: * open source licenses from such communities and third parties that govern the
024: * use of those portions, and any licenses granted hereunder do not alter any
025: * rights and obligations you may have under such open source licenses,
026: * however, the disclaimer of warranty and limitation of liability provisions
027: * in this License will apply to all Software in this distribution.
028: *
029: * You acknowledge that the Software is not designed, licensed or intended
030: * for use in the design, construction, operation or maintenance of any nuclear
031: * facility.
032: *
033: * Apache License
034: * Version 2.0, January 2004
035: * http://www.apache.org/licenses/
036: *
037: */
038:
039: package com.sun.xml.fastinfoset.algorithm;
040:
041: import java.io.EOFException;
042: import java.io.IOException;
043: import java.io.InputStream;
044: import java.io.OutputStream;
045: import java.nio.CharBuffer;
046: import java.util.ArrayList;
047: import java.util.List;
048: import org.jvnet.fastinfoset.EncodingAlgorithmException;
049: import com.sun.xml.fastinfoset.CommonResourceBundle;
050:
051: public class IntEncodingAlgorithm extends IntegerEncodingAlgorithm {
052:
053: public final int getPrimtiveLengthFromOctetLength(int octetLength)
054: throws EncodingAlgorithmException {
055: if (octetLength % INT_SIZE != 0) {
056: throw new EncodingAlgorithmException(CommonResourceBundle
057: .getInstance().getString(
058: "message.lengthNotMultipleOfInt",
059: new Object[] { Integer.valueOf(INT_SIZE) }));
060: }
061:
062: return octetLength / INT_SIZE;
063: }
064:
065: public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
066: return primitiveLength * INT_SIZE;
067: }
068:
069: public final Object decodeFromBytes(byte[] b, int start, int length)
070: throws EncodingAlgorithmException {
071: int[] data = new int[getPrimtiveLengthFromOctetLength(length)];
072: decodeFromBytesToIntArray(data, 0, b, start, length);
073:
074: return data;
075: }
076:
077: public final Object decodeFromInputStream(InputStream s)
078: throws IOException {
079: return decodeFromInputStreamToIntArray(s);
080: }
081:
082: public void encodeToOutputStream(Object data, OutputStream s)
083: throws IOException {
084: if (!(data instanceof int[])) {
085: throw new IllegalArgumentException(CommonResourceBundle
086: .getInstance().getString("message.dataNotIntArray"));
087: }
088:
089: final int[] idata = (int[]) data;
090:
091: encodeToOutputStreamFromIntArray(idata, s);
092: }
093:
094: public final Object convertFromCharacters(char[] ch, int start,
095: int length) {
096: final CharBuffer cb = CharBuffer.wrap(ch, start, length);
097: final List integerList = new ArrayList();
098:
099: matchWhiteSpaceDelimnatedWords(cb, new WordListener() {
100: public void word(int start, int end) {
101: String iStringValue = cb.subSequence(start, end)
102: .toString();
103: integerList.add(Integer.valueOf(iStringValue));
104: }
105: });
106:
107: return generateArrayFromList(integerList);
108: }
109:
110: public final void convertToCharacters(Object data, StringBuffer s) {
111: if (!(data instanceof int[])) {
112: throw new IllegalArgumentException(CommonResourceBundle
113: .getInstance().getString("message.dataNotIntArray"));
114: }
115:
116: final int[] idata = (int[]) data;
117:
118: convertToCharactersFromIntArray(idata, s);
119: }
120:
121: public final void decodeFromBytesToIntArray(int[] idata,
122: int istart, byte[] b, int start, int length) {
123: final int size = length / INT_SIZE;
124: for (int i = 0; i < size; i++) {
125: idata[istart++] = ((b[start++] & 0xFF) << 24)
126: | ((b[start++] & 0xFF) << 16)
127: | ((b[start++] & 0xFF) << 8) | (b[start++] & 0xFF);
128: }
129: }
130:
131: public final int[] decodeFromInputStreamToIntArray(InputStream s)
132: throws IOException {
133: final List integerList = new ArrayList();
134: final byte[] b = new byte[INT_SIZE];
135:
136: while (true) {
137: int n = s.read(b);
138: if (n != 4) {
139: if (n == -1) {
140: break;
141: }
142:
143: while (n != 4) {
144: final int m = s.read(b, n, INT_SIZE - n);
145: if (m == -1) {
146: throw new EOFException();
147: }
148: n += m;
149: }
150: }
151:
152: final int i = ((b[0] & 0xFF) << 24) | ((b[1] & 0xFF) << 16)
153: | ((b[2] & 0xFF) << 8) | (b[3] & 0xFF);
154: integerList.add(Integer.valueOf(i));
155: }
156:
157: return generateArrayFromList(integerList);
158: }
159:
160: public final void encodeToOutputStreamFromIntArray(int[] idata,
161: OutputStream s) throws IOException {
162: for (int i = 0; i < idata.length; i++) {
163: final int bits = idata[i];
164: s.write((bits >>> 24) & 0xFF);
165: s.write((bits >>> 16) & 0xFF);
166: s.write((bits >>> 8) & 0xFF);
167: s.write(bits & 0xFF);
168: }
169: }
170:
171: public final void encodeToBytes(Object array, int astart,
172: int alength, byte[] b, int start) {
173: encodeToBytesFromIntArray((int[]) array, astart, alength, b,
174: start);
175: }
176:
177: public final void encodeToBytesFromIntArray(int[] idata,
178: int istart, int ilength, byte[] b, int start) {
179: final int iend = istart + ilength;
180: for (int i = istart; i < iend; i++) {
181: final int bits = idata[i];
182: b[start++] = (byte) ((bits >>> 24) & 0xFF);
183: b[start++] = (byte) ((bits >>> 16) & 0xFF);
184: b[start++] = (byte) ((bits >>> 8) & 0xFF);
185: b[start++] = (byte) (bits & 0xFF);
186: }
187: }
188:
189: public final void convertToCharactersFromIntArray(int[] idata,
190: StringBuffer s) {
191: final int end = idata.length - 1;
192: for (int i = 0; i <= end; i++) {
193: s.append(Integer.toString(idata[i]));
194: if (i != end) {
195: s.append(' ');
196: }
197: }
198: }
199:
200: public final int[] generateArrayFromList(List array) {
201: int[] idata = new int[array.size()];
202: for (int i = 0; i < idata.length; i++) {
203: idata[i] = ((Integer) array.get(i)).intValue();
204: }
205:
206: return idata;
207: }
208: }
|