Skip to content

Commit d117241

Browse files
authored
Open up xssfb to simplify client usage/modifications. (#1042)
* Open up xssfb to simplify client usage/modifications. * add @SInCE and simplify with Math.toIntExact
1 parent b5d2426 commit d117241

8 files changed

Lines changed: 40 additions & 44 deletions

File tree

poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBCellHeader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class XSSFBCellHeader {
4040
* @param cell cell buffer to update
4141
*/
4242
public static void parse(byte[] data, int offset, int currentRow, XSSFBCellHeader cell) {
43-
int colNum = XSSFBUtils.castToInt(LittleEndian.getUInt(data, offset)); offset += LittleEndianConsts.INT_SIZE;
43+
int colNum = Math.toIntExact(LittleEndian.getUInt(data, offset)); offset += LittleEndianConsts.INT_SIZE;
4444
int styleIdx = XSSFBUtils.get24BitInt(data, offset); offset += 3;
4545
//TODO: range checking
4646
boolean showPhonetic = false;//TODO: fill this out

poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBCellRange.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,49 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2323
import org.apache.poi.util.LittleEndianConsts;
2424

2525
/**
26-
* @since 3.16-beta3
26+
* @since 6.0.0
2727
*/
2828
@Internal
29-
class XSSFBCellRange {
29+
public class XSSFBCellRange {
30+
3031
//TODO: Convert this to generate an AreaReference
32+
public static final int length = 4 * LittleEndianConsts.INT_SIZE;
33+
34+
private int firstRow;
35+
private int lastRow;
36+
private int firstCol;
37+
private int lastCol;
38+
39+
public XSSFBCellRange(int firstRow, int lastRow, int firstCol, int lastCol) {
40+
this.firstRow = firstRow;
41+
this.lastRow = lastRow;
42+
this.firstCol = firstCol;
43+
this.lastCol = lastCol;
44+
}
45+
46+
XSSFBCellRange() {
47+
}
3148

32-
public static final int length = 4* LittleEndianConsts.INT_SIZE;
3349
/**
3450
* Parses an RfX cell range from the data starting at the offset.
35-
* This performs no range checking.
3651
* @param data raw bytes
3752
* @param offset offset at which to start reading from data
38-
* @param cellRange to overwrite. If null, a new cellRange will be created.
39-
* @return a mutable cell range.
53+
* @param cellRange to update. If null, a new cellRange will be created.
54+
* @return the updated or new cell range
4055
*/
4156
public static XSSFBCellRange parse(byte[] data, int offset, XSSFBCellRange cellRange) {
4257
if (cellRange == null) {
4358
cellRange = new XSSFBCellRange();
4459
}
45-
cellRange.firstRow = XSSFBUtils.castToInt(LittleEndian.getUInt(data, offset)); offset += LittleEndianConsts.INT_SIZE;
46-
cellRange.lastRow = XSSFBUtils.castToInt(LittleEndian.getUInt(data, offset)); offset += LittleEndianConsts.INT_SIZE;
47-
cellRange.firstCol = XSSFBUtils.castToInt(LittleEndian.getUInt(data, offset)); offset += LittleEndianConsts.INT_SIZE;
48-
cellRange.lastCol = XSSFBUtils.castToInt(LittleEndian.getUInt(data, offset));
49-
60+
cellRange.firstRow = Math.toIntExact(LittleEndian.getUInt(data, offset)); offset += LittleEndianConsts.INT_SIZE;
61+
cellRange.lastRow = Math.toIntExact(LittleEndian.getUInt(data, offset)); offset += LittleEndianConsts.INT_SIZE;
62+
cellRange.firstCol = Math.toIntExact(LittleEndian.getUInt(data, offset)); offset += LittleEndianConsts.INT_SIZE;
63+
cellRange.lastCol = Math.toIntExact(LittleEndian.getUInt(data, offset));
5064
return cellRange;
5165
}
5266

53-
int firstRow;
54-
int lastRow;
55-
int firstCol;
56-
int lastCol;
57-
58-
67+
public int getFirstRow() { return firstRow; }
68+
public int getLastRow() { return lastRow; }
69+
public int getFirstCol() { return firstCol; }
70+
public int getLastCol() { return lastCol; }
5971
}

poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBCommentsTable.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,10 @@ public void handleRecord(int id, byte[] data) throws XSSFBParseException {
6161
switch (recordType) {
6262
case BrtBeginComment:
6363
int offset = 0;
64-
authorId = XSSFBUtils.castToInt(LittleEndian.getUInt(data)); offset += LittleEndianConsts.INT_SIZE;
64+
authorId = Math.toIntExact(LittleEndian.getUInt(data)); offset += LittleEndianConsts.INT_SIZE;
6565
cellRange = XSSFBCellRange.parse(data, offset, cellRange);
66-
offset+= XSSFBCellRange.length;
6766
//for strict parsing; confirm that firstRow==lastRow and firstCol==colLats (2.4.28)
68-
cellAddress = new CellAddress(cellRange.firstRow, cellRange.firstCol);
67+
cellAddress = new CellAddress(cellRange.getFirstRow(), cellRange.getFirstCol());
6968
break;
7069
case BrtCommentText:
7170
XSSFBRichStr xssfbRichStr = XSSFBRichStr.build(data, 0);

poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBHyperlinksTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public void handleRecord(int recordType, byte[] data) throws XSSFBParseException
149149
xlWideStringBuffer.setLength(0);
150150
/*offset +=*/ XSSFBUtils.readXLWideString(data, offset, xlWideStringBuffer);
151151
String display = xlWideStringBuffer.toString();
152-
CellRangeAddress cellRangeAddress = new CellRangeAddress(hyperlinkCellRange.firstRow, hyperlinkCellRange.lastRow, hyperlinkCellRange.firstCol, hyperlinkCellRange.lastCol);
152+
CellRangeAddress cellRangeAddress = new CellRangeAddress(hyperlinkCellRange.getFirstRow(), hyperlinkCellRange.getLastRow(), hyperlinkCellRange.getFirstCol(), hyperlinkCellRange.getLastCol());
153153

154154
String url = relIdToHyperlink.get(relId);
155155
if (location.isEmpty()) {

poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBRichStr.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2020
import org.apache.poi.util.Internal;
2121

2222
/**
23-
* @since 3.16-beta3
23+
* @since 6.0.0
2424
*/
2525
@Internal
26-
class XSSFBRichStr {
26+
public class XSSFBRichStr {
2727

2828
public static XSSFBRichStr build(byte[] bytes, int offset) throws XSSFBParseException {
2929
byte first = bytes[offset];

poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBSharedStringsTable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ public void handleRecord(int recordType, byte[] data) throws XSSFBParseException
132132
strings.add(rstr.getString());
133133
break;
134134
case BrtBeginSst:
135-
count = XSSFBUtils.castToInt(LittleEndian.getUInt(data,0));
136-
uniqueCount = XSSFBUtils.castToInt(LittleEndian.getUInt(data, 4));
135+
count = Math.toIntExact(LittleEndian.getUInt(data,0));
136+
uniqueCount = Math.toIntExact(LittleEndian.getUInt(data, 4));
137137
break;
138138
}
139139

poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBSheetHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void handleRecord(int id, byte[] data) throws XSSFBParseException {
130130

131131
switch(type) {
132132
case BrtRowHdr:
133-
int rw = XSSFBUtils.castToInt(LittleEndian.getUInt(data, 0));
133+
int rw = Math.toIntExact(LittleEndian.getUInt(data, 0));
134134
if (rw > 0x00100000) {//could make sure this is larger than currentRow, according to spec?
135135
throw new XSSFBParseException("Row number beyond allowable range: "+rw);
136136
}
@@ -298,7 +298,7 @@ private void handleCellRk(byte[] data) {
298298

299299
private void handleBrtCellIsst(byte[] data) {
300300
beforeCellValue(data);
301-
int idx = XSSFBUtils.castToInt(LittleEndian.getUInt(data, XSSFBCellHeader.length));
301+
int idx = Math.toIntExact(LittleEndian.getUInt(data, XSSFBCellHeader.length));
302302
RichTextString rtss = stringsTable.getItemAt(idx);
303303
handleStringCellValue(rtss.getString());
304304
}

poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBUtils.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2020

2121
import java.nio.charset.StandardCharsets;
2222

23-
import org.apache.poi.ooxml.POIXMLException;
2423
import org.apache.poi.util.Internal;
2524
import org.apache.poi.util.LittleEndian;
2625

@@ -85,23 +84,9 @@ public static int readXLWideString(byte[] data, int offset, StringBuilder sb) th
8584
return numBytes;
8685
}
8786

88-
static int castToInt(long val) {
89-
if (val < Integer.MAX_VALUE && val > Integer.MIN_VALUE) {
90-
return (int)val;
91-
}
92-
throw new POIXMLException("val ("+val+") can't be cast to int");
93-
}
94-
95-
static short castToShort(int val) {
96-
if (val < Short.MAX_VALUE && val > Short.MIN_VALUE) {
97-
return (short)val;
98-
}
99-
throw new POIXMLException("val ("+val+") can't be cast to short");
100-
101-
}
10287

10388
//TODO: move to LittleEndian?
104-
static int get24BitInt( byte[] data, int offset) {
89+
static int get24BitInt(byte[] data, int offset) {
10590
int i = offset;
10691
int b0 = data[i++] & 0xFF;
10792
int b1 = data[i++] & 0xFF;

0 commit comments

Comments
 (0)