1/*
2 * Copyright (c) 1997, 2013, 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
26/* FROM mail.jar */
27package com.sun.xml.internal.org.jvnet.mimepull;
28
29final class ASCIIUtility {
30
31    // Private constructor so that this class is not instantiated
32    private ASCIIUtility() { }
33
34    /**
35     * Convert the bytes within the specified range of the given byte
36     * array into a signed integer in the given radix . The range extends
37     * from <code>start</code> till, but not including <code>end</code>. <p>
38     *
39     * Based on java.lang.Integer.parseInt()
40     */
41    public static int parseInt(byte[] b, int start, int end, int radix)
42                throws NumberFormatException {
43        if (b == null) {
44            throw new NumberFormatException("null");
45        }
46
47        int result = 0;
48        boolean negative = false;
49        int i = start;
50        int limit;
51        int multmin;
52        int digit;
53
54        if (end > start) {
55            if (b[i] == '-') {
56                negative = true;
57                limit = Integer.MIN_VALUE;
58                i++;
59            } else {
60                limit = -Integer.MAX_VALUE;
61            }
62            multmin = limit / radix;
63            if (i < end) {
64                digit = Character.digit((char)b[i++], radix);
65                if (digit < 0) {
66                    throw new NumberFormatException(
67                        "illegal number: " + toString(b, start, end)
68                        );
69                } else {
70                    result = -digit;
71                }
72            }
73            while (i < end) {
74                // Accumulating negatively avoids surprises near MAX_VALUE
75                digit = Character.digit((char)b[i++], radix);
76                if (digit < 0) {
77                    throw new NumberFormatException("illegal number");
78                }
79                if (result < multmin) {
80                    throw new NumberFormatException("illegal number");
81                }
82                result *= radix;
83                if (result < limit + digit) {
84                    throw new NumberFormatException("illegal number");
85                }
86                result -= digit;
87            }
88        } else {
89            throw new NumberFormatException("illegal number");
90        }
91        if (negative) {
92            if (i > start + 1) {
93                return result;
94            } else {    /* Only got "-" */
95                throw new NumberFormatException("illegal number");
96            }
97        } else {
98            return -result;
99        }
100    }
101
102    /**
103     * Convert the bytes within the specified range of the given byte
104     * array into a String. The range extends from <code>start</code>
105     * till, but not including <code>end</code>. <p>
106     */
107    public static String toString(byte[] b, int start, int end) {
108        int size = end - start;
109        char[] theChars = new char[size];
110
111        for (int i = 0, j = start; i < size; ) {
112            theChars[i++] = (char)(b[j++]&0xff);
113        }
114
115        return new String(theChars);
116    }
117
118}
119