001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * @(#)ASCIIUtility.java 1.9 02/03/27
022: */
023:
024: /*
025: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
026: *
027: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
028: *
029: * The contents of this file are subject to the terms of either the GNU
030: * General Public License Version 2 only ("GPL") or the Common Development
031: * and Distribution License("CDDL") (collectively, the "License"). You
032: * may not use this file except in compliance with the License. You can obtain
033: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
034: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
035: * language governing permissions and limitations under the License.
036: *
037: * When distributing the software, include this License Header Notice in each
038: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
039: * Sun designates this particular file as subject to the "Classpath" exception
040: * as provided by Sun in the GPL Version 2 section of the License file that
041: * accompanied this code. If applicable, add the following below the License
042: * Header, with the fields enclosed by brackets [] replaced by your own
043: * identifying information: "Portions Copyrighted [year]
044: * [name of copyright owner]"
045: *
046: * Contributor(s):
047: *
048: * If you wish your version of this file to be governed by only the CDDL or
049: * only the GPL Version 2, indicate your decision by adding "[Contributor]
050: * elects to include this software in this distribution under the [CDDL or GPL
051: * Version 2] license." If you don't indicate a single choice of license, a
052: * recipient has the option to distribute your version of this file under
053: * either the CDDL, the GPL Version 2 or to extend the choice of license to
054: * its licensees as provided above. However, if you add GPL Version 2 code
055: * and therefore, elected the GPL Version 2 license, then the option applies
056: * only if the new code is made subject to such option by the copyright
057: * holder.
058: */
059: package com.sun.xml.messaging.saaj.packaging.mime.util;
060:
061: import com.sun.xml.messaging.saaj.util.ByteOutputStream;
062:
063: import java.io.*;
064:
065: public class ASCIIUtility {
066:
067: // Private constructor so that this class is not instantiated
068: private ASCIIUtility() {
069: }
070:
071: /**
072: * Convert the bytes within the specified range of the given byte
073: * array into a signed integer in the given radix . The range extends
074: * from <code>start</code> till, but not including <code>end</code>. <p>
075: *
076: * Based on java.lang.Integer.parseInt()
077: */
078: public static int parseInt(byte[] b, int start, int end, int radix)
079: throws NumberFormatException {
080: if (b == null)
081: throw new NumberFormatException("null");
082:
083: int result = 0;
084: boolean negative = false;
085: int i = start;
086: int limit;
087: int multmin;
088: int digit;
089:
090: if (end > start) {
091: if (b[i] == '-') {
092: negative = true;
093: limit = Integer.MIN_VALUE;
094: i++;
095: } else {
096: limit = -Integer.MAX_VALUE;
097: }
098: multmin = limit / radix;
099: if (i < end) {
100: digit = Character.digit((char) b[i++], radix);
101: if (digit < 0) {
102: throw new NumberFormatException("illegal number: "
103: + toString(b, start, end));
104: } else {
105: result = -digit;
106: }
107: }
108: while (i < end) {
109: // Accumulating negatively avoids surprises near MAX_VALUE
110: digit = Character.digit((char) b[i++], radix);
111: if (digit < 0) {
112: throw new NumberFormatException("illegal number");
113: }
114: if (result < multmin) {
115: throw new NumberFormatException("illegal number");
116: }
117: result *= radix;
118: if (result < limit + digit) {
119: throw new NumberFormatException("illegal number");
120: }
121: result -= digit;
122: }
123: } else {
124: throw new NumberFormatException("illegal number");
125: }
126: if (negative) {
127: if (i > start + 1) {
128: return result;
129: } else { /* Only got "-" */
130: throw new NumberFormatException("illegal number");
131: }
132: } else {
133: return -result;
134: }
135: }
136:
137: /**
138: * Convert the bytes within the specified range of the given byte
139: * array into a String. The range extends from <code>start</code>
140: * till, but not including <code>end</code>. <p>
141: */
142: public static String toString(byte[] b, int start, int end) {
143: int size = end - start;
144: char[] theChars = new char[size];
145:
146: for (int i = 0, j = start; i < size;)
147: theChars[i++] = (char) (b[j++] & 0xff);
148:
149: return new String(theChars);
150: }
151:
152: public static byte[] getBytes(String s) {
153: char[] chars = s.toCharArray();
154: int size = chars.length;
155: byte[] bytes = new byte[size];
156:
157: for (int i = 0; i < size;)
158: bytes[i] = (byte) chars[i++];
159: return bytes;
160: }
161:
162: /**
163: *
164: * @deprecated
165: * this is an expensive operation that require an additional
166: * buffer reallocation just to get the array of an exact size.
167: * Unless you absolutely need the exact size array, don't use this.
168: * Use {@link ByteOutputStream} and {@link ByteOutputStream#write(InputStream)}.
169: */
170: public static byte[] getBytes(InputStream is) throws IOException {
171: ByteOutputStream bos = new ByteOutputStream();
172: bos.write(is);
173: return bos.toByteArray();
174: }
175: }
|