src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/algorithm/LongEncodingAlgorithm.java

changeset 0
373ffda63c9a
equal deleted inserted replaced
-1:000000000000 0:373ffda63c9a
1 /*
2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
26 */
27
28 package com.sun.xml.internal.fastinfoset.algorithm;
29
30 import java.io.EOFException;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34 import java.nio.CharBuffer;
35 import java.util.ArrayList;
36 import java.util.List;
37 import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException;
38 import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
39
40
41 public class LongEncodingAlgorithm extends IntegerEncodingAlgorithm {
42
43 public int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
44 if (octetLength % LONG_SIZE != 0) {
45 throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().
46 getString("message.lengthNotMultipleOfLong", new Object[]{Integer.valueOf(LONG_SIZE)}));
47 }
48
49 return octetLength / LONG_SIZE;
50 }
51
52 public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
53 return primitiveLength * LONG_SIZE;
54 }
55
56 public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException {
57 long[] data = new long[getPrimtiveLengthFromOctetLength(length)];
58 decodeFromBytesToLongArray(data, 0, b, start, length);
59
60 return data;
61 }
62
63 public final Object decodeFromInputStream(InputStream s) throws IOException {
64 return decodeFromInputStreamToIntArray(s);
65 }
66
67
68 public void encodeToOutputStream(Object data, OutputStream s) throws IOException {
69 if (!(data instanceof long[])) {
70 throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotLongArray"));
71 }
72
73 final long[] ldata = (long[])data;
74
75 encodeToOutputStreamFromLongArray(ldata, s);
76 }
77
78
79 public Object convertFromCharacters(char[] ch, int start, int length) {
80 final CharBuffer cb = CharBuffer.wrap(ch, start, length);
81 final List longList = new ArrayList();
82
83 matchWhiteSpaceDelimnatedWords(cb,
84 new WordListener() {
85 public void word(int start, int end) {
86 String lStringValue = cb.subSequence(start, end).toString();
87 longList.add(Long.valueOf(lStringValue));
88 }
89 }
90 );
91
92 return generateArrayFromList(longList);
93 }
94
95 public void convertToCharacters(Object data, StringBuffer s) {
96 if (!(data instanceof long[])) {
97 throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotLongArray"));
98 }
99
100 final long[] ldata = (long[])data;
101
102 convertToCharactersFromLongArray(ldata, s);
103 }
104
105
106 public final void decodeFromBytesToLongArray(long[] ldata, int istart, byte[] b, int start, int length) {
107 final int size = length / LONG_SIZE;
108 for (int i = 0; i < size; i++) {
109 ldata[istart++] =
110 ((long)(b[start++] & 0xFF) << 56) |
111 ((long)(b[start++] & 0xFF) << 48) |
112 ((long)(b[start++] & 0xFF) << 40) |
113 ((long)(b[start++] & 0xFF) << 32) |
114 ((long)(b[start++] & 0xFF) << 24) |
115 ((long)(b[start++] & 0xFF) << 16) |
116 ((long)(b[start++] & 0xFF) << 8) |
117 (long)(b[start++] & 0xFF);
118 }
119 }
120
121 public final long[] decodeFromInputStreamToIntArray(InputStream s) throws IOException {
122 final List longList = new ArrayList();
123 final byte[] b = new byte[LONG_SIZE];
124
125 while (true) {
126 int n = s.read(b);
127 if (n != LONG_SIZE) {
128 if (n == -1) {
129 break;
130 }
131
132 while(n != LONG_SIZE) {
133 final int m = s.read(b, n, LONG_SIZE - n);
134 if (m == -1) {
135 throw new EOFException();
136 }
137 n += m;
138 }
139 }
140
141 final long l =
142 (((long) b[0] << 56) +
143 ((long) (b[1] & 0xFF) << 48) +
144 ((long) (b[2] & 0xFF) << 40) +
145 ((long) (b[3] & 0xFF) << 32) +
146 ((long) (b[4] & 0xFF) << 24) +
147 ((b[5] & 0xFF) << 16) +
148 ((b[6] & 0xFF) << 8) +
149 ((b[7] & 0xFF) << 0));
150
151 longList.add(Long.valueOf(l));
152 }
153
154 return generateArrayFromList(longList);
155 }
156
157
158 public final void encodeToOutputStreamFromLongArray(long[] ldata, OutputStream s) throws IOException {
159 for (int i = 0; i < ldata.length; i++) {
160 final long bits = ldata[i];
161 s.write((int)((bits >>> 56) & 0xFF));
162 s.write((int)((bits >>> 48) & 0xFF));
163 s.write((int)((bits >>> 40) & 0xFF));
164 s.write((int)((bits >>> 32) & 0xFF));
165 s.write((int)((bits >>> 24) & 0xFF));
166 s.write((int)((bits >>> 16) & 0xFF));
167 s.write((int)((bits >>> 8) & 0xFF));
168 s.write((int)(bits & 0xFF));
169 }
170 }
171
172 public final void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) {
173 encodeToBytesFromLongArray((long[])array, astart, alength, b, start);
174 }
175
176 public final void encodeToBytesFromLongArray(long[] ldata, int lstart, int llength, byte[] b, int start) {
177 final int lend = lstart + llength;
178 for (int i = lstart; i < lend; i++) {
179 final long bits = ldata[i];
180 b[start++] = (byte)((bits >>> 56) & 0xFF);
181 b[start++] = (byte)((bits >>> 48) & 0xFF);
182 b[start++] = (byte)((bits >>> 40) & 0xFF);
183 b[start++] = (byte)((bits >>> 32) & 0xFF);
184 b[start++] = (byte)((bits >>> 24) & 0xFF);
185 b[start++] = (byte)((bits >>> 16) & 0xFF);
186 b[start++] = (byte)((bits >>> 8) & 0xFF);
187 b[start++] = (byte)(bits & 0xFF);
188 }
189 }
190
191
192 public final void convertToCharactersFromLongArray(long[] ldata, StringBuffer s) {
193 final int end = ldata.length - 1;
194 for (int i = 0; i <= end; i++) {
195 s.append(Long.toString(ldata[i]));
196 if (i != end) {
197 s.append(' ');
198 }
199 }
200 }
201
202
203 public final long[] generateArrayFromList(List array) {
204 long[] ldata = new long[array.size()];
205 for (int i = 0; i < ldata.length; i++) {
206 ldata[i] = ((Long)array.get(i)).longValue();
207 }
208
209 return ldata;
210 }
211 }

mercurial