]> source.dussan.org Git - poi.git/commitdiff
Further conversion of Ptg classes to use LittleEndian input/output interfaces
authorJosh Micich <josh@apache.org>
Fri, 24 Oct 2008 02:08:47 +0000 (02:08 +0000)
committerJosh Micich <josh@apache.org>
Fri, 24 Oct 2008 02:08:47 +0000 (02:08 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@707525 13f79535-47bb-0310-9956-ffa450edef68

37 files changed:
src/java/org/apache/poi/hssf/record/CRNRecord.java
src/java/org/apache/poi/hssf/record/constant/ConstantValueParser.java
src/java/org/apache/poi/hssf/record/formula/Area2DPtgBase.java
src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java
src/java/org/apache/poi/hssf/record/formula/AreaErrPtg.java
src/java/org/apache/poi/hssf/record/formula/AreaNPtg.java
src/java/org/apache/poi/hssf/record/formula/AreaPtg.java
src/java/org/apache/poi/hssf/record/formula/AreaPtgBase.java
src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java
src/java/org/apache/poi/hssf/record/formula/AttrPtg.java
src/java/org/apache/poi/hssf/record/formula/BoolPtg.java
src/java/org/apache/poi/hssf/record/formula/DeletedArea3DPtg.java
src/java/org/apache/poi/hssf/record/formula/DeletedRef3DPtg.java
src/java/org/apache/poi/hssf/record/formula/ErrPtg.java
src/java/org/apache/poi/hssf/record/formula/ExpPtg.java
src/java/org/apache/poi/hssf/record/formula/FuncPtg.java
src/java/org/apache/poi/hssf/record/formula/FuncVarPtg.java
src/java/org/apache/poi/hssf/record/formula/IntPtg.java
src/java/org/apache/poi/hssf/record/formula/MemAreaPtg.java
src/java/org/apache/poi/hssf/record/formula/MemErrPtg.java
src/java/org/apache/poi/hssf/record/formula/MemFuncPtg.java
src/java/org/apache/poi/hssf/record/formula/NamePtg.java
src/java/org/apache/poi/hssf/record/formula/NameXPtg.java
src/java/org/apache/poi/hssf/record/formula/NumberPtg.java
src/java/org/apache/poi/hssf/record/formula/Ptg.java
src/java/org/apache/poi/hssf/record/formula/Ref2DPtgBase.java
src/java/org/apache/poi/hssf/record/formula/Ref3DPtg.java
src/java/org/apache/poi/hssf/record/formula/RefErrorPtg.java
src/java/org/apache/poi/hssf/record/formula/RefNPtg.java
src/java/org/apache/poi/hssf/record/formula/RefPtg.java
src/java/org/apache/poi/hssf/record/formula/RefPtgBase.java
src/java/org/apache/poi/hssf/record/formula/StringPtg.java
src/java/org/apache/poi/hssf/record/formula/TblPtg.java
src/java/org/apache/poi/util/LittleEndianByteArrayOutputStream.java
src/java/org/apache/poi/util/StringUtil.java
src/testcases/org/apache/poi/hssf/record/constant/TestConstantValueParser.java
src/testcases/org/apache/poi/hssf/record/formula/TestArrayPtg.java

index 417f2a559a3250c749facea04750cb82f51bffec..050076cca27e362635e5f2df7ee492a6bad79ee7 100755 (executable)
 package org.apache.poi.hssf.record;
 
 import org.apache.poi.hssf.record.constant.ConstantValueParser;
-import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianByteArrayOutputStream;
+import org.apache.poi.util.LittleEndianOutput;
 
 /**
- * Title:       CRN  <P>
- * Description: This record stores the contents of an external cell or cell range <P>
- * REFERENCE:  5.23<P>
+ * Title:       CRN(0x005A) <p/>
+ * Description: This record stores the contents of an external cell or cell range <p/>
+ * REFERENCE:   OOO 5.23<p/>
  *
  * @author josh micich
  */
 public final class CRNRecord extends Record {
-       public final static short sid = 0x5A;
+       public final static short sid = 0x005A;
 
        private int      field_1_last_column_index;
        private int      field_2_first_column_index;
@@ -45,8 +46,8 @@ public final class CRNRecord extends Record {
 
 
        public CRNRecord(RecordInputStream in) {
-               field_1_last_column_index = in.readByte() & 0x00FF;
-               field_2_first_column_index = in.readByte() & 0x00FF;
+               field_1_last_column_index = in.readUByte();
+               field_2_first_column_index = in.readUByte();
                field_3_row_index = in.readShort();
                int nValues = field_1_last_column_index - field_2_first_column_index + 1;
                field_4_constant_values = ConstantValueParser.parse(in, nValues);
@@ -68,13 +69,15 @@ public final class CRNRecord extends Record {
 
        public int serialize(int offset, byte [] data) {
                int dataSize = getDataSize();
-               LittleEndian.putShort(data, 0 + offset, sid);
-               LittleEndian.putShort(data, 2 + offset, (short) dataSize);
-               LittleEndian.putByte(data, 4 + offset, field_1_last_column_index);
-               LittleEndian.putByte(data, 5 + offset, field_2_first_column_index);
-               LittleEndian.putShort(data, 6 + offset, (short) field_3_row_index);
-               ConstantValueParser.encode(data, 8 + offset, field_4_constant_values);
-               return getRecordSize();
+               int recSize = 4 + dataSize;
+               LittleEndianOutput out = new LittleEndianByteArrayOutputStream(data, offset, recSize);
+               out.writeShort(sid);
+               out.writeShort(dataSize);
+               out.writeByte(field_1_last_column_index);
+               out.writeByte(field_2_first_column_index);
+               out.writeShort(field_3_row_index);
+               ConstantValueParser.encode(out, field_4_constant_values);
+               return recSize;
        }
 
        public int getRecordSize() {
index 12f26bfdee5fdbd44df17eaca84923114eef2729..8304eb0ee33a3f5db87dcb285ef8a4553b928b5e 100755 (executable)
 
 package org.apache.poi.hssf.record.constant;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.hssf.record.UnicodeString;
 import org.apache.poi.hssf.record.UnicodeString.UnicodeRecordStats;
-import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianInput;
+import org.apache.poi.util.LittleEndianOutput;
+import org.apache.poi.util.StringUtil;
 
 /**
  * To support Constant Values (2.5.7) as required by the CRN record.
@@ -47,7 +48,7 @@ public final class ConstantValueParser {
                // no instances of this class
        }
 
-       public static Object[] parse(RecordInputStream in, int nValues) {
+       public static Object[] parse(LittleEndianInput in, int nValues) {
                Object[] result = new Object[nValues];
                for (int i = 0; i < result.length; i++) {
                        result[i] = readAConstantValue(in);
@@ -55,7 +56,7 @@ public final class ConstantValueParser {
                return result;
        }
 
-       private static Object readAConstantValue(RecordInputStream in) {
+       private static Object readAConstantValue(LittleEndianInput in) {
                byte grbit = in.readByte();
                switch(grbit) {
                        case TYPE_EMPTY:
@@ -64,7 +65,7 @@ public final class ConstantValueParser {
                        case TYPE_NUMBER:
                                return new Double(in.readDouble());
                        case TYPE_STRING:
-                               return in.readUnicodeString();
+                               return new UnicodeString(StringUtil.readUnicodeString(in));
                        case TYPE_BOOLEAN:
                                return readBoolean(in);
                        case TYPE_ERROR_CODE:
@@ -77,7 +78,7 @@ public final class ConstantValueParser {
                throw new RuntimeException("Unknown grbit value (" + grbit + ")");
        }
 
-       private static Object readBoolean(RecordInputStream in) {
+       private static Object readBoolean(LittleEndianInput in) {
                byte val = (byte)in.readLong(); // 7 bytes 'not used'
                switch(val) {
                        case FALSE_ENCODING:
@@ -116,46 +117,43 @@ public final class ConstantValueParser {
                return urs.recordSize;
        }
 
-       public static void encode(byte[] data, int offset, Object[] values) {
-               int currentOffset = offset;
+       public static void encode(LittleEndianOutput out, Object[] values) {
                for (int i = 0; i < values.length; i++) {
-                       currentOffset += encodeSingleValue(data, currentOffset, values[i]);
+                       encodeSingleValue(out, values[i]);
                }
        }
 
-       private static int encodeSingleValue(byte[] data, int offset, Object value) {
+       private static void encodeSingleValue(LittleEndianOutput out, Object value) {
                if (value == EMPTY_REPRESENTATION) {
-                       LittleEndian.putByte(data, offset, TYPE_EMPTY);
-                       LittleEndian.putLong(data, offset+1, 0L);
-                       return 9;
+                       out.writeByte(TYPE_EMPTY);
+                       out.writeLong(0L);
+                       return;
                }
                if (value instanceof Boolean) {
                        Boolean bVal = ((Boolean)value);
-                       LittleEndian.putByte(data, offset, TYPE_BOOLEAN);
+                       out.writeByte(TYPE_BOOLEAN);
                        long longVal = bVal.booleanValue() ? 1L : 0L;
-                       LittleEndian.putLong(data, offset+1, longVal);
-                       return 9;
+                       out.writeLong(longVal);
+                       return;
                }
                if (value instanceof Double) {
                        Double dVal = (Double) value;
-                       LittleEndian.putByte(data, offset, TYPE_NUMBER);
-                       LittleEndian.putDouble(data, offset+1, dVal.doubleValue());
-                       return 9;
+                       out.writeByte(TYPE_NUMBER);
+                       out.writeDouble(dVal.doubleValue());
+                       return;
                }
                if (value instanceof UnicodeString) {
                        UnicodeString usVal = (UnicodeString) value;
-                       LittleEndian.putByte(data, offset, TYPE_STRING);
-                       UnicodeRecordStats urs = new UnicodeRecordStats();
-                       usVal.serialize(urs, offset +1, data);
-                       return 1 + urs.recordSize;
+                       out.writeByte(TYPE_STRING);
+                       StringUtil.writeUnicodeString(out, usVal.getString());
+                       return;
                }
                if (value instanceof ErrorConstant) {
                        ErrorConstant ecVal = (ErrorConstant) value;
-                       LittleEndian.putByte(data, offset, TYPE_ERROR_CODE);
-                       LittleEndian.putUShort(data, offset+1, ecVal.getErrorCode());
-                       LittleEndian.putUShort(data, offset+3, 0);
-                       LittleEndian.putInt(data, offset+5, 0);
-                       return 9;
+                       out.writeByte(TYPE_ERROR_CODE);
+                       long longVal = ecVal.getErrorCode();
+                       out.writeLong(longVal);
+                       return;
                }
 
                throw new IllegalStateException("Unexpected value type (" + value.getClass().getName() + "'");
index 5bee0afe03b9bca0a4832b37568a5456ae5d613c..7802fcd13f5b7ac568f9c0622821ecd681f05a23 100644 (file)
@@ -17,7 +17,7 @@
 \r
 package org.apache.poi.hssf.record.formula;\r
 \r
-import org.apache.poi.hssf.record.RecordInputStream;\r
+import org.apache.poi.util.LittleEndianInput;\r
 import org.apache.poi.util.LittleEndianOutput;\r
 \r
 /**\r
@@ -30,7 +30,7 @@ public abstract class Area2DPtgBase extends AreaPtgBase {
                super(firstRow, lastRow, firstColumn, lastColumn, firstRowRelative, lastRowRelative, firstColRelative, lastColRelative);\r
        }\r
 \r
-       protected Area2DPtgBase(RecordInputStream in) {\r
+       protected Area2DPtgBase(LittleEndianInput in)  {\r
                readCoordinates(in);\r
        }\r
 \r
index e5c2fdc10dac9b94746326b01ebdde1479fbc472..675ee45551aa09d14bda860250ae396e8824b059 100644 (file)
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.ss.formula.ExternSheetReferenceToken;
 import org.apache.poi.ss.formula.FormulaRenderingWorkbook;
 import org.apache.poi.ss.formula.WorkbookDependentFormula;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -44,7 +44,7 @@ public final class Area3DPtg extends AreaPtgBase implements WorkbookDependentFor
                setExternSheetIndex( externIdx );
        }
 
-       public Area3DPtg(RecordInputStream in) {
+       public Area3DPtg(LittleEndianInput in)  {
                field_1_index_extern_sheet = in.readShort();
                readCoordinates(in);
        }
index f50af0d0cf259fe7d46aa5540212c8acdfb0ee9a..dfbfb7ab32b49ce3c551f05e11cdd08bc0fdfb29 100644 (file)
@@ -17,8 +17,8 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -36,7 +36,7 @@ public final class AreaErrPtg extends OperandPtg {
                unused2 = 0;
        }
 
-       public AreaErrPtg(RecordInputStream in) {
+       public AreaErrPtg(LittleEndianInput in)  {
                // 8 bytes unused:
                unused1 = in.readInt();
                unused2 = in.readInt();
index fc391b801d03829393b37c5d782431ce6b621244..f8b761f315eb05072c8762764cbbb60aa78b6942 100644 (file)
@@ -17,7 +17,7 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.util.LittleEndianInput;
 
 /**
  * Specifies a rectangular area of cells A1:A4 for instance.
@@ -26,7 +26,7 @@ import org.apache.poi.hssf.record.RecordInputStream;
 public final class AreaNPtg extends Area2DPtgBase {
        public final static short sid = 0x2D;
 
-       public AreaNPtg(RecordInputStream in) {
+       public AreaNPtg(LittleEndianInput in)  {
                super(in);
        }
 
index 77e9fa0b5e67759cfa1a1b85de0748d27306f2bd..1da9b784c85e4a846227722b3d08712d6615ab96 100755 (executable)
@@ -17,7 +17,7 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.util.LittleEndianInput;
 
 /**
  * Specifies a rectangular area of cells A1:A4 for instance.
@@ -29,7 +29,7 @@ public final class AreaPtg extends Area2DPtgBase {
        public AreaPtg(int firstRow, int lastRow, int firstColumn, int lastColumn, boolean firstRowRelative, boolean lastRowRelative, boolean firstColRelative, boolean lastColRelative) {
                super(firstRow, lastRow, firstColumn, lastColumn, firstRowRelative, lastRowRelative, firstColRelative, lastColRelative);
        }
-       public AreaPtg(RecordInputStream in) {
+       public AreaPtg(LittleEndianInput in)  {
                super(in);
        }
        public AreaPtg(String arearef) {
index 2fa1290c0c65d1e302424b90ff4a528491358c8e..8e338c587aef17e1438d98ac015dee35fa47f4bc 100644 (file)
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.hssf.util.AreaReference;
 import org.apache.poi.hssf.util.CellReference;
 import org.apache.poi.util.BitField;
 import org.apache.poi.util.BitFieldFactory;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -113,7 +113,7 @@ public abstract class AreaPtgBase extends OperandPtg implements AreaI {
                }
        }
 
-       protected final void readCoordinates(RecordInputStream in) {
+       protected final void readCoordinates(LittleEndianInput in)  {
                field_1_first_row = in.readUShort();
                field_2_last_row = in.readUShort();
                field_3_first_column = in.readUShort();
index 21df3982a22c3105f840b81d90adbd9dbea84d16..3fcc23eda0345b53cd64663784e89364327be682 100644 (file)
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.hssf.record.UnicodeString;
 import org.apache.poi.hssf.record.constant.ConstantValueParser;
 import org.apache.poi.hssf.record.constant.ErrorConstant;
-import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -55,7 +54,7 @@ public final class ArrayPtg extends Ptg {
        private short token_2_rows;
        private Object[] token_3_arrayValues;
 
-       public ArrayPtg(RecordInputStream in) {
+       public ArrayPtg(LittleEndianInput in) {
                field_1_reserved = new byte[RESERVED_FIELD_LEN];
                // TODO - add readFully method to RecordInputStream
                for(int i=0; i< RESERVED_FIELD_LEN; i++) {
@@ -109,7 +108,7 @@ public final class ArrayPtg extends Ptg {
         * AFTER the last Ptg in the expression.
         * See page 304-305 of Excel97-2007BinaryFileFormat(xls)Specification.pdf
         */
-       public void readTokenValues(RecordInputStream in) {
+       public void readTokenValues(LittleEndianInput in) {
                int nColumns = in.readUByte();
                short nRows = in.readShort();
                //The token_1_columns and token_2_rows do not follow the documentation.
@@ -133,7 +132,7 @@ public final class ArrayPtg extends Ptg {
                if (token_3_arrayValues == null) {
                        sb.append("  #values#uninitialised#\n");
                } else {
-                       sb.append("  ").append(formatAsString());
+                       sb.append("  ").append(toFormulaString());
                }
                return sb.toString();
        }
@@ -159,11 +158,11 @@ public final class ArrayPtg extends Ptg {
                out.write(field_1_reserved);
        }
 
-       public int writeTokenValueBytes(byte[] data, int offset) {
+       public int writeTokenValueBytes(LittleEndianOutput out) {
 
-               LittleEndian.putByte(data,  offset + 0, token_1_columns-1);
-               LittleEndian.putUShort(data, offset + 1, token_2_rows-1);
-               ConstantValueParser.encode(data, offset + 3, token_3_arrayValues);
+               out.writeByte(token_1_columns-1);
+               out.writeShort(token_2_rows-1);
+               ConstantValueParser.encode(out, token_3_arrayValues);
                return 3 + ConstantValueParser.getEncodedSize(token_3_arrayValues);
        }
 
@@ -183,7 +182,7 @@ public final class ArrayPtg extends Ptg {
                        + ConstantValueParser.getEncodedSize(token_3_arrayValues);
        }
 
-       public String formatAsString() { // TODO - fold into toFormulaString
+       public String toFormulaString() {
                StringBuffer b = new StringBuffer();
                b.append("{");
                for (int y=0;y<getRowCount();y++) {
@@ -201,9 +200,6 @@ public final class ArrayPtg extends Ptg {
                b.append("}");
                return b.toString();
        }
-       public String toFormulaString() {
-               return formatAsString();
-       }
        
        private static String getConstantText(Object o) {
 
index 51d3194c0b29dcb4980d689e7bd34b54be8d57a1..7a256bf2f7bf9dc6ba852f681a09a2f8a2e2906b 100644 (file)
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.util.BitField;
 import org.apache.poi.util.BitFieldFactory;
 import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -79,7 +79,7 @@ public final class AttrPtg extends ControlPtg {
         _chooseFuncOffset = -1;
     }
 
-    public AttrPtg(RecordInputStream in)
+    public AttrPtg(LittleEndianInput in) 
     {
         field_1_options = in.readByte();
         field_2_data    = in.readShort();
index 8dfbca33fa1fdd3374ac24a15a2e474dacd6c0ad..88604ac50d96475ab136e0bae16fd6d519f00cf1 100644 (file)
@@ -17,7 +17,7 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -32,7 +32,7 @@ public final class BoolPtg extends ScalarConstantPtg {
        public final static byte sid = 0x1D;
        private final boolean _value;
 
-       public BoolPtg(RecordInputStream in) {
+       public BoolPtg(LittleEndianInput in)  {
                _value = (in.readByte() == 1);
        }
 
index 9aa07dddfce38f728a3f8089f701a53d78ef7119..0cf412d859d785fa624243aa712b5b22d24617fa 100644 (file)
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
 import org.apache.poi.ss.formula.FormulaRenderingWorkbook;
 import org.apache.poi.ss.formula.WorkbookDependentFormula;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -42,7 +42,7 @@ public final class DeletedArea3DPtg extends OperandPtg implements WorkbookDepend
                unused2 = 0;
        }
        
-       public DeletedArea3DPtg(RecordInputStream in) {
+       public DeletedArea3DPtg(LittleEndianInput in)  {
                field_1_index_extern_sheet = in.readUShort();
                unused1 = in.readInt();
                unused2 = in.readInt();
index 90ac7efac77a53fb8ab1e54fcaa60cdf5b120665..dcc07ab6d9df4ce481e3cfb3f37bf86311a0f3bd 100644 (file)
 package org.apache.poi.hssf.record.formula;
 
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
 import org.apache.poi.ss.formula.FormulaRenderingWorkbook;
 import org.apache.poi.ss.formula.WorkbookDependentFormula;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -37,7 +37,7 @@ public final class DeletedRef3DPtg extends OperandPtg implements WorkbookDepende
        private final int unused1;
 
        /** Creates new DeletedRef3DPtg */
-       public DeletedRef3DPtg(RecordInputStream in) {
+       public DeletedRef3DPtg(LittleEndianInput in)  {
                field_1_index_extern_sheet = in.readUShort();
                unused1 = in.readInt();
        }
index f2720e52d89253e3f75bd8a71bbaaf3b8c70a92a..361e4b5cf9b2f79ce1066d5ef6c10157ccbed98c 100644 (file)
@@ -17,8 +17,8 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -58,7 +58,7 @@ public final class ErrPtg extends ScalarConstantPtg {
         field_1_error_code = errorCode;
     }
 
-    public static ErrPtg read(RecordInputStream in) {
+    public static ErrPtg read(LittleEndianInput in)  {
         return valueOf(in.readByte());
     }
 
index 9a75a627bf47eecb0c8d74da5ed27600fe2e3b42..665fa97c837913373b854aae3d3bd969f98b78cf 100644 (file)
@@ -18,7 +18,7 @@
 package org.apache.poi.hssf.record.formula;
 
 import org.apache.poi.hssf.record.RecordFormatException;
-import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -33,7 +33,7 @@ public final class ExpPtg extends ControlPtg {
     private final short            field_1_first_row;
     private final short            field_2_first_col;
 
-    public ExpPtg(RecordInputStream in)
+    public ExpPtg(LittleEndianInput in) 
     {
       field_1_first_row = in.readShort();
       field_2_first_col = in.readShort();
index 3265f88b1dda657f673854a10a3e9f5177d545a3..133072203bf65b66b8d32a02bc0c32088d6dfd00 100644 (file)
@@ -16,9 +16,9 @@
 ==================================================================== */
 
 package org.apache.poi.hssf.record.formula;
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.hssf.record.formula.function.FunctionMetadata;
 import org.apache.poi.hssf.record.formula.function.FunctionMetadataRegistry;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -35,7 +35,7 @@ public final class FuncPtg extends AbstractFunctionPtg {
     /**Creates new function pointer from a byte array
      * usually called while reading an excel file.
      */
-    public FuncPtg(RecordInputStream in) {
+    public FuncPtg(LittleEndianInput in) {
         //field_1_num_args = data[ offset + 0 ];
         field_2_fnc_index  = in.readShort();
 
index 6a6ccda8aee0317d1c91e9179780259c3cca8957..1deb630d50b38fedc36956d51b612be583d58566 100644 (file)
@@ -16,9 +16,9 @@
 ==================================================================== */
 
 package org.apache.poi.hssf.record.formula;
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.hssf.record.formula.function.FunctionMetadata;
 import org.apache.poi.hssf.record.formula.function.FunctionMetadataRegistry;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -33,7 +33,7 @@ public final class FuncVarPtg extends AbstractFunctionPtg{
     /**Creates new function pointer from a byte array
      * usually called while reading an excel file.
      */
-    public FuncVarPtg(RecordInputStream in) {
+    public FuncVarPtg(LittleEndianInput in)  {
         field_1_num_args = in.readByte();
         field_2_fnc_index  = in.readShort();
         FunctionMetadata fm = FunctionMetadataRegistry.getFunctionByIndex(field_2_fnc_index);
index 4df6d6f22fd37bce253f7e1b47ec9273a032df4d..bb5d36f0075e2f6c17cd22fa897be6a7ebcbe619 100644 (file)
@@ -17,7 +17,7 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -46,7 +46,7 @@ public final class IntPtg extends ScalarConstantPtg {
        public final static byte sid = 0x1e;
        private final int field_1_value;
 
-       public IntPtg(RecordInputStream in) {
+       public IntPtg(LittleEndianInput in)  {
                this(in.readUShort());
        }
 
index 9e9c0905101bbbc5bd04b475ace3225014e1bfd6..58a836e28f7cd67d5b0a57276db8696591766a3c 100644 (file)
@@ -17,7 +17,7 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -36,7 +36,7 @@ public class MemAreaPtg extends OperandPtg {
                field_2_subex_len = subexLen;
        }
 
-       public MemAreaPtg(RecordInputStream in) {
+       public MemAreaPtg(LittleEndianInput in)  {
                field_1_reserved = in.readInt();
                field_2_subex_len = in.readShort();
        }
index dc87ec2734786464a33a8d109ff8622ef904b7a9..91027b0ab0098998bf408c09c277332da88f0b8e 100644 (file)
@@ -17,7 +17,7 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -32,7 +32,7 @@ public final class MemErrPtg extends OperandPtg {
        private int field_1_reserved;
        private short field_2_subex_len;
 
-       public MemErrPtg(RecordInputStream in) {
+       public MemErrPtg(LittleEndianInput in)  {
                field_1_reserved = in.readInt();
                field_2_subex_len = in.readShort();
        }
index 98557263c03ddb96a0ff4bd12cfa0db72c11867a..3ee893bdd795abacf6df841682556d9e5cac6676 100644 (file)
@@ -17,7 +17,7 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -32,7 +32,7 @@ public final class MemFuncPtg extends OperandPtg {
         * Creates new function pointer from a byte array usually called while
         * reading an excel file.
         */
-       public MemFuncPtg(RecordInputStream in) {
+       public MemFuncPtg(LittleEndianInput in)  {
                this(in.readUShort());
        }
 
index e44369d72d373d7ecd822b330a4c687bf36e2441..e5cd0fc9dea5faaa3792b29f02a8a2cb95ef080a 100644 (file)
@@ -17,9 +17,9 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.ss.formula.FormulaRenderingWorkbook;
 import org.apache.poi.ss.formula.WorkbookDependentFormula;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -43,7 +43,7 @@ public final class NamePtg extends OperandPtg implements WorkbookDependentFormul
 
        /** Creates new NamePtg */
 
-       public NamePtg(RecordInputStream in) {
+       public NamePtg(LittleEndianInput in)  {
                field_1_label_index = in.readShort();
                field_2_zero = in.readShort();
        }
index bf8ea135610f2e47542b002f080a312a9a341e1d..de08763c7341a05972a0a5582073bc6d08ed2965 100644 (file)
@@ -17,9 +17,9 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.ss.formula.FormulaRenderingWorkbook;
 import org.apache.poi.ss.formula.WorkbookDependentFormula;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -51,7 +51,7 @@ public final class NameXPtg extends OperandPtg implements WorkbookDependentFormu
                this(sheetRefIndex, nameIndex + 1, 0);
        }
 
-       public NameXPtg(RecordInputStream in) {
+       public NameXPtg(LittleEndianInput in)  {
                this(in.readUShort(), in.readUShort(), in.readUShort());
        }
 
index 52779e0641c75b6f65c9f623d0a298d43b06c3e3..5bd8b08453ac2f0e34d8471e28915c3dc19c3994 100644 (file)
@@ -17,7 +17,7 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -32,7 +32,7 @@ public final class NumberPtg extends ScalarConstantPtg {
        public final static byte sid = 0x1f;
        private final double field_1_value;
 
-       public NumberPtg(RecordInputStream in) {
+       public NumberPtg(LittleEndianInput in)  {
                this(in.readDouble());
        }
 
index a1a826dbbf858822cb8f634de1392a19d082f47a..771cccf5824f57443c0ab398b3536cb776c8dfc4 100644 (file)
@@ -20,8 +20,8 @@ package org.apache.poi.hssf.record.formula;
 import java.util.ArrayList;
 import java.util.List;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.util.LittleEndianByteArrayOutputStream;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -48,12 +48,12 @@ public abstract class Ptg implements Cloneable {
         * Reads <tt>size</tt> bytes of the input stream, to create an array of <tt>Ptg</tt>s.
         * Extra data (beyond <tt>size</tt>) may be read if and <tt>ArrayPtg</tt>s are present.
         */
-       public static Ptg[] readTokens(int size, RecordInputStream in) {
+       public static Ptg[] readTokens(int size, LittleEndianInput in) {
                List temp = new ArrayList(4 + size / 2);
                int pos = 0;
                List arrayPtgs = null;
                while (pos < size) {
-                       Ptg ptg = Ptg.createPtg( in );
+                       Ptg ptg = Ptg.createPtg(in);
                        if (ptg instanceof ArrayPtg) {
                                if (arrayPtgs == null) {
                                        arrayPtgs = new ArrayList(5);
@@ -77,7 +77,7 @@ public abstract class Ptg implements Cloneable {
                return toPtgArray(temp);
        }
 
-       public static Ptg createPtg(RecordInputStream in) {
+       public static Ptg createPtg(LittleEndianInput in) {
                byte id = in.readByte();
                
                if (id < 0x20) {
@@ -97,7 +97,7 @@ public abstract class Ptg implements Cloneable {
                return retval;
        }
 
-       private static Ptg createClassifiedPtg(byte id, RecordInputStream in) {
+       private static Ptg createClassifiedPtg(byte id, LittleEndianInput in) {
                
                int baseId = id & 0x1F | 0x20;
                
@@ -126,7 +126,7 @@ public abstract class Ptg implements Cloneable {
                                   Integer.toHexString(id) + " (" + ( int ) id + ")");
        }
 
-       private static Ptg createBasePtg(byte id, RecordInputStream in) {
+       private static Ptg createBasePtg(byte id, LittleEndianInput in) {
                switch(id) {
                        case 0x00:                return new UnknownPtg(id); // TODO - not a real Ptg
                        case ExpPtg.sid:          return new ExpPtg(in);          // 0x01
@@ -228,10 +228,9 @@ public abstract class Ptg implements Cloneable {
         * @return number of bytes written
         */
        public static int serializePtgs(Ptg[] ptgs, byte[] array, int offset) {
-               int pos = 0;
                int nTokens = ptgs.length;
                
-               LittleEndianOutput out = new LittleEndianByteArrayOutputStream(array, offset);
+               LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(array, offset);
 
                List arrayPtgs = null;
 
@@ -244,18 +243,15 @@ public abstract class Ptg implements Cloneable {
                                        arrayPtgs = new ArrayList(5);
                                }
                                arrayPtgs.add(ptg);
-                               pos += ArrayPtg.PLAIN_TOKEN_SIZE;
-                       } else {
-                               pos += ptg.getSize();
                        }
                }
                if (arrayPtgs != null) {
                        for (int i=0;i<arrayPtgs.size();i++) {
                                ArrayPtg p = (ArrayPtg)arrayPtgs.get(i);
-                               pos += p.writeTokenValueBytes(array, pos + offset);
+                               p.writeTokenValueBytes(out);
                        }
                }
-               return pos;
+               return out.getWriteIndex() - offset;
        }
 
        /**
index f2fcd504c7bd6d635e7abeace29a19d76b3bb4fd..3e52bc2081b4a2d0411e918b9bc65932e5db3c02 100644 (file)
@@ -17,7 +17,7 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -41,7 +41,7 @@ abstract class Ref2DPtgBase extends RefPtgBase {
                setColRelative(isColumnRelative);
        }
 
-       protected Ref2DPtgBase(RecordInputStream in) {
+       protected Ref2DPtgBase(LittleEndianInput in)  {
                readCoordinates(in);
        }
 
index ff86b8c3c8b79c94a802c1935034b4be450a0e1b..09b6725a87392cc32f6a167db745febce61ed05c 100644 (file)
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.hssf.util.CellReference;
 import org.apache.poi.ss.formula.ExternSheetReferenceToken;
 import org.apache.poi.ss.formula.FormulaRenderingWorkbook;
 import org.apache.poi.ss.formula.WorkbookDependentFormula;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -41,7 +41,7 @@ public final class Ref3DPtg extends RefPtgBase implements WorkbookDependentFormu
     /** Creates new AreaPtg */
     public Ref3DPtg() {}
 
-    public Ref3DPtg(RecordInputStream in) {
+    public Ref3DPtg(LittleEndianInput in)  {
         field_1_index_extern_sheet = in.readShort();
         readCoordinates(in);
     }
index 3cf9b340bfe80a148888a97b7baf1cb3340e9707..4cbd501053f1de5996930aabb001238514b3719c 100755 (executable)
@@ -17,8 +17,8 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -34,7 +34,7 @@ public final class RefErrorPtg extends OperandPtg {
     public RefErrorPtg() {
         field_1_reserved = 0;
     }
-    public RefErrorPtg(RecordInputStream in) {
+    public RefErrorPtg(LittleEndianInput in)  {
         field_1_reserved = in.readInt();
     }
 
index 07114265d0c1a3858d5a66addd0537feb0b0d7da..3a8148c46cc5c450e1c124574413bc00644d7d53 100644 (file)
@@ -17,7 +17,7 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.util.LittleEndianInput;
 
 /**
  * RefNPtg
@@ -26,7 +26,7 @@ import org.apache.poi.hssf.record.RecordInputStream;
 public final class RefNPtg extends Ref2DPtgBase {
        public final static byte sid = 0x2C;
 
-       public RefNPtg(RecordInputStream in) {
+       public RefNPtg(LittleEndianInput in)  {
                super(in);
        }
 
index 94a1b3301c314c225d24bcc4217551893e599c4b..69508fd7d21b1365b098422ccd3d3260d01ae953 100644 (file)
@@ -17,7 +17,7 @@
 \r
 package org.apache.poi.hssf.record.formula;\r
 \r
-import org.apache.poi.hssf.record.RecordInputStream;\r
+import org.apache.poi.util.LittleEndianInput;\r
 \r
 /**\r
  * ReferencePtg - handles references (such as A1, A2, IA4)\r
@@ -39,7 +39,7 @@ public final class RefPtg extends Ref2DPtgBase {
                super(row, column, isRowRelative, isColumnRelative);\r
        }\r
 \r
-       public RefPtg(RecordInputStream in) {\r
+       public RefPtg(LittleEndianInput in)  {\r
                super(in);\r
        }\r
 \r
index d623324a374c9db6859170bafa1e61dd6224b8f9..8357e158c511a1d2d5b5b5ce41e8e6b8ee010a1e 100644 (file)
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.hssf.util.CellReference;
 import org.apache.poi.util.BitField;
 import org.apache.poi.util.BitFieldFactory;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -67,7 +67,7 @@ public abstract class RefPtgBase extends OperandPtg {
                setColRelative(isColumnRelative);
        }
 
-       protected final void readCoordinates(RecordInputStream in) {
+       protected final void readCoordinates(LittleEndianInput in)  {
                field_1_row = in.readUShort();
                field_2_col = in.readUShort();
        }
index cd6749e5bc0f06e8162229c8ee1e4f3f89e1e275..59a5521cc2c577415f6388aa41bf0d4821ab420f 100644 (file)
@@ -17,9 +17,9 @@
 
 package org.apache.poi.hssf.record.formula;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 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;
 
@@ -47,7 +47,7 @@ public final class StringPtg extends ScalarConstantPtg {
     private final String field_3_string;
 
     /** Create a StringPtg from a stream */
-    public StringPtg(RecordInputStream in) {
+    public StringPtg(LittleEndianInput in)  {
         field_1_length = in.readUByte();
         field_2_options = in.readByte();
         if (fHighByte.isSet(field_2_options)) {
index c62783a3a094cb288a75d17e444da4802b35e03a..280bd9c010bbf8ec3e3a68d8eefc9fb8f03b58e9 100644 (file)
@@ -18,7 +18,7 @@
 package org.apache.poi.hssf.record.formula;
 
 import org.apache.poi.hssf.record.RecordFormatException;
-import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianOutput;
 
 /**
@@ -43,7 +43,7 @@ public final class TblPtg extends ControlPtg {
     /** The column number of the upper left corner */
     private final int field_2_first_col;
 
-    public TblPtg(RecordInputStream in) {
+    public TblPtg(LittleEndianInput in)  {
       field_1_first_row = in.readUShort();
       field_2_first_col = in.readUShort();
     }
index 598a9cbdbca0a1c0fbd29db419d87079519e4750..b77407c7a926b70e1480bb99705734162b279437 100644 (file)
@@ -81,4 +81,7 @@ public final class LittleEndianByteArrayOutputStream implements LittleEndianOutp
                System.arraycopy(b, 0, _buf, _writeIndex, len);
                _writeIndex += len;
        }
+       public int getWriteIndex() {
+               return _writeIndex;
+       }
 }
index e78e9b40e9962419c68d6b9e7a30042714b96120..8a57d2340b83ad476c868ff0b2b549d8df306161 100644 (file)
@@ -140,6 +140,27 @@ public class StringUtil {
                }
                return readUnicodeLE(in, nChars);
        }
+       /**
+        * OutputStream <tt>out</tt> will get:
+        * <ol>
+        * <li>ushort nChars</li>
+        * <li>byte is16BitFlag</li>
+        * <li>byte[]/char[] characterData</li>
+        * </ol>
+        * For this encoding, the is16BitFlag is always present even if nChars==0.
+        */
+       public static void writeUnicodeString(LittleEndianOutput out, String value) {
+               
+               int nChars = value.length();
+               out.writeShort(nChars);
+               boolean is16Bit = hasMultibyte(value);
+               out.writeByte(is16Bit ? 0x01 : 0x00);
+               if (is16Bit) {
+                       putUnicodeLE(value, out);
+               } else {
+                       putCompressedUnicode(value, out);
+               }
+       }
 
        /**
         * Takes a unicode (java) string, and returns it as 8 bit data (in ISO-8859-1
index 17a0901040317b1041852379748e30a42e266054..6e1a06ac5341ff9c21dcdd1a37a7b2e148eb12a2 100644 (file)
@@ -26,6 +26,7 @@ import org.apache.poi.hssf.record.TestcaseRecordInputStream;
 import org.apache.poi.hssf.record.UnicodeString;
 import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
 import org.apache.poi.util.HexRead;
+import org.apache.poi.util.LittleEndianByteArrayOutputStream;
 /**
  * 
  * @author Josh Micich
@@ -52,7 +53,8 @@ public final class TestConstantValueParser extends TestCase {
        public void testEncode() {
                int size = ConstantValueParser.getEncodedSize(SAMPLE_VALUES);
                byte[] data = new byte[size];
-               ConstantValueParser.encode(data, 0, SAMPLE_VALUES);
+               
+               ConstantValueParser.encode(new LittleEndianByteArrayOutputStream(data, 0), SAMPLE_VALUES);
                
                if (!Arrays.equals(data, SAMPLE_ENCODING)) {
                        fail("Encoding differs");
index 8d2fe631d25cb5b62ae000d7453e6d5a9dc9ffe3..16ec4444fa0f248383079a47e53e2bd763be4539 100644 (file)
@@ -24,6 +24,7 @@ import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.hssf.record.TestcaseRecordInputStream;
 import org.apache.poi.hssf.record.UnicodeString;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.util.LittleEndianByteArrayOutputStream;
 
 import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
@@ -70,7 +71,7 @@ public final class TestArrayPtg extends TestCase {
                assertEquals(new UnicodeString("FG"), values[1][2]);
                
                byte[] outBuf = new byte[ENCODED_CONSTANT_DATA.length];
-               ptg.writeTokenValueBytes(outBuf, 0);
+               ptg.writeTokenValueBytes(new LittleEndianByteArrayOutputStream(outBuf, 0));
                
                if(outBuf[0] == 4) {
                        throw new AssertionFailedError("Identified bug 42564b");