Fixes to chart handling and to a hidden bug to do with where the beginning of the sheet is located. Also fixed an issue with doubles and the NaN values. Some of this could use some more refactoring.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352827 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Glen Stampoultzis 2002-08-21 11:56:49 +00:00
parent ef21669380
commit f42b7bbdbb
10 changed files with 151 additions and 104 deletions

View File

@ -78,7 +78,7 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
* @see org.apache.poi.hssf.dev.EFHSSF * @see org.apache.poi.hssf.dev.EFHSSF
* *
* @author Andrew C. Oliver (acoliver at apache dot org) * @author Andrew C. Oliver (acoliver at apache dot org)
* @authro Carey Sublette (careysub@earthling.net) * @author Carey Sublette (careysub@earthling.net)
*/ */
public class HSSFEventFactory public class HSSFEventFactory
@ -103,7 +103,7 @@ public class HSSFEventFactory
processEvents(req, in); processEvents(req, in);
} }
/** /**
* Processes a file into essentially record events. * Processes a file into essentially record events.
* *
@ -111,7 +111,7 @@ public class HSSFEventFactory
* @param fs a POIFS filesystem containing your workbook * @param fs a POIFS filesystem containing your workbook
* @return numeric user-specified result code. * @return numeric user-specified result code.
*/ */
public short abortableProcessWorkbookEvents(HSSFRequest req, POIFSFileSystem fs) public short abortableProcessWorkbookEvents(HSSFRequest req, POIFSFileSystem fs)
throws IOException, HSSFUserException throws IOException, HSSFUserException
{ {
@ -121,9 +121,9 @@ public class HSSFEventFactory
/** /**
* Processes a DocumentInputStream into essentially Record events. * Processes a DocumentInputStream into essentially Record events.
* *
* If an <code>AbortableHSSFListener</code> causes a halt to processing during this call * If an <code>AbortableHSSFListener</code> causes a halt to processing during this call
* the method will return just as with <code>abortableProcessEvents</code>, but no * the method will return just as with <code>abortableProcessEvents</code>, but no
* user code or <code>HSSFUserException</code> will be passed back. * user code or <code>HSSFUserException</code> will be passed back.
* *
* @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String) * @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
@ -133,15 +133,15 @@ public class HSSFEventFactory
public void processEvents(HSSFRequest req, InputStream in) public void processEvents(HSSFRequest req, InputStream in)
throws IOException throws IOException
{ {
try try
{ {
genericProcessEvents(req, in); genericProcessEvents(req, in);
} }
catch (HSSFUserException hue) catch (HSSFUserException hue)
{/*If an HSSFUserException user exception is thrown, ignore it.*/ } {/*If an HSSFUserException user exception is thrown, ignore it.*/ }
} }
/** /**
* Processes a DocumentInputStream into essentially Record events. * Processes a DocumentInputStream into essentially Record events.
@ -153,11 +153,11 @@ public class HSSFEventFactory
*/ */
public short abortableProcessEvents(HSSFRequest req, InputStream in) public short abortableProcessEvents(HSSFRequest req, InputStream in)
throws IOException, HSSFUserException throws IOException, HSSFUserException
{ {
return genericProcessEvents(req, in); return genericProcessEvents(req, in);
} }
/** /**
* Processes a DocumentInputStream into essentially Record events. * Processes a DocumentInputStream into essentially Record events.
* *

View File

@ -59,12 +59,10 @@ import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.hssf import org.apache.poi.hssf
.record.*; // normally I don't do this, buy we literally mean ALL .record.*; // normally I don't do this, buy we literally mean ALL
import org.apache.poi.hssf.record.formula.Ptg; import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.util.IntList; import org.apache.poi.util.*;
import org.apache.poi.util.POILogger;
import org.apache.poi.hssf.record import org.apache.poi.hssf.record
.aggregates.*; // normally I don't do this, buy we literally mean ALL .aggregates.*; // normally I don't do this, buy we literally mean ALL
@ -91,35 +89,33 @@ import org.apache.poi.hssf.record
public class Sheet public class Sheet
extends java.lang.Object extends java.lang.Object
{ {
public static final short LeftMargin = 0; public static final short LeftMargin = 0;
public static final short RightMargin = 1; public static final short RightMargin = 1;
public static final short TopMargin = 2; public static final short TopMargin = 2;
public static final short BottomMargin = 3; public static final short BottomMargin = 3;
protected ArrayList records = null; protected ArrayList records = null;
int preoffset = 0; // offset of the sheet in a new file int preoffset = 0; // offset of the sheet in a new file
int loc = 0; int loc = 0;
protected boolean containsLabels = false; protected boolean containsLabels = false;
; protected int dimsloc = 0;
protected int dimsloc = 0; protected DimensionsRecord dims;
protected DimensionsRecord dims; protected DefaultColWidthRecord defaultcolwidth = null;
protected DefaultColWidthRecord defaultcolwidth = null; protected DefaultRowHeightRecord defaultrowheight = null;
protected DefaultRowHeightRecord defaultrowheight = null; protected GridsetRecord gridset = null;
protected GridsetRecord gridset = null; protected PrintSetupRecord printSetup = null;
protected PrintSetupRecord printSetup = null; protected HeaderRecord header = null;
protected HeaderRecord header = null; protected FooterRecord footer = null;
protected FooterRecord footer = null; protected PrintGridlinesRecord printGridlines = null;
protected PrintGridlinesRecord printGridlines = null; protected MergeCellsRecord merged = null;
protected MergeCellsRecord merged = null; protected int mergedloc = 0;
protected int mergedloc = 0; private static POILogger log = POILogFactory.getLogger(Sheet.class);
private static POILogger log = private ArrayList columnSizes = null; // holds column info
POILogFactory.getLogger(Sheet.class); protected ValueRecordsAggregate cells = null;
private ArrayList columnSizes = protected RowRecordsAggregate rows = null;
null; // holds column info private Iterator valueRecIterator = null;
protected ValueRecordsAggregate cells = null; private Iterator rowRecIterator = null;
protected RowRecordsAggregate rows = null; protected int eofLoc = 0;
private Iterator valueRecIterator = null;
private Iterator rowRecIterator = null;
/** /**
* Creates new Sheet with no intialization --useless at this point * Creates new Sheet with no intialization --useless at this point
@ -164,19 +160,23 @@ public class Sheet
if (rec.getSid() == LabelRecord.sid) if (rec.getSid() == LabelRecord.sid)
{ {
log.log(log.DEBUG, "Hit label record"); log.log(log.DEBUG, "Hit label record.");
retval.containsLabels = true; retval.containsLabels = true;
} }
else if (rec.getSid() == BOFRecord.sid) else if (rec.getSid() == BOFRecord.sid)
{ {
bofEofNestingLevel++; bofEofNestingLevel++;
log.log(log.DEBUG, "Hit BOF record. Nesting increased to " + bofEofNestingLevel);
} }
else if ((rec.getSid() == EOFRecord.sid) else if (rec.getSid() == EOFRecord.sid)
&& (--bofEofNestingLevel == 0))
{ {
log.log(log.DEBUG, "Hit EOF record at "); --bofEofNestingLevel;
records.add(rec); log.log(log.DEBUG, "Hit EOF record. Nesting decreased to " + bofEofNestingLevel);
break; if (bofEofNestingLevel == 0) {
records.add(rec);
retval.eofLoc = k;
break;
}
} }
else if (rec.getSid() == DimensionsRecord.sid) else if (rec.getSid() == DimensionsRecord.sid)
{ {
@ -204,7 +204,7 @@ public class Sheet
{ {
retval.defaultrowheight = ( DefaultRowHeightRecord ) rec; retval.defaultrowheight = ( DefaultRowHeightRecord ) rec;
} }
else if ( rec.isValue() ) else if ( rec.isValue() && bofEofNestingLevel == 1 )
{ {
if ( isfirstcell ) if ( isfirstcell )
{ {
@ -218,6 +218,10 @@ public class Sheet
rec = null; rec = null;
} }
} }
else if ( rec.getSid() == StringRecord.sid )
{
rec = null;
}
else if ( rec.getSid() == RowRecord.sid ) else if ( rec.getSid() == RowRecord.sid )
{ {
if ( isfirstrow ) if ( isfirstrow )
@ -255,6 +259,14 @@ public class Sheet
} }
} }
retval.records = records; retval.records = records;
if (retval.rows == null)
{
retval.rows = new RowRecordsAggregate();
}
if (retval.cells == null)
{
retval.cells = new ValueRecordsAggregate();
}
log.log(log.DEBUG, "sheet createSheet (existing file) exited"); log.log(log.DEBUG, "sheet createSheet (existing file) exited");
return retval; return retval;
} }
@ -421,7 +433,7 @@ public class Sheet
newrec.setColumn(oldrec.getColumn()); newrec.setColumn(oldrec.getColumn());
newrec.setXFIndex(oldrec.getXFIndex()); newrec.setXFIndex(oldrec.getXFIndex());
newrec.setSSTIndex(stringid); newrec.setSSTIndex(stringid);
records.add(k, newrec); records.add(k, newrec);
} }
} }
} }
@ -608,11 +620,19 @@ public class Sheet
// } // }
for (int k = 0; k < records.size(); k++) for (int k = 0; k < records.size(); k++)
{ {
// byte[] rec = (( byte [] ) bytes.get(k));
// byte[] rec = (( byte [] ) bytes.get(k));
// System.arraycopy(rec, 0, data, offset + pos, rec.length); // System.arraycopy(rec, 0, data, offset + pos, rec.length);
pos += (( Record ) records.get(k)).serialize(pos + offset, Record record = (( Record ) records.get(k));
data); // rec.length;
//uncomment to test record sizes
// byte[] data2 = new byte[record.getRecordSize()];
// record.serialize(0, data2 ); // rec.length;
// if (LittleEndian.getUShort(data2, 2) != record.getRecordSize() - 4
// && record instanceof RowRecordsAggregate == false && record instanceof ValueRecordsAggregate == false)
// throw new RuntimeException("Blah!!!");
pos += record.serialize(pos + offset, data ); // rec.length;
} }
log.log(log.DEBUG, "Sheet.serialize returning "); log.log(log.DEBUG, "Sheet.serialize returning ");
return pos; return pos;
@ -2116,4 +2136,10 @@ public class Sheet
} }
m.setMargin(size); m.setMargin(size);
} }
public int getEofLoc()
{
return eofLoc;
}
} }

View File

@ -57,8 +57,6 @@ package org.apache.poi.hssf.record;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import java.util.ArrayList;
/** /**
* Title: DBCell Record (Currently read only. Not required.) * Title: DBCell Record (Currently read only. Not required.)
* Description: Used to find rows in blocks...TODO<P> * Description: Used to find rows in blocks...TODO<P>
@ -130,7 +128,7 @@ public class DBCellRecord
* sets offset from the start of this DBCellRecord to the start of the first cell in * sets offset from the start of this DBCellRecord to the start of the first cell in
* the next DBCell block. * the next DBCell block.
* *
* @param rowoffset to the start of the first cell in the next DBCell block * @param offset offset to the start of the first cell in the next DBCell block
*/ */
public void setRowOffset(int offset) public void setRowOffset(int offset)

View File

@ -370,7 +370,7 @@ public class FormulaRecord
LittleEndian.putShort(data, 4 + offset, ( short ) getRow()); LittleEndian.putShort(data, 4 + offset, ( short ) getRow());
LittleEndian.putShort(data, 6 + offset, getColumn()); LittleEndian.putShort(data, 6 + offset, getColumn());
LittleEndian.putShort(data, 8 + offset, getXFIndex()); LittleEndian.putShort(data, 8 + offset, getXFIndex());
LittleEndian.putDouble(data, 10 + offset, getValue()); LittleEndian.putDouble(data, 10 + offset, field_4_value);
LittleEndian.putShort(data, 18 + offset, getOptions()); LittleEndian.putShort(data, 18 + offset, getOptions());
LittleEndian.putInt(data, 20 + offset, field_6_zero); LittleEndian.putInt(data, 20 + offset, field_6_zero);
LittleEndian.putShort(data, 24 + offset, getExpressionLength()); LittleEndian.putShort(data, 24 + offset, getExpressionLength());

View File

@ -55,9 +55,7 @@
package org.apache.poi.hssf.record.aggregates; package org.apache.poi.hssf.record.aggregates;
import org.apache.poi.hssf.record.CellValueRecordInterface; import org.apache.poi.hssf.record.*;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.UnknownRecord;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -100,7 +98,8 @@ public class ValueRecordsAggregate
}*/ }*/
// XYLocator xy = new XYLocator(cell.getRow(), cell.getColumn()); // XYLocator xy = new XYLocator(cell.getRow(), cell.getColumn());
records.put(cell, cell); Object o = records.put(cell, cell);
if ((cell.getColumn() < firstcell) || (firstcell == -1)) if ((cell.getColumn() < firstcell) || (firstcell == -1))
{ {
firstcell = cell.getColumn(); firstcell = cell.getColumn();
@ -138,15 +137,26 @@ public class ValueRecordsAggregate
{ {
int k = 0; int k = 0;
FormulaRecordAggregate lastFormulaAggregate = null;
for (k = offset; k < records.size(); k++) for (k = offset; k < records.size(); k++)
{ {
Record rec = ( Record ) records.get(k); Record rec = ( Record ) records.get(k);
if (!rec.isInValueSection() && !(rec instanceof UnknownRecord)) if (rec instanceof StringRecord == false && !rec.isInValueSection() && !(rec instanceof UnknownRecord))
{ {
break; break;
} }
if (rec.isValue()) if (rec instanceof FormulaRecord)
{
lastFormulaAggregate = new FormulaRecordAggregate((FormulaRecord)rec, null);
insertCell( lastFormulaAggregate );
}
else if (rec instanceof StringRecord)
{
lastFormulaAggregate.setStringRecord((StringRecord)rec);
}
else if (rec.isValue())
{ {
insertCell(( CellValueRecordInterface ) rec); insertCell(( CellValueRecordInterface ) rec);
} }
@ -175,7 +185,6 @@ public class ValueRecordsAggregate
} }
return pos - offset; return pos - offset;
} }
/** /**
* called by the constructor, should set class level fields. Should throw * called by the constructor, should set class level fields. Should throw
* runtime exception for bad/icomplete data. * runtime exception for bad/icomplete data.

View File

@ -299,14 +299,14 @@ public abstract class Ptg
*/ */
public abstract String toFormulaString(SheetReferences refs); public abstract String toFormulaString(SheetReferences refs);
/** /**
* dump a debug representation (hexdump) to a strnig * dump a debug representation (hexdump) to a string
*/ */
public String toDebugString() { public String toDebugString() {
byte[] ba = new byte[getSize()]; byte[] ba = new byte[getSize()];
String retval=null; String retval=null;
writeBytes(ba,0); writeBytes(ba,0);
try { try {
retval = org.apache.poi.util.HexDump.dump(ba,0,0); retval = org.apache.poi.util.HexDump.dump(ba,0,0);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -71,6 +71,7 @@ import org.apache.poi.hssf.record.NumberRecord;
import org.apache.poi.hssf.record.BlankRecord; import org.apache.poi.hssf.record.BlankRecord;
import org.apache.poi.hssf.record.BoolErrRecord; import org.apache.poi.hssf.record.BoolErrRecord;
import org.apache.poi.hssf.record.ExtendedFormatRecord; import org.apache.poi.hssf.record.ExtendedFormatRecord;
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.record.formula.Ptg; import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.hssf.util.SheetReferences; import org.apache.poi.hssf.util.SheetReferences;
@ -259,10 +260,11 @@ public class HSSFCell
break; break;
case CELL_TYPE_FORMULA : case CELL_TYPE_FORMULA :
record = new FormulaRecord(); FormulaRecord formulaRecord = new FormulaRecord();
(( FormulaRecord ) record).setColumn(col); record = new FormulaRecordAggregate(formulaRecord,null);
(( FormulaRecord ) record).setRow(row); formulaRecord.setColumn(col);
(( FormulaRecord ) record).setXFIndex(( short ) 0); formulaRecord.setRow(row);
formulaRecord.setXFIndex(( short ) 0);
case CELL_TYPE_BOOLEAN : case CELL_TYPE_BOOLEAN :
record = new BoolErrRecord(); record = new BoolErrRecord();
(( BoolErrRecord ) record).setColumn(col); (( BoolErrRecord ) record).setColumn(col);
@ -314,15 +316,14 @@ public class HSSFCell
case CELL_TYPE_STRING : case CELL_TYPE_STRING :
stringValue = stringValue =
book book.getSSTString( ( (LabelSSTRecord ) cval).getSSTIndex());
.getSSTString((( LabelSSTRecord ) cval).getSSTIndex());
break; break;
case CELL_TYPE_BLANK : case CELL_TYPE_BLANK :
break; break;
case CELL_TYPE_FORMULA : case CELL_TYPE_FORMULA :
cellValue = (( FormulaRecord ) cval).getValue(); cellValue = (( FormulaRecordAggregate ) cval).getFormulaRecord().getValue();
break; break;
case CELL_TYPE_BOOLEAN : case CELL_TYPE_BOOLEAN :
@ -369,7 +370,7 @@ public class HSSFCell
retval = HSSFCell.CELL_TYPE_STRING; retval = HSSFCell.CELL_TYPE_STRING;
break; break;
case FormulaRecord.sid : case FormulaRecordAggregate.sid :
retval = HSSFCell.CELL_TYPE_FORMULA; retval = HSSFCell.CELL_TYPE_FORMULA;
break; break;
@ -446,20 +447,20 @@ public class HSSFCell
{ {
case CELL_TYPE_FORMULA : case CELL_TYPE_FORMULA :
FormulaRecord frec = null; FormulaRecordAggregate frec = null;
if (cellType != this.cellType) if (cellType != this.cellType)
{ {
frec = new FormulaRecord(); frec = new FormulaRecordAggregate(new FormulaRecord(),null);
} }
else else
{ {
frec = ( FormulaRecord ) record; frec = ( FormulaRecordAggregate ) record;
} }
frec.setColumn(getCellNum()); frec.setColumn(getCellNum());
if (setValue) if (setValue)
{ {
frec.setValue(getNumericCellValue()); frec.getFormulaRecord().setValue(getNumericCellValue());
} }
frec.setXFIndex(( short ) cellStyle.getIndex()); frec.setXFIndex(( short ) cellStyle.getIndex());
frec.setRow(row); frec.setRow(row);
@ -676,8 +677,7 @@ public class HSSFCell
} }
else else
{ {
if ((cellType != CELL_TYPE_STRING) if ((cellType != CELL_TYPE_STRING ) && ( cellType != CELL_TYPE_FORMULA))
&& (cellType != CELL_TYPE_FORMULA))
{ {
setCellType(CELL_TYPE_STRING, false); setCellType(CELL_TYPE_STRING, false);
} }
@ -702,9 +702,9 @@ public class HSSFCell
setCellType(CELL_TYPE_BLANK,false); setCellType(CELL_TYPE_BLANK,false);
} else { } else {
setCellType(CELL_TYPE_FORMULA,false); setCellType(CELL_TYPE_FORMULA,false);
FormulaRecord rec = (FormulaRecord) record; FormulaRecordAggregate rec = (FormulaRecordAggregate) record;
rec.setOptions(( short ) 2); rec.getFormulaRecord().setOptions(( short ) 2);
rec.setValue(0); rec.getFormulaRecord().setValue(0);
rec.setXFIndex(( short ) 0x0f); rec.setXFIndex(( short ) 0x0f);
FormulaParser fp = new FormulaParser(formula+";",book); FormulaParser fp = new FormulaParser(formula+";",book);
fp.parse(); fp.parse();
@ -713,9 +713,9 @@ public class HSSFCell
//System.out.println("got Ptgs " + ptg.length); //System.out.println("got Ptgs " + ptg.length);
for (int k = 0; k < ptg.length; k++) { for (int k = 0; k < ptg.length; k++) {
size += ptg[ k ].getSize(); size += ptg[ k ].getSize();
rec.pushExpressionToken(ptg[ k ]); rec.getFormulaRecord().pushExpressionToken(ptg[ k ]);
} }
rec.setExpressionLength(( short ) size); rec.getFormulaRecord().setExpressionLength(( short ) size);
//Workbook.currentBook = null; //Workbook.currentBook = null;
} }
} }
@ -723,7 +723,7 @@ public class HSSFCell
public String getCellFormula() { public String getCellFormula() {
//Workbook.currentBook=book; //Workbook.currentBook=book;
SheetReferences refs = book.getSheetReferences(); SheetReferences refs = book.getSheetReferences();
String retval = FormulaParser.toFormulaString(refs, ((FormulaRecord)record).getParsedExpression()); String retval = FormulaParser.toFormulaString(refs, ((FormulaRecordAggregate)record).getFormulaRecord().getParsedExpression());
//Workbook.currentBook=null; //Workbook.currentBook=null;
return retval; return retval;
} }
@ -825,8 +825,7 @@ public class HSSFCell
public void setCellValue(boolean value) public void setCellValue(boolean value)
{ {
if ((cellType != CELL_TYPE_BOOLEAN) if ((cellType != CELL_TYPE_BOOLEAN ) && ( cellType != CELL_TYPE_FORMULA))
&& (cellType != CELL_TYPE_FORMULA))
{ {
setCellType(CELL_TYPE_BOOLEAN, false); setCellType(CELL_TYPE_BOOLEAN, false);
} }

View File

@ -152,14 +152,14 @@ public class HSSFWorkbook
workbook = Workbook.createWorkbook(records); workbook = Workbook.createWorkbook(records);
setPropertiesFromWorkbook(workbook); setPropertiesFromWorkbook(workbook);
int numRecords = workbook.getNumRecords(); int recOffset = workbook.getNumRecords();
int sheetNum = 0; int sheetNum = 0;
while (numRecords < records.size()) while (recOffset < records.size())
{ {
Sheet sheet = Sheet.createSheet(records, sheetNum++, numRecords); Sheet sheet = Sheet.createSheet(records, sheetNum++, recOffset );
numRecords += sheet.getNumRecords(); recOffset = sheet.getEofLoc()+1;
sheet.convertLabelRecords( sheet.convertLabelRecords(
workbook); // convert all LabelRecord records to LabelSSTRecord workbook); // convert all LabelRecord records to LabelSSTRecord
HSSFSheet hsheet = new HSSFSheet(workbook, sheet); HSSFSheet hsheet = new HSSFSheet(workbook, sheet);
@ -511,8 +511,8 @@ public class HSSFWorkbook
{ {
totalsize = 4096; totalsize = 4096;
} }
byte[] retval = new byte[totalsize]; byte[] data = new byte[totalsize];
int pos = workbook.serialize(0, retval); int pos = workbook.serialize(0, data);
// System.arraycopy(wb, 0, retval, 0, wb.length); // System.arraycopy(wb, 0, retval, 0, wb.length);
for (int k = 0; k < sheets.size(); k++) for (int k = 0; k < sheets.size(); k++)
@ -521,13 +521,13 @@ public class HSSFWorkbook
// byte[] sb = (byte[])sheetbytes.get(k); // byte[] sb = (byte[])sheetbytes.get(k);
// System.arraycopy(sb, 0, retval, pos, sb.length); // System.arraycopy(sb, 0, retval, pos, sb.length);
pos += ((HSSFSheet) sheets.get(k)).getSheet().serialize(pos, pos += ((HSSFSheet) sheets.get(k)).getSheet().serialize(pos,
retval); // sb.length; data); // sb.length;
} }
for (int k = pos; k < totalsize; k++) for (int k = pos; k < totalsize; k++)
{ {
retval[k] = 0; data[k] = 0;
} }
return retval; return data;
} }
public int addSSTString(String string) public int addSSTString(String string)

View File

@ -386,7 +386,7 @@ public class LittleEndian
public static void putDouble(final byte[] data, final int offset, public static void putDouble(final byte[] data, final int offset,
final double value) { final double value) {
putNumber(data, offset, Double.doubleToLongBits(value), DOUBLE_SIZE); putNumber(data, offset, Double.doubleToRawLongBits(value), DOUBLE_SIZE);
} }

View File

@ -147,9 +147,13 @@ public class TestLittleEndian
{ {
56, 50, -113, -4, -63, -64, -13, 63, 76, -32, -42, -35, 60, -43, 3, 64 56, 50, -113, -4, -63, -64, -13, 63, 76, -32, -42, -35, 60, -43, 3, 64
}; };
private static final byte[] _nan_double_array =
{
(byte)0x00, (byte)0x00, (byte)0x3C, (byte)0x00, (byte)0x20, (byte)0x04, (byte)0xFF, (byte)0xFF
};
private static final double[] _doubles = private static final double[] _doubles =
{ {
1.23456, 2.47912 1.23456, 2.47912, Double.NaN
}; };
/** /**
@ -158,10 +162,21 @@ public class TestLittleEndian
public void testGetDouble() public void testGetDouble()
{ {
assertEquals(_doubles[ 0 ], LittleEndian.getDouble(_double_array), assertEquals(_doubles[ 0 ], LittleEndian.getDouble(_double_array), 0.000001 );
0.000001); assertEquals(_doubles[ 1 ], LittleEndian.getDouble( _double_array, LittleEndian.DOUBLE_SIZE), 0.000001);
assertEquals(_doubles[ 1 ], LittleEndian assertTrue(Double.isNaN(LittleEndian.getDouble(_nan_double_array)));
.getDouble(_double_array, LittleEndian.DOUBLE_SIZE), 0.000001);
// does not work. apparently nan does not always equal nan!
//assertEquals(_doubles[ 2 ], LittleEndian.getDouble(_nan_double_array), 0.000001);
double nan = LittleEndian.getDouble(_nan_double_array);
byte[] data = new byte[8];
LittleEndian.putDouble(data, nan);
for ( int i = 0; i < data.length; i++ )
{
byte b = data[i];
assertEquals(data[i], _nan_double_array[i]);
}
} }
/** /**