1 /*
   2  * Copyright (c) 1997, 2014, 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 /*
  27  * @(#)ASCIIUtility.java      1.9 02/03/27
  28  */
  29 
  30 
  31 package com.sun.xml.internal.messaging.saaj.packaging.mime.util;
  32 
  33 import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
  34 
  35 import java.io.*;
  36 
  37 public class ASCIIUtility {
  38 
  39     // Private constructor so that this class is not instantiated
  40     private ASCIIUtility() { }
  41 
  42 
  43     /**
  44      * Convert the bytes within the specified range of the given byte
  45      * array into a signed integer in the given radix . The range extends
  46      * from <code>start</code> till, but not including <code>end</code>. <p>
  47      *
  48      * Based on java.lang.Integer.parseInt()
  49      */
  50     public static int parseInt(byte[] b, int start, int end, int radix)
  51                 throws NumberFormatException {
  52         if (b == null)
  53             throw new NumberFormatException("null");
  54 
  55         int result = 0;
  56         boolean negative = false;
  57         int i = start;
  58         int limit;
  59         int multmin;
  60         int digit;
  61 
  62         if (end > start) {
  63             if (b[i] == '-') {
  64                 negative = true;
  65                 limit = Integer.MIN_VALUE;
  66                 i++;
  67             } else {
  68                 limit = -Integer.MAX_VALUE;
  69             }
  70             multmin = limit / radix;
  71             if (i < end) {
  72                 digit = Character.digit((char)b[i++], radix);
  73                 if (digit < 0) {
  74                     throw new NumberFormatException(
  75                         "illegal number: " + toString(b, start, end)
  76                         );
  77                 } else {
  78                     result = -digit;
  79                 }
  80             }
  81             while (i < end) {
  82                 // Accumulating negatively avoids surprises near MAX_VALUE
  83                 digit = Character.digit((char)b[i++], radix);
  84                 if (digit < 0) {
  85                     throw new NumberFormatException("illegal number");
  86                 }
  87                 if (result < multmin) {
  88                     throw new NumberFormatException("illegal number");
  89                 }
  90                 result *= radix;
  91                 if (result < limit + digit) {
  92                     throw new NumberFormatException("illegal number");
  93                 }
  94                 result -= digit;
  95             }
  96         } else {
  97             throw new NumberFormatException("illegal number");
  98         }
  99         if (negative) {
 100             if (i > start + 1) {
 101                 return result;
 102             } else {    /* Only got "-" */
 103                 throw new NumberFormatException("illegal number");
 104             }
 105         } else {
 106             return -result;
 107         }
 108     }
 109 
 110     /**
 111      * Convert the bytes within the specified range of the given byte
 112      * array into a String. The range extends from <code>start</code>
 113      * till, but not including <code>end</code>. <p>
 114      */
 115     public static String toString(byte[] b, int start, int end) {
 116         int size = end - start;
 117         char[] theChars = new char[size];
 118 
 119         for (int i = 0, j = start; i < size; )
 120             theChars[i++] = (char)(b[j++]&0xff);
 121 
 122         return new String(theChars);
 123     }
 124 
 125     public static byte[] getBytes(String s) {
 126         char [] chars= s.toCharArray();
 127         int size = chars.length;
 128         byte[] bytes = new byte[size];
 129 
 130         for (int i = 0; i < size;)
 131             bytes[i] = (byte) chars[i++];
 132         return bytes;
 133     }
 134 
 135     /**
 136      *
 137      * @deprecated
 138      *      this is an expensive operation that require an additional
 139      *      buffer reallocation just to get the array of an exact size.
 140      *      Unless you absolutely need the exact size array, don't use this.
 141      *      Use {@link ByteOutputStream} and {@link ByteOutputStream#write(InputStream)}.
 142      */
 143     public static byte[] getBytes(InputStream is) throws IOException {
 144         ByteOutputStream bos = null;
 145         try {
 146             bos = new ByteOutputStream();
 147             bos.write(is);
 148         } finally {
 149             if (bos != null)
 150                 bos.close();
 151             is.close();
 152         }
 153         return bos.toByteArray();
 154     }
 155 }