KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > StringHelper


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 import java.io.UnsupportedEncodingException JavaDoc;
23
24 import common.Logger;
25
26 import jxl.WorkbookSettings;
27
28 /**
29  * Helper function to convert Java string objects to and from the byte
30  * representations
31  */

32 public final class StringHelper
33 {
34   /**
35    * The logger
36    */

37   private static Logger logger = Logger.getLogger(StringHelper.class);
38
39   /**
40    * Private default constructor to prevent instantiation
41    */

42   private StringHelper()
43   {
44   }
45
46   /**
47    * Gets the bytes of the specified string. This will simply return the ASCII
48    * values of the characters in the string
49    *
50    * @param s the string to convert into bytes
51    * @return the ASCII values of the characters in the string
52    * @deprecated
53    */

54   public static byte[] getBytes(String JavaDoc s)
55   {
56     return s.getBytes();
57   }
58
59   /**
60    * Gets the bytes of the specified string. This will simply return the ASCII
61    * values of the characters in the string
62    *
63    * @param s the string to convert into bytes
64    * @return the ASCII values of the characters in the string
65    */

66   public static byte[] getBytes(String JavaDoc s, WorkbookSettings ws)
67   {
68     try
69     {
70       return s.getBytes(ws.getEncoding());
71     }
72     catch (UnsupportedEncodingException JavaDoc e)
73     {
74       // fail silently
75
return null;
76     }
77   }
78
79   /**
80    * Converts the string into a little-endian array of Unicode bytes
81    *
82    * @param s the string to convert
83    * @return the unicode values of the characters in the string
84    */

85   public static byte[] getUnicodeBytes(String JavaDoc s)
86   {
87     try
88     {
89       byte[] b = s.getBytes("UnicodeLittle");
90
91       // Sometimes this method writes out the unicode
92
// identifier
93
if (b.length == (s.length() * 2 + 2))
94       {
95         byte[] b2 = new byte[b.length - 2];
96         System.arraycopy(b, 2, b2, 0, b2.length);
97         b = b2;
98       }
99       return b;
100     }
101     catch (UnsupportedEncodingException JavaDoc e)
102     {
103       // Fail silently
104
return null;
105     }
106   }
107
108   /**
109    * Gets the ASCII bytes from the specified string and places them in the
110    * array at the specified position
111    *
112    * @param pos the position at which to place the converted data
113    * @param s the string to convert
114    * @param d the byte array which will contain the converted string data
115    */

116   public static void getBytes(String JavaDoc s, byte[] d, int pos)
117   {
118     byte[] b = getBytes(s);
119     System.arraycopy(b, 0, d, pos, b.length);
120   }
121
122   /**
123    * Inserts the unicode byte representation of the specified string into the
124    * array passed in
125    *
126    * @param pos the position at which to insert the converted data
127    * @param s the string to convert
128    * @param d the byte array which will hold the string data
129    */

130   public static void getUnicodeBytes(String JavaDoc s, byte[] d, int pos)
131   {
132     byte[] b = getUnicodeBytes(s);
133     System.arraycopy(b, 0, d, pos, b.length);
134   }
135
136   /**
137    * Gets a string from the data array using the character encoding for
138    * this workbook
139    *
140    * @param pos The start position of the string
141    * @param length The number of characters in the string
142    * @param d The byte data
143    * @param ws the workbook settings
144    * @return the string built up from the raw bytes
145    */

146   public static String JavaDoc getString(byte[] d, int length, int pos,
147                                  WorkbookSettings ws)
148   {
149     try
150     {
151       byte[] b = new byte[length];
152       System.arraycopy(d, pos, b, 0, length);
153       return new String JavaDoc(b, ws.getEncoding());
154     }
155     catch (UnsupportedEncodingException JavaDoc e)
156     {
157       logger.warn(e.toString());
158       return "";
159     }
160   }
161
162   /**
163    * Gets a string from the data array
164    *
165    * @param pos The start position of the string
166    * @param length The number of characters in the string
167    * @param d The byte data
168    * @return the string built up from the unicode characters
169    */

170   public static String JavaDoc getUnicodeString(byte[] d, int length, int pos)
171   {
172     try
173     {
174       byte[] b = new byte[length * 2];
175       System.arraycopy(d, pos, b, 0, length * 2);
176       return new String JavaDoc(b, "UnicodeLittle");
177     }
178     catch (UnsupportedEncodingException JavaDoc e)
179     {
180       // Fail silently
181
return "";
182     }
183   }
184 }
185
186
187
Popular Tags