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 LongEncodingAlgorithm extends IntegerEncodingAlgorithm {
052:
053: public int getPrimtiveLengthFromOctetLength(int octetLength)
054: throws EncodingAlgorithmException {
055: if (octetLength % LONG_SIZE != 0) {
056: throw new EncodingAlgorithmException(
057: CommonResourceBundle.getInstance()
058: .getString(
059: "message.lengthNotMultipleOfLong",
060: new Object[] { Integer
061: .valueOf(LONG_SIZE) }));
062: }
063:
064: return octetLength / LONG_SIZE;
065: }
066:
067: public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
068: return primitiveLength * LONG_SIZE;
069: }
070:
071: public final Object decodeFromBytes(byte[] b, int start, int length)
072: throws EncodingAlgorithmException {
073: long[] data = new long[getPrimtiveLengthFromOctetLength(length)];
074: decodeFromBytesToLongArray(data, 0, b, start, length);
075:
076: return data;
077: }
078:
079: public final Object decodeFromInputStream(InputStream s)
080: throws IOException {
081: return decodeFromInputStreamToIntArray(s);
082: }
083:
084: public void encodeToOutputStream(Object data, OutputStream s)
085: throws IOException {
086: if (!(data instanceof long[])) {
087: throw new IllegalArgumentException(CommonResourceBundle
088: .getInstance()
089: .getString("message.dataNotLongArray"));
090: }
091:
092: final long[] ldata = (long[]) data;
093:
094: encodeToOutputStreamFromLongArray(ldata, s);
095: }
096:
097: public Object convertFromCharacters(char[] ch, int start, int length) {
098: final CharBuffer cb = CharBuffer.wrap(ch, start, length);
099: final List longList = new ArrayList();
100:
101: matchWhiteSpaceDelimnatedWords(cb, new WordListener() {
102: public void word(int start, int end) {
103: String lStringValue = cb.subSequence(start, end)
104: .toString();
105: longList.add(Long.valueOf(lStringValue));
106: }
107: });
108:
109: return generateArrayFromList(longList);
110: }
111:
112: public void convertToCharacters(Object data, StringBuffer s) {
113: if (!(data instanceof long[])) {
114: throw new IllegalArgumentException(CommonResourceBundle
115: .getInstance()
116: .getString("message.dataNotLongArray"));
117: }
118:
119: final long[] ldata = (long[]) data;
120:
121: convertToCharactersFromLongArray(ldata, s);
122: }
123:
124: public final void decodeFromBytesToLongArray(long[] ldata,
125: int istart, byte[] b, int start, int length) {
126: final int size = length / LONG_SIZE;
127: for (int i = 0; i < size; i++) {
128: ldata[istart++] = ((long) (b[start++] & 0xFF) << 56)
129: | ((long) (b[start++] & 0xFF) << 48)
130: | ((long) (b[start++] & 0xFF) << 40)
131: | ((long) (b[start++] & 0xFF) << 32)
132: | ((long) (b[start++] & 0xFF) << 24)
133: | ((long) (b[start++] & 0xFF) << 16)
134: | ((long) (b[start++] & 0xFF) << 8)
135: | (long) (b[start++] & 0xFF);
136: }
137: }
138:
139: public final long[] decodeFromInputStreamToIntArray(InputStream s)
140: throws IOException {
141: final List longList = new ArrayList();
142: final byte[] b = new byte[LONG_SIZE];
143:
144: while (true) {
145: int n = s.read(b);
146: if (n != LONG_SIZE) {
147: if (n == -1) {
148: break;
149: }
150:
151: while (n != LONG_SIZE) {
152: final int m = s.read(b, n, LONG_SIZE - n);
153: if (m == -1) {
154: throw new EOFException();
155: }
156: n += m;
157: }
158: }
159:
160: final long l = ((long) (b[0] & 0xFF) << 56)
161: | ((long) (b[1] & 0xFF) << 48)
162: | ((long) (b[2] & 0xFF) << 40)
163: | ((long) (b[3] & 0xFF) << 32)
164: | ((b[4] & 0xFF) << 24) | ((b[5] & 0xFF) << 16)
165: | ((b[6] & 0xFF) << 8) | (b[7] & 0xFF);
166: longList.add(Long.valueOf(l));
167: }
168:
169: return generateArrayFromList(longList);
170: }
171:
172: public final void encodeToOutputStreamFromLongArray(long[] ldata,
173: OutputStream s) throws IOException {
174: for (int i = 0; i < ldata.length; i++) {
175: final long bits = ldata[i];
176: s.write((int) ((bits >>> 56) & 0xFF));
177: s.write((int) ((bits >>> 48) & 0xFF));
178: s.write((int) ((bits >>> 40) & 0xFF));
179: s.write((int) ((bits >>> 32) & 0xFF));
180: s.write((int) ((bits >>> 24) & 0xFF));
181: s.write((int) ((bits >>> 16) & 0xFF));
182: s.write((int) ((bits >>> 8) & 0xFF));
183: s.write((int) (bits & 0xFF));
184: }
185: }
186:
187: public final void encodeToBytes(Object array, int astart,
188: int alength, byte[] b, int start) {
189: encodeToBytesFromLongArray((long[]) array, astart, alength, b,
190: start);
191: }
192:
193: public final void encodeToBytesFromLongArray(long[] ldata,
194: int lstart, int llength, byte[] b, int start) {
195: final int lend = lstart + llength;
196: for (int i = lstart; i < lend; i++) {
197: final long bits = ldata[i];
198: b[start++] = (byte) ((bits >>> 56) & 0xFF);
199: b[start++] = (byte) ((bits >>> 48) & 0xFF);
200: b[start++] = (byte) ((bits >>> 40) & 0xFF);
201: b[start++] = (byte) ((bits >>> 32) & 0xFF);
202: b[start++] = (byte) ((bits >>> 24) & 0xFF);
203: b[start++] = (byte) ((bits >>> 16) & 0xFF);
204: b[start++] = (byte) ((bits >>> 8) & 0xFF);
205: b[start++] = (byte) (bits & 0xFF);
206: }
207: }
208:
209: public final void convertToCharactersFromLongArray(long[] ldata,
210: StringBuffer s) {
211: final int end = ldata.length - 1;
212: for (int i = 0; i <= end; i++) {
213: s.append(Long.toString(ldata[i]));
214: if (i != end) {
215: s.append(' ');
216: }
217: }
218: }
219:
220: public final long[] generateArrayFromList(List array) {
221: long[] ldata = new long[array.size()];
222: for (int i = 0; i < ldata.length; i++) {
223: ldata[i] = ((Long) array.get(i)).longValue();
224: }
225:
226: return ldata;
227: }
228: }
|