From 450be0f4ad2b360e3b9c6bc89c1eff3da087ce54 Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Fri, 24 Oct 2008 04:30:38 +0000 Subject: [PATCH] Removed String methods from LittleEndianInput git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@707541 13f79535-47bb-0310-9956-ffa450edef68 --- .../record/EmbeddedObjectRefSubRecord.java | 5 +-- .../poi/hssf/record/formula/StringPtg.java | 44 +++++++------------ .../LittleEndianByteArrayInputStream.java | 16 ------- .../apache/poi/util/LittleEndianInput.java | 2 - .../poi/util/LittleEndianInputStream.java | 30 ------------- 5 files changed, 19 insertions(+), 78 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/EmbeddedObjectRefSubRecord.java b/src/java/org/apache/poi/hssf/record/EmbeddedObjectRefSubRecord.java index f40dc0a49a..f033149ee8 100644 --- a/src/java/org/apache/poi/hssf/record/EmbeddedObjectRefSubRecord.java +++ b/src/java/org/apache/poi/hssf/record/EmbeddedObjectRefSubRecord.java @@ -26,7 +26,6 @@ import org.apache.poi.hssf.record.formula.Ref3DPtg; import org.apache.poi.hssf.record.formula.RefPtg; import org.apache.poi.util.HexDump; import org.apache.poi.util.LittleEndian; -import org.apache.poi.util.LittleEndianByteArrayOutputStream; import org.apache.poi.util.LittleEndianInput; import org.apache.poi.util.LittleEndianInputStream; import org.apache.poi.util.LittleEndianOutput; @@ -113,10 +112,10 @@ public final class EmbeddedObjectRefSubRecord extends SubRecord { field_3_unicode_flag = ( in.readByte() & 0x01 ) != 0; remaining -= LittleEndian.BYTE_SIZE; if (field_3_unicode_flag) { - field_4_ole_classname = in.readUnicodeLEString(nChars); + field_4_ole_classname = StringUtil.readUnicodeLE(in, nChars); stringByteCount = nChars * 2; } else { - field_4_ole_classname = in.readCompressedUnicode(nChars); + field_4_ole_classname = StringUtil.readCompressedUnicode(in, nChars); stringByteCount = nChars; } } else { diff --git a/src/java/org/apache/poi/hssf/record/formula/StringPtg.java b/src/java/org/apache/poi/hssf/record/formula/StringPtg.java index 59a5521cc2..be6a20d6e4 100644 --- a/src/java/org/apache/poi/hssf/record/formula/StringPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/StringPtg.java @@ -17,8 +17,6 @@ package org.apache.poi.hssf.record.formula; -import org.apache.poi.util.BitField; -import org.apache.poi.util.BitFieldFactory; import org.apache.poi.util.LittleEndianInput; import org.apache.poi.util.LittleEndianOutput; import org.apache.poi.util.StringUtil; @@ -32,29 +30,26 @@ import org.apache.poi.util.StringUtil; * @author Bernard Chesnoy */ public final class StringPtg extends ScalarConstantPtg { - public final static int SIZE = 9; - public final static byte sid = 0x17; - private static final BitField fHighByte = BitFieldFactory.getInstance(0x01); - /** the character (")used in formulas to delimit string literals */ + public final static byte sid = 0x17; + /** the character (") used in formulas to delimit string literals */ private static final char FORMULA_DELIMITER = '"'; + private final boolean _is16bitUnicode; /** * NOTE: OO doc says 16bit length, but BiffViewer says 8 Book says something * totally different, so don't look there! */ - private final int field_1_length; - private final byte field_2_options; private final String field_3_string; /** Create a StringPtg from a stream */ public StringPtg(LittleEndianInput in) { - field_1_length = in.readUByte(); - field_2_options = in.readByte(); - if (fHighByte.isSet(field_2_options)) { - field_3_string = in.readUnicodeLEString(field_1_length); - } else { - field_3_string = in.readCompressedUnicode(field_1_length); - } + int nChars = in.readUByte(); // Note - nChars is 8-bit + _is16bitUnicode = (in.readByte() & 0x01) != 0; + if (_is16bitUnicode) { + field_3_string = StringUtil.readUnicodeLE(in, nChars); + } else { + field_3_string = StringUtil.readCompressedUnicode(in, nChars); + } } /** @@ -70,9 +65,8 @@ public final class StringPtg extends ScalarConstantPtg { throw new IllegalArgumentException( "String literals in formulas can't be bigger than 255 characters ASCII"); } - field_2_options = (byte) fHighByte.setBoolean(0, StringUtil.hasMultibyte(value)); + _is16bitUnicode = StringUtil.hasMultibyte(value); field_3_string = value; - field_1_length = value.length(); // for the moment, we support only ASCII strings in formulas we create } public String getValue() { @@ -81,21 +75,17 @@ public final class StringPtg extends ScalarConstantPtg { public void write(LittleEndianOutput out) { out.writeByte(sid + getPtgClass()); - out.writeByte(field_1_length); - out.writeByte(field_2_options); - if (fHighByte.isSet(field_2_options)) { - StringUtil.putUnicodeLE(getValue(), out); + out.writeByte(field_3_string.length()); // Note - nChars is 8-bit + out.writeByte(_is16bitUnicode ? 0x01 : 0x00); + if (_is16bitUnicode) { + StringUtil.putUnicodeLE(field_3_string, out); } else { - StringUtil.putCompressedUnicode(getValue(), out); + StringUtil.putCompressedUnicode(field_3_string, out); } } public int getSize() { - if (fHighByte.isSet(field_2_options)) { - return 2 * field_1_length + 3; - } else { - return field_1_length + 3; - } + return 3 + field_3_string.length() * (_is16bitUnicode ? 2 : 1); } public String toFormulaString() { diff --git a/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java b/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java index 3ea0ab61a2..2729ab773a 100644 --- a/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java +++ b/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java @@ -114,20 +114,4 @@ public final class LittleEndianByteArrayInputStream implements LittleEndianInput public double readDouble() { return Double.longBitsToDouble(readLong()); } - public String readCompressedUnicode(int nChars) { - checkPosition(nChars); - char[] buf = new char[nChars]; - for (int i = 0; i < buf.length; i++) { - buf[i] = (char) readUByte(); - } - return new String(buf); - } - public String readUnicodeLEString(int nChars) { - checkPosition(nChars*2); - char[] buf = new char[nChars]; - for (int i = 0; i < buf.length; i++) { - buf[i] = (char) readUShort(); - } - return new String(buf); - } } diff --git a/src/java/org/apache/poi/util/LittleEndianInput.java b/src/java/org/apache/poi/util/LittleEndianInput.java index 433999dfc3..64bbd18626 100644 --- a/src/java/org/apache/poi/util/LittleEndianInput.java +++ b/src/java/org/apache/poi/util/LittleEndianInput.java @@ -30,6 +30,4 @@ public interface LittleEndianInput { double readDouble(); void readFully(byte[] buf); void readFully(byte[] buf, int off, int len); - String readUnicodeLEString(int nChars); - String readCompressedUnicode(int nChars); } diff --git a/src/java/org/apache/poi/util/LittleEndianInputStream.java b/src/java/org/apache/poi/util/LittleEndianInputStream.java index 035230491b..329119bfbe 100644 --- a/src/java/org/apache/poi/util/LittleEndianInputStream.java +++ b/src/java/org/apache/poi/util/LittleEndianInputStream.java @@ -131,34 +131,4 @@ public class LittleEndianInputStream extends FilterInputStream implements Little buf[i] = (byte) ch; } } - public String readCompressedUnicode(int nChars) { - char[] buf = new char[nChars]; - for (int i = 0; i < buf.length; i++) { - int ch; - try { - ch = in.read(); - } catch (IOException e) { - throw new RuntimeException(e); - } - checkEOF(ch); - buf[i] = (char) ch; - - } - return new String(buf); - } - public String readUnicodeLEString(int nChars) { - char[] buf = new char[nChars]; - for (int i = 0; i < buf.length; i++) { - int ch; - try { - ch = in.read(); - } catch (IOException e) { - throw new RuntimeException(e); - } - checkEOF(ch); - buf[i] = (char) ch; - - } - return new String(buf); - } } -- 2.39.5