KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > IntegerHelper


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.biff;
21
22 /**
23  * Converts excel byte representations into integers
24  */

25 public final class IntegerHelper
26 {
27   /**
28    * Private constructor disables the instantiation of this object
29    */

30   private IntegerHelper()
31   {
32   }
33
34   /**
35    * Gets an int from two bytes
36    *
37    * @param b2 the second byte
38    * @param b1 the first byte
39    * @return The integer value
40    */

41   public static int getInt(byte b1, byte b2)
42   {
43     int i1 = ((int) b1) & 0xff;
44     int i2 = ((int) b2) & 0xff;
45     int val = i2 << 8 | i1;
46     return val;
47   }
48
49   /**
50    * Gets an short from two bytes
51    *
52    * @param b2 the second byte
53    * @param b1 the first byte
54    * @return The short value
55    */

56   public static short getShort(byte b1, byte b2)
57   {
58     short i1 = (short) (b1 & 0xff);
59     short i2 = (short) (b2 & 0xff);
60     short val = (short) (i2 << 8 | i1);
61     return val;
62   }
63
64
65   /**
66    * Gets an int from four bytes, doing all the necessary swapping
67    *
68    * @param b1 a byte
69    * @param b2 a byte
70    * @param b3 a byte
71    * @param b4 a byte
72    * @return the integer value represented by the four bytes
73    */

74   public static int getInt(byte b1, byte b2, byte b3, byte b4)
75   {
76     int i1 = getInt(b1, b2);
77     int i2 = getInt(b3, b4);
78
79     int val = i2 << 16 | i1;
80     return val;
81   }
82
83   /**
84    * Gets a two byte array from an integer
85    *
86    * @param i the integer
87    * @return the two bytes
88    */

89   public static byte[] getTwoBytes(int i)
90   {
91     byte[] bytes = new byte[2];
92
93     bytes[0] = (byte) (i & 0xff);
94     bytes[1] = (byte) ((i & 0xff00) >> 8);
95
96     return bytes;
97   }
98
99   /**
100    * Gets a four byte array from an integer
101    *
102    * @param i the integer
103    * @return a four byte array
104    */

105   public static byte[] getFourBytes(int i)
106   {
107     byte[] bytes = new byte[4];
108
109     int i1 = i & 0xffff;
110     int i2 = (i & 0xffff0000) >> 16;
111
112     getTwoBytes(i1, bytes, 0);
113     getTwoBytes(i2, bytes, 2);
114
115     return bytes;
116   }
117
118
119   /**
120    * Converts an integer into two bytes, and places it in the array at the
121    * specified position
122    *
123    * @param target the array to place the byte data into
124    * @param pos the position at which to place the data
125    * @param i the integer value to convert
126    */

127   public static void getTwoBytes(int i, byte[] target, int pos)
128   {
129     byte[] bytes = getTwoBytes(i);
130     target[pos] = bytes[0];
131     target[pos + 1] = bytes[1];
132   }
133
134   /**
135    * Converts an integer into four bytes, and places it in the array at the
136    * specified position
137    *
138    * @param target the array which is to contain the converted data
139    * @param pos the position in the array in which to place the data
140    * @param i the integer to convert
141    */

142   public static void getFourBytes(int i, byte[] target, int pos)
143   {
144     byte[] bytes = getFourBytes(i);
145     target[pos] = bytes[0];
146     target[pos + 1] = bytes[1];
147     target[pos + 2] = bytes[2];
148     target[pos + 3] = bytes[3];
149   }
150 }
151
Popular Tags