diff options
Diffstat (limited to 'src/java')
13 files changed, 296 insertions, 369 deletions
diff --git a/src/java/org/apache/poi/ddf/EscherBitmapBlip.java b/src/java/org/apache/poi/ddf/EscherBitmapBlip.java index d81eebdcc9..096e0c4a87 100644 --- a/src/java/org/apache/poi/ddf/EscherBitmapBlip.java +++ b/src/java/org/apache/poi/ddf/EscherBitmapBlip.java @@ -20,8 +20,6 @@ package org.apache.poi.ddf; import org.apache.poi.util.HexDump; import org.apache.poi.util.LittleEndian; -import java.io.ByteArrayOutputStream; - /** * @author Glen Stampoultzis */ @@ -95,17 +93,8 @@ public class EscherBitmapBlip extends EscherBlipRecord { { String nl = System.getProperty( "line.separator" ); - String extraData; - ByteArrayOutputStream b = new ByteArrayOutputStream(); - try - { - HexDump.dump( this.field_pictureData, 0, b, 0 ); - extraData = b.toString(); - } - catch ( Exception e ) - { - extraData = e.toString(); - } + String extraData = HexDump.dump(this.field_pictureData, 0, 0); + return getClass().getName() + ":" + nl + " RecordId: 0x" + HexDump.toHex( getRecordId() ) + nl + " Version: 0x" + HexDump.toHex( getVersion() ) + nl + @@ -117,17 +106,7 @@ public class EscherBitmapBlip extends EscherBlipRecord { @Override public String toXml(String tab) { - String extraData; - ByteArrayOutputStream b = new ByteArrayOutputStream(); - try - { - HexDump.dump( this.field_pictureData, 0, b, 0 ); - extraData = b.toString(); - } - catch ( Exception e ) - { - extraData = e.toString(); - } + String extraData = HexDump.dump(this.field_pictureData, 0, 0); StringBuilder builder = new StringBuilder(); builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) .append(tab).append("\t").append("<UID>0x").append(HexDump.toHex(field_1_UID)).append("</UID>\n") diff --git a/src/java/org/apache/poi/ddf/EscherClientAnchorRecord.java b/src/java/org/apache/poi/ddf/EscherClientAnchorRecord.java index ffc9170c43..665a091d7d 100644 --- a/src/java/org/apache/poi/ddf/EscherClientAnchorRecord.java +++ b/src/java/org/apache/poi/ddf/EscherClientAnchorRecord.java @@ -20,8 +20,6 @@ package org.apache.poi.ddf; import org.apache.poi.util.HexDump; import org.apache.poi.util.LittleEndian; -import java.io.ByteArrayOutputStream; - /** * The escher client anchor specifies which rows and cells the shape is bound to as well as * the offsets within those cells. Each cell is 1024 units wide by 256 units long regardless @@ -138,18 +136,7 @@ public class EscherClientAnchorRecord public String toString() { String nl = System.getProperty("line.separator"); - - String extraData; - ByteArrayOutputStream b = new ByteArrayOutputStream(); - try - { - HexDump.dump(this.remainingData, 0, b, 0); - extraData = b.toString(); - } - catch ( Exception e ) - { - extraData = "error\n"; - } + String extraData = HexDump.dump(this.remainingData, 0, 0); return getClass().getName() + ":" + nl + " RecordId: 0x" + HexDump.toHex(RECORD_ID) + nl + " Version: 0x" + HexDump.toHex(getVersion()) + nl + @@ -169,20 +156,7 @@ public class EscherClientAnchorRecord @Override public String toXml(String tab) { - String extraData; - ByteArrayOutputStream b = new ByteArrayOutputStream(); - try - { - HexDump.dump(this.remainingData, 0, b, 0); - extraData = b.toString(); - } - catch ( Exception e ) - { - extraData = "error\n"; - } - if (extraData.contains("No Data")){ - extraData = "No Data"; - } + String extraData = HexDump.dump(this.remainingData, 0, 0).trim(); StringBuilder builder = new StringBuilder(); builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) .append(tab).append("\t").append("<Flag>").append(field_1_flag).append("</Flag>\n") diff --git a/src/java/org/apache/poi/ddf/EscherClientDataRecord.java b/src/java/org/apache/poi/ddf/EscherClientDataRecord.java index 4c78e6760e..8e519c555e 100644 --- a/src/java/org/apache/poi/ddf/EscherClientDataRecord.java +++ b/src/java/org/apache/poi/ddf/EscherClientDataRecord.java @@ -21,8 +21,6 @@ package org.apache.poi.ddf; import org.apache.poi.util.HexDump; import org.apache.poi.util.LittleEndian; -import java.io.ByteArrayOutputStream; - /** * The EscherClientDataRecord is used to store client specific data about the position of a * shape within a container. @@ -78,18 +76,7 @@ public class EscherClientDataRecord public String toString() { String nl = System.getProperty("line.separator"); - - String extraData; - ByteArrayOutputStream b = new ByteArrayOutputStream(); - try - { - HexDump.dump(this.remainingData, 0, b, 0); - extraData = b.toString(); - } - catch ( Exception e ) - { - extraData = "error\n"; - } + String extraData = HexDump.dump(this.remainingData, 0, 0); return getClass().getName() + ":" + nl + " RecordId: 0x" + HexDump.toHex(RECORD_ID) + nl + " Version: 0x" + HexDump.toHex(getVersion()) + nl + @@ -101,20 +88,7 @@ public class EscherClientDataRecord @Override public String toXml(String tab) { - String extraData; - ByteArrayOutputStream b = new ByteArrayOutputStream(); - try - { - HexDump.dump(this.remainingData, 0, b, 0); - extraData = b.toString(); - } - catch ( Exception e ) - { - extraData = "error"; - } - if (extraData.contains("No Data")){ - extraData = "No Data"; - } + String extraData = HexDump.dump(this.remainingData, 0, 0).trim(); StringBuilder builder = new StringBuilder(); builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) diff --git a/src/java/org/apache/poi/ddf/EscherDump.java b/src/java/org/apache/poi/ddf/EscherDump.java index 5621806e65..4ed9519c97 100644 --- a/src/java/org/apache/poi/ddf/EscherDump.java +++ b/src/java/org/apache/poi/ddf/EscherDump.java @@ -777,6 +777,10 @@ public final class EscherDump { * A simple test stub. */ public static void main( String[] args ) { + main(args, System.out); + } + + public static void main( String[] args, PrintStream out ) { String dump = "0F 00 00 F0 89 07 00 00 00 00 06 F0 18 00 00 00 " + "05 04 00 00 02 00 00 00 05 00 00 00 01 00 00 00 " + @@ -907,7 +911,7 @@ public final class EscherDump { EscherDump dumper = new EscherDump(); // Dump the contents of scher to screen. // dumper.dumpOld( bytes.length, new ByteArrayInputStream( bytes ), System.out ); - dumper.dump(bytes, 0, bytes.length, System.out); + dumper.dump(bytes, 0, bytes.length, out); } diff --git a/src/java/org/apache/poi/hssf/dev/BiffViewer.java b/src/java/org/apache/poi/hssf/dev/BiffViewer.java index 3afd25a4ac..33fb1912bd 100644 --- a/src/java/org/apache/poi/hssf/dev/BiffViewer.java +++ b/src/java/org/apache/poi/hssf/dev/BiffViewer.java @@ -19,7 +19,6 @@ package org.apache.poi.hssf.dev; import java.io.DataInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; @@ -31,8 +30,119 @@ import java.io.Writer; import java.util.ArrayList; import java.util.List; -import org.apache.poi.hssf.record.*; +import org.apache.poi.hssf.record.ArrayRecord; +import org.apache.poi.hssf.record.AutoFilterInfoRecord; +import org.apache.poi.hssf.record.BOFRecord; +import org.apache.poi.hssf.record.BackupRecord; +import org.apache.poi.hssf.record.BlankRecord; +import org.apache.poi.hssf.record.BookBoolRecord; +import org.apache.poi.hssf.record.BoolErrRecord; +import org.apache.poi.hssf.record.BottomMarginRecord; +import org.apache.poi.hssf.record.BoundSheetRecord; +import org.apache.poi.hssf.record.CFHeader12Record; +import org.apache.poi.hssf.record.CFHeaderRecord; +import org.apache.poi.hssf.record.CFRule12Record; +import org.apache.poi.hssf.record.CFRuleRecord; +import org.apache.poi.hssf.record.CalcCountRecord; +import org.apache.poi.hssf.record.CalcModeRecord; +import org.apache.poi.hssf.record.CodepageRecord; +import org.apache.poi.hssf.record.ColumnInfoRecord; +import org.apache.poi.hssf.record.ContinueRecord; +import org.apache.poi.hssf.record.CountryRecord; +import org.apache.poi.hssf.record.DBCellRecord; +import org.apache.poi.hssf.record.DConRefRecord; +import org.apache.poi.hssf.record.DSFRecord; +import org.apache.poi.hssf.record.DVALRecord; +import org.apache.poi.hssf.record.DVRecord; +import org.apache.poi.hssf.record.DateWindow1904Record; +import org.apache.poi.hssf.record.DefaultColWidthRecord; +import org.apache.poi.hssf.record.DefaultRowHeightRecord; +import org.apache.poi.hssf.record.DeltaRecord; +import org.apache.poi.hssf.record.DimensionsRecord; +import org.apache.poi.hssf.record.DrawingGroupRecord; +import org.apache.poi.hssf.record.DrawingRecordForBiffViewer; +import org.apache.poi.hssf.record.DrawingSelectionRecord; +import org.apache.poi.hssf.record.EOFRecord; +import org.apache.poi.hssf.record.ExtSSTRecord; +import org.apache.poi.hssf.record.ExtendedFormatRecord; +import org.apache.poi.hssf.record.ExternSheetRecord; +import org.apache.poi.hssf.record.ExternalNameRecord; +import org.apache.poi.hssf.record.FeatHdrRecord; +import org.apache.poi.hssf.record.FeatRecord; +import org.apache.poi.hssf.record.FilePassRecord; +import org.apache.poi.hssf.record.FileSharingRecord; +import org.apache.poi.hssf.record.FnGroupCountRecord; +import org.apache.poi.hssf.record.FontRecord; +import org.apache.poi.hssf.record.FooterRecord; +import org.apache.poi.hssf.record.FormatRecord; +import org.apache.poi.hssf.record.FormulaRecord; +import org.apache.poi.hssf.record.GridsetRecord; +import org.apache.poi.hssf.record.GutsRecord; +import org.apache.poi.hssf.record.HCenterRecord; +import org.apache.poi.hssf.record.HeaderRecord; +import org.apache.poi.hssf.record.HideObjRecord; +import org.apache.poi.hssf.record.HorizontalPageBreakRecord; +import org.apache.poi.hssf.record.HyperlinkRecord; +import org.apache.poi.hssf.record.IndexRecord; +import org.apache.poi.hssf.record.InterfaceEndRecord; +import org.apache.poi.hssf.record.InterfaceHdrRecord; +import org.apache.poi.hssf.record.IterationRecord; +import org.apache.poi.hssf.record.LabelRecord; +import org.apache.poi.hssf.record.LabelSSTRecord; +import org.apache.poi.hssf.record.LeftMarginRecord; +import org.apache.poi.hssf.record.MMSRecord; +import org.apache.poi.hssf.record.MergeCellsRecord; +import org.apache.poi.hssf.record.MulBlankRecord; +import org.apache.poi.hssf.record.MulRKRecord; +import org.apache.poi.hssf.record.NameCommentRecord; +import org.apache.poi.hssf.record.NameRecord; +import org.apache.poi.hssf.record.NoteRecord; +import org.apache.poi.hssf.record.NumberRecord; +import org.apache.poi.hssf.record.ObjRecord; +import org.apache.poi.hssf.record.PaletteRecord; +import org.apache.poi.hssf.record.PaneRecord; +import org.apache.poi.hssf.record.PasswordRecord; +import org.apache.poi.hssf.record.PasswordRev4Record; +import org.apache.poi.hssf.record.PrecisionRecord; +import org.apache.poi.hssf.record.PrintGridlinesRecord; +import org.apache.poi.hssf.record.PrintHeadersRecord; +import org.apache.poi.hssf.record.PrintSetupRecord; +import org.apache.poi.hssf.record.ProtectRecord; +import org.apache.poi.hssf.record.ProtectionRev4Record; +import org.apache.poi.hssf.record.RKRecord; +import org.apache.poi.hssf.record.RecalcIdRecord; +import org.apache.poi.hssf.record.Record; +import org.apache.poi.hssf.record.RecordFormatException; +import org.apache.poi.hssf.record.RecordInputStream; import org.apache.poi.hssf.record.RecordInputStream.LeftoverDataException; +import org.apache.poi.hssf.record.RefModeRecord; +import org.apache.poi.hssf.record.RefreshAllRecord; +import org.apache.poi.hssf.record.RightMarginRecord; +import org.apache.poi.hssf.record.RowRecord; +import org.apache.poi.hssf.record.SCLRecord; +import org.apache.poi.hssf.record.SSTRecord; +import org.apache.poi.hssf.record.SaveRecalcRecord; +import org.apache.poi.hssf.record.SelectionRecord; +import org.apache.poi.hssf.record.SharedFormulaRecord; +import org.apache.poi.hssf.record.StringRecord; +import org.apache.poi.hssf.record.StyleRecord; +import org.apache.poi.hssf.record.SupBookRecord; +import org.apache.poi.hssf.record.TabIdRecord; +import org.apache.poi.hssf.record.TableRecord; +import org.apache.poi.hssf.record.TableStylesRecord; +import org.apache.poi.hssf.record.TextObjectRecord; +import org.apache.poi.hssf.record.TopMarginRecord; +import org.apache.poi.hssf.record.UncalcedRecord; +import org.apache.poi.hssf.record.UnknownRecord; +import org.apache.poi.hssf.record.UseSelFSRecord; +import org.apache.poi.hssf.record.VCenterRecord; +import org.apache.poi.hssf.record.VerticalPageBreakRecord; +import org.apache.poi.hssf.record.WSBoolRecord; +import org.apache.poi.hssf.record.WindowOneRecord; +import org.apache.poi.hssf.record.WindowProtectRecord; +import org.apache.poi.hssf.record.WindowTwoRecord; +import org.apache.poi.hssf.record.WriteAccessRecord; +import org.apache.poi.hssf.record.WriteProtectRecord; import org.apache.poi.hssf.record.chart.AreaFormatRecord; import org.apache.poi.hssf.record.chart.AreaRecord; import org.apache.poi.hssf.record.chart.AxisLineFormatRecord; @@ -85,13 +195,16 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.util.HexDump; import org.apache.poi.util.LittleEndian; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; /** * Utillity for reading in BIFF8 records and displaying data from them. * @see #main */ public final class BiffViewer { - static final char[] NEW_LINE_CHARS = System.getProperty("line.separator").toCharArray(); + static final String NEW_LINE_CHARS = System.getProperty("line.separator"); + private static POILogger logger = POILogFactory.getLogger(BiffViewer.class); private BiffViewer() { // no instances of this class @@ -114,8 +227,7 @@ public final class BiffViewer { try { hasNext = recStream.hasNextRecord(); } catch (LeftoverDataException e) { - e.printStackTrace(); - System.err.println("Discarding " + recStream.remaining() + " bytes and continuing"); + logger.log(POILogger.ERROR, "Discarding " + recStream.remaining() + " bytes and continuing", e); recStream.readRemainder(); hasNext = recStream.hasNextRecord(); } @@ -427,7 +539,7 @@ public final class BiffViewer { * </table> * */ - public static void main(String[] args) { + public static void main(String[] args) throws IOException { // args = new String[] { "--out", "", }; CommandArgs cmdArgs; try { @@ -437,40 +549,37 @@ public final class BiffViewer { return; } - try { - - PrintStream ps; - if (cmdArgs.shouldOutputToFile()) { - OutputStream os = new FileOutputStream(cmdArgs.getFile().getAbsolutePath() + ".out"); - ps = new PrintStream(os); - } else { - ps = System.out; - } - - InputStream is = getPOIFSInputStream(cmdArgs.getFile()); + PrintStream ps; + if (cmdArgs.shouldOutputToFile()) { + OutputStream os = new FileOutputStream(cmdArgs.getFile().getAbsolutePath() + ".out"); + ps = new PrintStream(os); + } else { + ps = System.out; + } - if (cmdArgs.shouldOutputRawHexOnly()) { - int size = is.available(); - byte[] data = new byte[size]; + NPOIFSFileSystem fs = new NPOIFSFileSystem(cmdArgs.getFile(), true); + InputStream is = getPOIFSInputStream(fs); + + if (cmdArgs.shouldOutputRawHexOnly()) { + int size = is.available(); + byte[] data = new byte[size]; - is.read(data); - HexDump.dump(data, 0, System.out, 0); - } else { - boolean dumpInterpretedRecords = cmdArgs.shouldDumpRecordInterpretations(); - boolean dumpHex = cmdArgs.shouldDumpBiffHex(); - boolean zeroAlignHexDump = dumpInterpretedRecords; // TODO - fix non-zeroAlign - runBiffViewer(ps, is, dumpInterpretedRecords, dumpHex, zeroAlignHexDump, - cmdArgs.suppressHeader()); - } - ps.close(); - } catch (Exception e) { - e.printStackTrace(); + is.read(data); + HexDump.dump(data, 0, System.out, 0); + } else { + boolean dumpInterpretedRecords = cmdArgs.shouldDumpRecordInterpretations(); + boolean dumpHex = cmdArgs.shouldDumpBiffHex(); + boolean zeroAlignHexDump = dumpInterpretedRecords; // TODO - fix non-zeroAlign + runBiffViewer(ps, is, dumpInterpretedRecords, dumpHex, zeroAlignHexDump, + cmdArgs.suppressHeader()); } + is.close(); + fs.close(); + ps.close(); } - protected static InputStream getPOIFSInputStream(File file) + protected static InputStream getPOIFSInputStream(NPOIFSFileSystem fs) throws IOException, FileNotFoundException { - NPOIFSFileSystem fs = new NPOIFSFileSystem(new FileInputStream(file)); String workbookName = HSSFWorkbook.getWorkbookDirEntryName(fs.getRoot()); return fs.createDocumentInputStream(workbookName); } diff --git a/src/java/org/apache/poi/hssf/dev/EFBiffViewer.java b/src/java/org/apache/poi/hssf/dev/EFBiffViewer.java index 2893f31ed9..c87f5f73b9 100644 --- a/src/java/org/apache/poi/hssf/dev/EFBiffViewer.java +++ b/src/java/org/apache/poi/hssf/dev/EFBiffViewer.java @@ -25,6 +25,7 @@ import org.apache.poi.hssf.eventusermodel.HSSFEventFactory; import org.apache.poi.hssf.eventusermodel.HSSFListener; import org.apache.poi.hssf.eventusermodel.HSSFRequest; import org.apache.poi.hssf.record.Record; +import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; /** * @@ -42,7 +43,8 @@ public class EFBiffViewer } public void run() throws IOException { - InputStream din = BiffViewer.getPOIFSInputStream(new File(file)); + NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(file), true); + InputStream din = BiffViewer.getPOIFSInputStream(fs); HSSFRequest req = new HSSFRequest(); req.addListenerForAllRecords(new HSSFListener() @@ -55,6 +57,8 @@ public class EFBiffViewer HSSFEventFactory factory = new HSSFEventFactory(); factory.processEvents(req, din); + din.close(); + fs.close(); } public void setFile(String file) diff --git a/src/java/org/apache/poi/hssf/dev/FormulaViewer.java b/src/java/org/apache/poi/hssf/dev/FormulaViewer.java index 69e0c3005b..eaa3fff0b5 100644 --- a/src/java/org/apache/poi/hssf/dev/FormulaViewer.java +++ b/src/java/org/apache/poi/hssf/dev/FormulaViewer.java @@ -18,6 +18,7 @@ package org.apache.poi.hssf.dev; import java.io.File; +import java.io.InputStream; import java.util.List; import org.apache.poi.hssf.model.HSSFFormulaParser; @@ -25,6 +26,7 @@ import org.apache.poi.hssf.record.FormulaRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.RecordFactory; import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.ss.formula.ptg.ExpPtg; import org.apache.poi.ss.formula.ptg.FuncPtg; import org.apache.poi.ss.formula.ptg.OperationPtg; @@ -56,12 +58,10 @@ public class FormulaViewer * */ - public void run() - throws Exception - { - List<Record> records = - RecordFactory - .createRecords(BiffViewer.getPOIFSInputStream(new File(file))); + public void run() throws Exception { + NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(file), true); + InputStream is = BiffViewer.getPOIFSInputStream(fs); + List<Record> records = RecordFactory.createRecords(is); for (int k = 0; k < records.size(); k++) { @@ -76,6 +76,8 @@ public class FormulaViewer } } } + is.close(); + fs.close(); } private void listFormula(FormulaRecord record) { diff --git a/src/java/org/apache/poi/hssf/dev/RecordLister.java b/src/java/org/apache/poi/hssf/dev/RecordLister.java index 28896f2a11..7c626b8a12 100644 --- a/src/java/org/apache/poi/hssf/dev/RecordLister.java +++ b/src/java/org/apache/poi/hssf/dev/RecordLister.java @@ -25,6 +25,7 @@ import org.apache.poi.hssf.record.ContinueRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.RecordFactory; import org.apache.poi.hssf.record.RecordInputStream; +import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; /** * This is a low-level debugging class, which simply prints @@ -49,7 +50,8 @@ public class RecordLister public void run() throws IOException { - InputStream din = BiffViewer.getPOIFSInputStream(new File(file)); + NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(file), true); + InputStream din = BiffViewer.getPOIFSInputStream(fs); RecordInputStream rinp = new RecordInputStream(din); while(rinp.hasNextRecord()) { @@ -77,6 +79,9 @@ public class RecordLister System.out.println( formatData(data) ); } } + + din.close(); + fs.close(); } private static String formatSID(int sid) { diff --git a/src/java/org/apache/poi/hssf/record/HyperlinkRecord.java b/src/java/org/apache/poi/hssf/record/HyperlinkRecord.java index 618a86b062..07466f06e3 100644 --- a/src/java/org/apache/poi/hssf/record/HyperlinkRecord.java +++ b/src/java/org/apache/poi/hssf/record/HyperlinkRecord.java @@ -124,16 +124,16 @@ public final class HyperlinkRecord extends StandardRecord { StringBuilder sb = new StringBuilder(36); int PREFIX_LEN = "0x".length(); - sb.append(HexDump.intToHex(_d1), PREFIX_LEN, 8); + sb.append(HexDump.intToHex(_d1).substring(PREFIX_LEN)); sb.append("-"); - sb.append(HexDump.shortToHex(_d2), PREFIX_LEN, 4); + sb.append(HexDump.shortToHex(_d2).substring(PREFIX_LEN)); sb.append("-"); - sb.append(HexDump.shortToHex(_d3), PREFIX_LEN, 4); + sb.append(HexDump.shortToHex(_d3).substring(PREFIX_LEN)); sb.append("-"); - char[] d4Chars = HexDump.longToHex(getD4()); - sb.append(d4Chars, PREFIX_LEN, 4); + String d4Chars = HexDump.longToHex(getD4()); + sb.append(d4Chars.substring(PREFIX_LEN, PREFIX_LEN+4)); sb.append("-"); - sb.append(d4Chars, PREFIX_LEN + 4, 12); + sb.append(d4Chars.substring(PREFIX_LEN+4)); return sb.toString(); } @@ -643,14 +643,14 @@ public final class HyperlinkRecord extends StandardRecord { private static byte[] readTail(byte[] expectedTail, LittleEndianInput in) { byte[] result = new byte[TAIL_SIZE]; in.readFully(result); - if (false) { // Quite a few examples in the unit tests which don't have the exact expected tail - for (int i = 0; i < expectedTail.length; i++) { - if (expectedTail[i] != result[i]) { - logger.log( POILogger.ERROR, "Mismatch in tail byte [" + i + "]" - + "expected " + (expectedTail[i] & 0xFF) + " but got " + (result[i] & 0xFF)); - } - } - } +// if (false) { // Quite a few examples in the unit tests which don't have the exact expected tail +// for (int i = 0; i < expectedTail.length; i++) { +// if (expectedTail[i] != result[i]) { +// logger.log( POILogger.ERROR, "Mismatch in tail byte [" + i + "]" +// + "expected " + (expectedTail[i] & 0xFF) + " but got " + (result[i] & 0xFF)); +// } +// } +// } return result; } private static void writeTail(byte[] tail, LittleEndianOutput out) { diff --git a/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java b/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java index c94e427b92..7e7ca0780c 100644 --- a/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java +++ b/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java @@ -18,7 +18,6 @@ package org.apache.poi.poifs.filesystem; import java.io.BufferedInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -211,31 +210,22 @@ public final class NPOIFSDocument implements POIFSViewable { * @return an array of Object; may not be null, but may be empty */ public Object[] getViewableArray() { - Object[] results = new Object[1]; - String result; + String result = "<NO DATA>"; - try { - if(getSize() > 0) { - // Get all the data into a single array - byte[] data = new byte[getSize()]; - int offset = 0; - for(ByteBuffer buffer : _stream) { - int length = Math.min(_block_size, data.length-offset); - buffer.get(data, offset, length); - offset += length; - } - - ByteArrayOutputStream output = new ByteArrayOutputStream(); - HexDump.dump(data, 0, output, 0); - result = output.toString(); - } else { - result = "<NO DATA>"; + if(getSize() > 0) { + // Get all the data into a single array + byte[] data = new byte[getSize()]; + int offset = 0; + for(ByteBuffer buffer : _stream) { + int length = Math.min(_block_size, data.length-offset); + buffer.get(data, offset, length); + offset += length; } - } catch (IOException e) { - result = e.getMessage(); + + result = HexDump.dump(data, 0, 0); } - results[0] = result; - return results; + + return new String[]{ result }; } /** diff --git a/src/java/org/apache/poi/poifs/filesystem/OPOIFSDocument.java b/src/java/org/apache/poi/poifs/filesystem/OPOIFSDocument.java index 05b21baeaa..a7479f3194 100644 --- a/src/java/org/apache/poi/poifs/filesystem/OPOIFSDocument.java +++ b/src/java/org/apache/poi/poifs/filesystem/OPOIFSDocument.java @@ -332,11 +332,9 @@ public final class OPOIFSDocument implements BATManaged, BlockWritable, POIFSVie * @return an array of Object; may not be null, but may be empty */ public Object[] getViewableArray() { - Object[] results = new Object[1]; - String result; + String result = "<NO DATA>"; try { - ByteArrayOutputStream output = new ByteArrayOutputStream(); BlockWritable[] blocks = null; if (_big_store.isValid()) { @@ -345,28 +343,17 @@ public final class OPOIFSDocument implements BATManaged, BlockWritable, POIFSVie blocks = _small_store.getBlocks(); } if (blocks != null) { - for (int k = 0; k < blocks.length; k++) { - blocks[k].writeBlocks(output); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + for (BlockWritable bw : blocks) { + bw.writeBlocks(output); } - byte[] data = output.toByteArray(); - - if (data.length > _property.getSize()) { - byte[] tmp = new byte[_property.getSize()]; - - System.arraycopy(data, 0, tmp, 0, tmp.length); - data = tmp; - } - output = new ByteArrayOutputStream(); - HexDump.dump(data, 0, output, 0); - result = output.toString(); - } else { - result = "<NO DATA>"; + int length = Math.min(output.size(), _property.getSize()); + result = HexDump.dump(output.toByteArray(), 0, 0, length); } } catch (IOException e) { result = e.getMessage(); } - results[0] = result; - return results; + return new String[]{ result }; } /** diff --git a/src/java/org/apache/poi/poifs/storage/HeaderBlock.java b/src/java/org/apache/poi/poifs/storage/HeaderBlock.java index 06e9c4a1b9..49e491c5ad 100644 --- a/src/java/org/apache/poi/poifs/storage/HeaderBlock.java +++ b/src/java/org/apache/poi/poifs/storage/HeaderBlock.java @@ -34,17 +34,12 @@ import org.apache.poi.util.IntegerField; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndianConsts; import org.apache.poi.util.LongField; -import org.apache.poi.util.POILogFactory; -import org.apache.poi.util.POILogger; import org.apache.poi.util.ShortField; /** * The block containing the archive header */ public final class HeaderBlock implements HeaderBlockConstants { - private static final POILogger _logger = - POILogFactory.getLogger(HeaderBlock.class); - /** * What big block size the file uses. Most files * use 512 bytes, but a few use 4096 @@ -165,8 +160,8 @@ public final class HeaderBlock implements HeaderBlockConstants { // Give a generic error if the OLE2 signature isn't found throw new NotOLE2FileException("Invalid header signature; read " - + longToHex(signature) + ", expected " - + longToHex(_signature) + " - Your file appears " + + HexDump.longToHex(signature) + ", expected " + + HexDump.longToHex(_signature) + " - Your file appears " + "not to be a valid OLE2 document"); } @@ -237,10 +232,6 @@ public final class HeaderBlock implements HeaderBlockConstants { return data; } - private static String longToHex(long value) { - return new String(HexDump.longToHex(value)); - } - private static IOException alertShortRead(int pRead, int expectedReadSize) { int read; if (pRead < 0) { diff --git a/src/java/org/apache/poi/util/HexDump.java b/src/java/org/apache/poi/util/HexDump.java index b874e6c42a..7fd9d11e12 100644 --- a/src/java/org/apache/poi/util/HexDump.java +++ b/src/java/org/apache/poi/util/HexDump.java @@ -24,6 +24,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.io.PrintStream; import java.nio.charset.Charset; import java.text.DecimalFormat; @@ -31,20 +32,12 @@ import java.text.DecimalFormat; import org.apache.commons.codec.CharEncoding; /** - * dump data in hexadecimal format; derived from a HexDump utility I - * wrote in June 2001. - * - * @author Marc Johnson - * @author Glen Stampoultzis (glens at apache.org) + * dump data in hexadecimal format */ +@Internal public class HexDump { public static final String EOL = System.getProperty("line.separator"); - private static final Charset UTF8 = Charset.forName(CharEncoding.UTF_8); - private static final char _hexcodes[] = "0123456789ABCDEF".toCharArray(); - private static final int _shifts[] = - { - 60, 56, 52, 48, 44, 40, 36, 32, 28, 24, 20, 16, 12, 8, 4, 0 - }; + public static final Charset UTF8 = Charset.forName(CharEncoding.UTF_8); private HexDump() { // all static methods, so no need for a public constructor @@ -68,73 +61,15 @@ public class HexDump { * null */ public static void dump(final byte [] data, final long offset, - final OutputStream stream, final int index, final int length) - throws IOException, ArrayIndexOutOfBoundsException, - IllegalArgumentException - { - if (data.length == 0) - { - stream.write( ("No Data" + EOL).getBytes(UTF8) ); - stream.flush(); - return; - } - if ((index < 0) || (index >= data.length)) - { - throw new ArrayIndexOutOfBoundsException( - "illegal index: " + index + " into array of length " - + data.length); - } - if (stream == null) - { + final OutputStream stream, final int index, final int length) + throws IOException, ArrayIndexOutOfBoundsException, IllegalArgumentException { + if (stream == null) { throw new IllegalArgumentException("cannot write to nullstream"); } - long display_offset = offset + index; - StringBuffer buffer = new StringBuffer(74); - - - int data_length = Math.min(data.length,index+length); - for (int j = index; j < data_length; j += 16) - { - int chars_read = data_length - j; - - if (chars_read > 16) - { - chars_read = 16; - } - buffer.append( - dump(display_offset) - ).append(' '); - for (int k = 0; k < 16; k++) - { - if (k < chars_read) - { - buffer.append(dump(data[ k + j ])); - } - else - { - buffer.append(" "); - } - buffer.append(' '); - } - for (int k = 0; k < chars_read; k++) - { - if ((data[ k + j ] >= ' ') && (data[ k + j ] < 127)) - { - buffer.append(( char ) data[ k + j ]); - } - else - { - buffer.append('.'); - } - } - buffer.append(EOL); - stream.write(buffer.toString().getBytes(UTF8)); - stream.flush(); - buffer.setLength(0); - display_offset += chars_read; - } - + OutputStreamWriter osw = new OutputStreamWriter(stream, UTF8); + osw.write(dump(data, offset, index, length)); + osw.flush(); } /** @@ -155,11 +90,9 @@ public class HexDump { */ public synchronized static void dump(final byte [] data, final long offset, - final OutputStream stream, final int index) - throws IOException, ArrayIndexOutOfBoundsException, - IllegalArgumentException - { - dump(data, offset, stream, index, data.length-index); + final OutputStream stream, final int index) + throws IOException, ArrayIndexOutOfBoundsException, IllegalArgumentException { + dump(data, offset, stream, index, Integer.MAX_VALUE); } /** @@ -175,40 +108,57 @@ public class HexDump { */ public static String dump(final byte [] data, final long offset, final int index) { - if ((index < 0) || (index > data.length)) - { - throw new ArrayIndexOutOfBoundsException( - "illegal index: " + index + " into array of length " - + data.length); + return dump(data, offset, index, Integer.MAX_VALUE); + } + + /** + * dump an array of bytes to a String + * + * @param data the byte array to be dumped + * @param offset its offset, whatever that might mean + * @param index initial index into the byte array + * @param length number of characters to output + * + * @exception ArrayIndexOutOfBoundsException if the index is + * outside the data array's bounds + * @return output string + */ + + public static String dump(final byte [] data, final long offset, final int index, final int length) { + if (data == null || data.length == 0) { + return "No Data"+EOL; } + + int data_length = (length == Integer.MAX_VALUE || length < 0 || index+length < 0) + ? data.length + : Math.min(data.length,index+length); + + + if ((index < 0) || (index >= data.length)) { + String err = "illegal index: "+index+" into array of length "+data.length; + throw new ArrayIndexOutOfBoundsException(err); + } + long display_offset = offset + index; StringBuilder buffer = new StringBuilder(74); - - for (int j = index; j <= data.length; j += 16) { - int chars_read = data.length - j; + + for (int j = index; j < data_length; j += 16) { + int chars_read = data_length - j; if (chars_read > 16) { chars_read = 16; } - buffer.append(dump(display_offset)).append(' '); + buffer.append(String.format("%08X ", display_offset)); for (int k = 0; k < 16; k++) { - String hexDmp = (k < chars_read) ? dump(data[ k + j ]) : " "; - buffer.append(hexDmp); - buffer.append(' '); + if (k < chars_read) { + buffer.append(String.format("%02X ", data[ k + j ])); + } else { + buffer.append(" "); + } } for (int k = 0; k < chars_read; k++) { - byte dataB = data[ k + j ]; - char charB = (char)(dataB & 0xFF); - switch (charB) { - case 127: case 128: case 129: case 141: case 142: case 143: case 144: case 157: case 158: - charB = '.'; - break; - default: - if (charB < ' ') charB = '.'; - break; - } - buffer.append(charB); + buffer.append(toAscii(data[ k + j ])); } buffer.append(EOL); display_offset += chars_read; @@ -216,29 +166,18 @@ public class HexDump { return buffer.toString(); } - - private static String dump(final long value) - { - StringBuffer buf = new StringBuffer(); - buf.setLength(0); - for (int j = 0; j < 8; j++) - { - buf.append( _hexcodes[ (( int ) (value >> _shifts[ j + _shifts.length - 8 ])) & 15 ]); - } - return buf.toString(); - } - - private static String dump(final byte value) - { - StringBuffer buf = new StringBuffer(); - buf.setLength(0); - for (int j = 0; j < 2; j++) - { - buf.append(_hexcodes[ (value >> _shifts[ j + 6 ]) & 15 ]); + public static char toAscii(int dataB) { + char charB = (char)(dataB & 0xFF); + if (Character.isISOControl(charB)) return '.'; + + switch (charB) { + case 0xFF: case 0xDD: // printable, but not compilable with current compiler encoding + charB = '.'; + break; } - return buf.toString(); + return charB; } - + /** * Converts the parameter to a hex value. * @@ -326,9 +265,8 @@ public class HexDump { * @param value The value to convert * @return The result right padded with 0 */ - public static String toHex(final short value) - { - return toHex(value, 4); + public static String toHex(final short value) { + return String.format("%04X", value); } /** @@ -337,9 +275,8 @@ public class HexDump { * @param value The value to convert * @return The result right padded with 0 */ - public static String toHex(final byte value) - { - return toHex(value, 2); + public static String toHex(final byte value) { + return String.format("%02X", value); } /** @@ -348,9 +285,8 @@ public class HexDump { * @param value The value to convert * @return The result right padded with 0 */ - public static String toHex(final int value) - { - return toHex(value, 8); + public static String toHex(final int value) { + return String.format("%08X", value); } /** @@ -359,20 +295,8 @@ public class HexDump { * @param value The value to convert * @return The result right padded with 0 */ - public static String toHex(final long value) - { - return toHex(value, 16); - } - - - private static String toHex(final long value, final int digits) - { - StringBuffer result = new StringBuffer(digits); - for (int j = 0; j < digits; j++) - { - result.append( _hexcodes[ (int) ((value >> _shifts[ j + (16 - digits) ]) & 15)]); - } - return result.toString(); + public static String toHex(final long value) { + return String.format("%016X", value); } /** @@ -411,49 +335,33 @@ public class HexDump { byte[] data = buf.toByteArray(); dump(data, 0, out, start, data.length); } - /** - * @return char array of uppercase hex chars, zero padded and prefixed with '0x' - */ - private static char[] toHexChars(long pValue, int nBytes) { - int charPos = 2 + nBytes*2; - // The return type is char array because most callers will probably append the value to a - // StringBuffer, or write it to a Stream / Writer so there is no need to create a String; - char[] result = new char[charPos]; - long value = pValue; - do { - result[--charPos] = _hexcodes[(int) (value & 0x0F)]; - value >>>= 4; - } while (charPos > 1); - - // Prefix added to avoid ambiguity - result[0] = '0'; - result[1] = 'x'; - return result; - } /** - * @return char array of 4 (zero padded) uppercase hex chars and prefixed with '0x' + * @return string of 16 (zero padded) uppercase hex chars and prefixed with '0x' */ - public static char[] longToHex(long value) { - return toHexChars(value, 8); + public static String longToHex(long value) { + return String.format("0x%016X", value); } + /** - * @return char array of 4 (zero padded) uppercase hex chars and prefixed with '0x' + * @return string of 8 (zero padded) uppercase hex chars and prefixed with '0x' */ - public static char[] intToHex(int value) { - return toHexChars(value, 4); + public static String intToHex(int value) { + return String.format("0x%08X", value & 0xFFFFFFFF); } + /** - * @return char array of 2 (zero padded) uppercase hex chars and prefixed with '0x' + * @return string of 4 (zero padded) uppercase hex chars and prefixed with '0x' */ - public static char[] shortToHex(int value) { - return toHexChars(value, 2); + public static String shortToHex(int value) { + return String.format("0x%04X", value & 0xFFFF); } + /** - * @return char array of 1 (zero padded) uppercase hex chars and prefixed with '0x' + * @return string of 2 (zero padded) uppercase hex chars and prefixed with '0x' */ - public static char[] byteToHex(int value) { - return toHexChars(value, 1); + public static String byteToHex(int value) { + return String.format("0x%02X", value & 0xFF); } public static void main(String[] args) throws Exception { |