Rewinded: StringUtil.java: Sync to REL_2 BRANCH

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353249 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
tetsuya 2003-07-26 08:53:22 +00:00
parent d20e067403
commit e29ec7063a
1 changed files with 331 additions and 314 deletions

View File

@ -2,7 +2,7 @@
* ==================================================================== * ====================================================================
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 2003 The Apache Software Foundation. All rights * Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -55,32 +55,38 @@
package org.apache.poi.util; package org.apache.poi.util;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.text.FieldPosition; import java.text.FieldPosition;
import java.util.Arrays;
/** /**
* Title: String Utility Description: Collection of string handling utilities * Title: String Utility Description: Collection of string handling utilities
* *
* Now it is quite confusing: the method pairs, in which
* one of them write data and other read written data are:
* putUncompressedUnicodeHigh and getFromUnicode
* putUncompressedUnicode and getFromUnicodeHigh
* *
*@author Andrew C. Oliver *@author Andrew C. Oliver
*@author Sergei Kozello (sergeikozello at mail.ru) *@author Sergei Kozello (sergeikozello at mail.ru)
*@author Toshiaki Kamoshida (kamoshida.toshiaki at future dot co dot jp)
*@created May 10, 2002 *@created May 10, 2002
*@version 1.0 *@version 1.0
*/ */
public class StringUtil { public class StringUtil {
private final static String ENCODING="ISO-8859-1"; private final static String ENCODING="ISO-8859-1";
/** /**
* Constructor for the StringUtil object * Constructor for the StringUtil object
*/ */
private StringUtil() { private StringUtil() { }
}
/** /**
* given a byte array of 16-bit unicode characters, compress to 8-bit and * given a byte array of 16-bit unicode characters, compress to 8-bit and
* return a string * return a string
* *
* { 0x16, 0x00 } -0x16 * { 0x16, 0x00 } -> 0x16
* *
*@param string the byte array to be converted *@param string the byte array to be converted
*@param offset the initial offset into the *@param offset the initial offset into the
@ -96,11 +102,11 @@ public class StringUtil {
* length) * length)
*@len the length of the final string *@len the length of the final string
*/ */
public static String getFromUnicodeLE(
final byte[] string, public static String getFromUnicodeHigh(final byte[] string,
final int offset, final int offset, final int len)
final int len)
throws ArrayIndexOutOfBoundsException, IllegalArgumentException { throws ArrayIndexOutOfBoundsException, IllegalArgumentException {
if ((offset < 0) || (offset >= string.length)) { if ((offset < 0) || (offset >= string.length)) {
throw new ArrayIndexOutOfBoundsException("Illegal offset"); throw new ArrayIndexOutOfBoundsException("Illegal offset");
} }
@ -108,34 +114,39 @@ public class StringUtil {
throw new IllegalArgumentException("Illegal length"); throw new IllegalArgumentException("Illegal length");
} }
try { char[] chars = new char[ len ];
return new String(string, offset, len * 2, "UTF-16LE"); for ( int i = 0; i < chars.length; i++ ) {
} catch (UnsupportedEncodingException e) { chars[i] = (char)( string[ offset + ( 2*i ) ] & 0xFF |
throw new InternalError(); /*unreachable*/ ( string[ offset + ( 2*i+1 ) ] << 8 ) );
} }
return new String( chars );
} }
/** /**
* given a byte array of 16-bit unicode characters, compress to 8-bit and * given a byte array of 16-bit unicode characters, compress to 8-bit and
* return a string * return a string
* *
* { 0x16, 0x00 } -0x16 * { 0x16, 0x00 } -> 0x16
* *
*@param string the byte array to be converted *@param string the byte array to be converted
*@return the converted string *@return the converted string
*/ */
public static String getFromUnicodeLE(final byte[] string) {
return getFromUnicodeLE(string, 0, string.length / 2); public static String getFromUnicodeHigh( final byte[] string ) {
return getFromUnicodeHigh( string, 0, string.length / 2 );
} }
/** /**
* given a byte array of 16-bit unicode characters, compress to 8-bit and * given a byte array of 16-bit unicode characters, compress to 8-bit and
* return a string * return a string
* *
* { 0x00, 0x16 } -0x16 * { 0x00, 0x16 } -> 0x16
* *
*@param string the byte array to be converted *@param string the byte array to be converted
**@param offset the initial offset into the *@param offset the initial offset into the
* byte array. it is assumed that string[ offset ] and string[ offset + * byte array. it is assumed that string[ offset ] and string[ offset +
* 1 ] contain the first 16-bit unicode character * 1 ] contain the first 16-bit unicode character
*@param len *@param len
@ -148,10 +159,9 @@ public class StringUtil {
* length) * length)
*@len the length of the final string *@len the length of the final string
*/ */
public static String getFromUnicodeBE(
final byte[] string, public static String getFromUnicode(final byte[] string,
final int offset, final int offset, final int len)
final int len)
throws ArrayIndexOutOfBoundsException, IllegalArgumentException { throws ArrayIndexOutOfBoundsException, IllegalArgumentException {
if ((offset < 0) || (offset >= string.length)) { if ((offset < 0) || (offset >= string.length)) {
throw new ArrayIndexOutOfBoundsException("Illegal offset"); throw new ArrayIndexOutOfBoundsException("Illegal offset");
@ -159,41 +169,49 @@ public class StringUtil {
if ((len < 0) || (((string.length - offset) / 2) < len)) { if ((len < 0) || (((string.length - offset) / 2) < len)) {
throw new IllegalArgumentException("Illegal length"); throw new IllegalArgumentException("Illegal length");
} }
try {
return new String(string, offset, len * 2, "UTF-16BE");
} catch (UnsupportedEncodingException e) { char[] chars = new char[ len ];
throw new InternalError(); /*unreachable*/ for ( int i = 0; i < chars.length; i++ ) {
chars[i] = (char)( ( string[ offset + ( 2*i ) ] << 8 ) +
string[ offset + ( 2*i+1 ) ] );
} }
return new String( chars );
} }
/** /**
* given a byte array of 16-bit unicode characters, compress to 8-bit and * given a byte array of 16-bit unicode characters, compress to 8-bit and
* return a string * return a string
* *
* { 0x00, 0x16 } -0x16 * { 0x00, 0x16 } -> 0x16
* *
*@param string the byte array to be converted *@param string the byte array to be converted
*@return the converted string *@return the converted string
*/ */
public static String getFromUnicodeBE(final byte[] string) {
return getFromUnicodeBE(string, 0, string.length / 2); public static String getFromUnicode(final byte[] string) {
return getFromUnicode(string, 0, string.length / 2);
} }
/** /**
* read compressed unicode(8bit) * read compressed unicode(8bit)
* *
* @author Toshiaki Kamoshida(kamoshida.toshiaki at future dot co dot jp)
*
* @param string byte array to read * @param string byte array to read
* @param offset offset to read byte array * @param offset offset to read byte array
* @param len length to read byte array * @param len length to read byte array
* @return String generated String instance by reading byte array * @return String generated String instance by reading byte array
*/ */
public static String getFromCompressedUnicode( public static String getFromCompressedUnicode(final byte[] string,
final byte[] string, final int offset, final int len){
final int offset,
final int len) {
try{ try{
return new String(string,offset,len,"ISO-8859-1"); return new String(string,offset,len,"ISO-8859-1");
} catch (UnsupportedEncodingException e) { }
catch(UnsupportedEncodingException e){
throw new InternalError();/* unreachable */ throw new InternalError();/* unreachable */
} }
} }
@ -206,15 +224,36 @@ public class StringUtil {
*@param offset an offset into the byte arrat at which the data is start *@param offset an offset into the byte arrat at which the data is start
* when written * when written
*/ */
public static void putCompressedUnicode(
final String input, public static void putCompressedUnicode(final String input,
final byte[] output, final byte[] output,
final int offset) { final int offset) {
try { int strlen = input.length();
byte[] bytes = input.getBytes("ISO-8859-1");
System.arraycopy(bytes, 0, output, offset, bytes.length); for (int k = 0; k < strlen; k++) {
} catch (UnsupportedEncodingException e) { output[offset + k] = (byte) input.charAt(k);
throw new InternalError(); /*unreachable*/ }
}
/**
* Write uncompressed unicode
*
*@param input the String containing the unicode data to be written
*@param output the byte array to hold the uncompressed unicode
*@param offset the offset to start writing into the byte array
*/
public static void putUncompressedUnicode(final String input,
final byte[] output,
final int offset) {
int strlen = input.length();
for (int k = 0; k < strlen; k++) {
char c = input.charAt(k);
output[offset + (2 * k)] = (byte) c;
output[offset + (2 * k) + 1] = (byte) (c >> 8);
} }
} }
@ -225,36 +264,22 @@ public class StringUtil {
*@param output the byte array to hold the uncompressed unicode *@param output the byte array to hold the uncompressed unicode
*@param offset the offset to start writing into the byte array *@param offset the offset to start writing into the byte array
*/ */
public static void putUnicodeLE(
final String input, public static void putUncompressedUnicodeHigh(final String input,
final byte[] output, final byte[] output,
final int offset) { final int offset) {
try { int strlen = input.length();
byte[] bytes = input.getBytes("UTF-16LE");
System.arraycopy(bytes, 0, output, offset, bytes.length); for (int k = 0; k < strlen; k++) {
} catch (UnsupportedEncodingException e) { char c = input.charAt(k);
throw new InternalError(); /*unreachable*/
output[offset + (2 * k)] = (byte) (c >> 8);
output[offset + (2 * k)] = (byte) c;
} }
} }
/**
* Write uncompressed unicode
*
*@param input the String containing the unicode data to be written
*@param output the byte array to hold the uncompressed unicode
*@param offset the offset to start writing into the byte array
*/
public static void putUnicodeBE(
final String input,
final byte[] output,
final int offset) {
try {
byte[] bytes = input.getBytes("UTF-16BE");
System.arraycopy(bytes, 0, output, offset, bytes.length);
} catch (UnsupportedEncodingException e) {
throw new InternalError(); /*unreachable*/
}
}
/** /**
* Description of the Method * Description of the Method
@ -266,25 +291,21 @@ public class StringUtil {
public static String format(String message, Object[] params) { public static String format(String message, Object[] params) {
int currentParamNumber = 0; int currentParamNumber = 0;
StringBuffer formattedMessage = new StringBuffer(); StringBuffer formattedMessage = new StringBuffer();
for (int i = 0; i < message.length(); i++) { for (int i = 0; i < message.length(); i++) {
if (message.charAt(i) == '%') { if (message.charAt(i) == '%') {
if (currentParamNumber >= params.length) { if (currentParamNumber >= params.length) {
formattedMessage.append("?missing data?"); formattedMessage.append("?missing data?");
} else if ( } else if ((params[currentParamNumber] instanceof Number)
(params[currentParamNumber] instanceof Number)
&& (i + 1 < message.length())) { && (i + 1 < message.length())) {
i i += matchOptionalFormatting(
+= matchOptionalFormatting(
(Number) params[currentParamNumber++], (Number) params[currentParamNumber++],
message.substring(i + 1), message.substring(i + 1), formattedMessage);
formattedMessage);
} else { } else {
formattedMessage.append( formattedMessage.append(params[currentParamNumber++].toString());
params[currentParamNumber++].toString());
} }
} else { } else {
if ((message.charAt(i) == '\\') if ((message.charAt(i) == '\\') && (i + 1 < message.length())
&& (i + 1 < message.length())
&& (message.charAt(i + 1) == '%')) { && (message.charAt(i + 1) == '%')) {
formattedMessage.append('%'); formattedMessage.append('%');
i++; i++;
@ -296,6 +317,7 @@ public class StringUtil {
return formattedMessage.toString(); return formattedMessage.toString();
} }
/** /**
* Description of the Method * Description of the Method
* *
@ -304,31 +326,26 @@ public class StringUtil {
*@param outputTo Description of the Parameter *@param outputTo Description of the Parameter
*@return Description of the Return Value *@return Description of the Return Value
*/ */
private static int matchOptionalFormatting( private static int matchOptionalFormatting(Number number,
Number number,
String formatting, String formatting,
StringBuffer outputTo) { StringBuffer outputTo) {
NumberFormat numberFormat = NumberFormat.getInstance(); NumberFormat numberFormat = NumberFormat.getInstance();
if ((0 < formatting.length()) if ((0 < formatting.length())
&& Character.isDigit(formatting.charAt(0))) { && Character.isDigit(formatting.charAt(0))) {
numberFormat.setMinimumIntegerDigits( numberFormat.setMinimumIntegerDigits(Integer.parseInt(formatting.charAt(0) + ""));
Integer.parseInt(formatting.charAt(0) + "")); if ((2 < formatting.length()) && (formatting.charAt(1) == '.')
if ((2 < formatting.length())
&& (formatting.charAt(1) == '.')
&& Character.isDigit(formatting.charAt(2))) { && Character.isDigit(formatting.charAt(2))) {
numberFormat.setMaximumFractionDigits( numberFormat.setMaximumFractionDigits(Integer.parseInt(formatting.charAt(2) + ""));
Integer.parseInt(formatting.charAt(2) + ""));
numberFormat.format(number, outputTo, new FieldPosition(0)); numberFormat.format(number, outputTo, new FieldPosition(0));
return 3; return 3;
} }
numberFormat.format(number, outputTo, new FieldPosition(0)); numberFormat.format(number, outputTo, new FieldPosition(0));
return 1; return 1;
} else if ( } else if ((0 < formatting.length()) && (formatting.charAt(0) == '.')) {
(0 < formatting.length()) && (formatting.charAt(0) == '.')) {
if ((1 < formatting.length()) if ((1 < formatting.length())
&& Character.isDigit(formatting.charAt(1))) { && Character.isDigit(formatting.charAt(1))) {
numberFormat.setMaximumFractionDigits( numberFormat.setMaximumFractionDigits(Integer.parseInt(formatting.charAt(1) + ""));
Integer.parseInt(formatting.charAt(1) + ""));
numberFormat.format(number, outputTo, new FieldPosition(0)); numberFormat.format(number, outputTo, new FieldPosition(0));
return 2; return 2;
} }