Row row = sheet.getRow(i - sheet.getFirstRowNum());
if (row != null) {
short h = row.getHeight();
- int height = (int)Math.round(Math.max(1., h / (res / 70. * 20.) + 3.));
+ int height = Math.toIntExact(Math.round(Math.max(1., h / (res / 70. * 20.) + 3.)));
System.out.printf("%d: %d (%d @ %d)%n", i, height, h, res);
setRowHeight(i, height);
}
if (converted) {
System.out.println("Conversion took " +
- (int)((System.currentTimeMillis() - startTime)/1000) + " seconds");
+ ((System.currentTimeMillis() - startTime)/1000) + " seconds");
}
}
* @return the approximate number of pixels for a typical display
*/
protected int widthToPixels(final double widthUnits) {
- return (int) (Math.round(widthUnits * 9 / 256));
+ return Math.toIntExact(Math.round(widthUnits * 9 / 256));
}
private void printCols(Map<Integer, Integer> widths) {
final TreeBidiMap<Long,Long> offset2Id = new TreeBidiMap<>();
for (int i = 0; i < propertyCount; i++) {
/* Read the property ID. */
- long id = (int)leis.readUInt();
+ long id = leis.readUInt();
/* Offset from the section's start. */
- long off = (int)leis.readUInt();
+ long off = leis.readUInt();
offset2Id.put(off, id);
}
int codepage = -1;
if (cpOffset != null) {
/* Read the property's value type. It must be VT_I2. */
- leis.setReadIndex((int)(this._offset + cpOffset));
+ leis.setReadIndex(Math.toIntExact(this._offset + cpOffset));
final long type = leis.readUInt();
if (type != Variant.VT_I2) {
}
int pLen = propLen(offset2Id, off, size);
- leis.setReadIndex((int)(this._offset + off));
+ leis.setReadIndex(Math.toIntExact(this._offset + off));
if (id == PropertyIDMap.PID_DICTIONARY) {
leis.mark(100000);
}
}
- sectionBytes.write(src, (int)_offset, size);
+ sectionBytes.write(src, Math.toIntExact(_offset), size);
padSectionBytes();
}
Long nextKey = offset2Id.nextKey(entryOffset);
long begin = entryOffset;
long end = (nextKey != null) ? nextKey : maxSize;
- return (int)(end - begin);
+ return Math.toIntExact(end - begin);
}
/* Read the string - Strip 0x00 characters from the end of the string. */
int cp = (codepage == -1) ? Property.DEFAULT_CODEPAGE : codepage;
- int nrBytes = (int)((sLength-1) * (cp == CodePageUtil.CP_UNICODE ? 2 : 1));
+ int nrBytes = Math.toIntExact(((sLength-1) * (cp == CodePageUtil.CP_UNICODE ? 2 : 1)));
if (nrBytes > 0xFFFFFF) {
LOG.log(POILogger.WARN, errMsg);
isCorrupted = true;
for (Property aPa : pa) {
hashCode += aPa.hashCode();
}
- return (int) (hashCode & 0x0ffffffffL);
+ return Math.toIntExact(hashCode & 0x0ffffffffL);
}
d = -d;
}
long v = (long) Math.floor(d);
- return (int) (v & 0x0001);
+ return Math.toIntExact(v & 0x0001);
}
}
if(rowNum > Integer.MAX_VALUE) {
return false;
}
- return isRowWithinRange((int)rowNum, ssVersion);
+ return isRowWithinRange(Math.toIntExact(rowNum), ssVersion);
}
/**
private final int _binaryExponent;
public ExpandedDouble(long rawBits) {
- int biasedExp = (int) (rawBits >> 52);
+ int biasedExp = Math.toIntExact(rawBits >> 52);
if (biasedExp == 0) {
// sub-normal numbers
BigInteger frac = BigInteger.valueOf(rawBits).and(BI_FRAC_MASK);
* @return the top 12 bits (sign and biased exponent value)
*/
public static int getBiasedExponent(long rawBits) {
- return (int) ((rawBits & EXPONENT_MASK) >> EXPONENT_SHIFT);
+ return Math.toIntExact((rawBits & EXPONENT_MASK) >> EXPONENT_SHIFT);
}
}
char[] buf = new char[nDigits];
long acc = value;
for(int i=nDigits-1; i>=0; i--) {
- int digit = (int)(acc & 0x0F);
+ int digit = Math.toIntExact(acc & 0x0F);
buf[i] = (char) (digit < 10 ? ('0' + digit) : ('A' + digit - 10));
acc >>>= 4;
}
==================================================================== */
package org.apache.poi.openxml4j.util;
-import static org.apache.commons.collections4.IteratorUtils.asIterable;
-import static org.apache.commons.collections4.IteratorUtils.asIterator;
-
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
}
// the opc spec allows case-insensitive filename matching (see #49609)
- for (final ZipArchiveEntry ze : asIterable(asIterator(zipArchive.getEntries()))) {
+ final Enumeration<ZipArchiveEntry> zipArchiveEntryEnumeration = zipArchive.getEntries();
+ while (zipArchiveEntryEnumeration.hasMoreElements()) {
+ ZipArchiveEntry ze = zipArchiveEntryEnumeration.nextElement();
if (normalizedPath.equalsIgnoreCase(ze.getName().replace('\\','/'))) {
return ze;
}
import java.io.IOException;
import java.io.InputStream;
+import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
-import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
/**
@Override
public Enumeration<? extends ZipArchiveEntry> getEntries() {
- return IteratorUtils.asEnumeration(zipEntries.values().iterator());
+ return Collections.enumeration(zipEntries.values());
}
@Override
public static final int OOXML_DEGREE = 60_000;
public static final int degreesToAttribute(double angle) {
- return (int) (OOXML_DEGREE * angle);
+ return Math.toIntExact(Math.round(OOXML_DEGREE * angle));
}
public static final double attributeToDegrees(int angle) {
}
public void setPercent(double value) {
- percent.setVal((int) (1000 * value));
+ percent.setVal(Math.toIntExact(Math.round(1000 * value)));
}
}
}
public void setPoints(double value) {
- points.setVal((int) (100 * value));
+ points.setVal(Math.toIntExact(Math.round(100 * value)));
}
}
} else if (size < 1 || 400 < size) {
throw new IllegalArgumentException("Minimum inclusive = 1. Maximum inclusive = 400.");
} else {
- props.setSz((int) (100 * size));
+ props.setSz((int)(100 * size));
}
}
*/
@Override
public XSLFPictureData addPicture(File pict, PictureType format) throws IOException {
- int length = (int) pict.length();
- byte[] data = IOUtils.safelyAllocate(length, MAX_RECORD_LENGTH);
+ byte[] data = IOUtils.safelyAllocate(pict.length(), MAX_RECORD_LENGTH);
try (InputStream is = new FileInputStream(pict)) {
IOUtils.readFully(is, data);
}
// value of 0 or 1000 indicates no background,
// values 1-999 refer to the index of a fill style within the fillStyleLst element
// values 1001 and above refer to the index of a background fill style within the bgFillStyleLst element.
- int idx = (int)fillRef.getIdx();
+ long idx = fillRef.getIdx();
CTStyleMatrix matrix = theme.getXmlObject().getThemeElements().getFmtScheme();
final XmlObject styleLst;
- int childIdx;
+ long childIdx;
if (idx >= 1 && idx <= 999) {
childIdx = idx-1;
styleLst = (isLineStyle) ? matrix.getLnStyleLst() : matrix.getFillStyleLst();
}
XmlCursor cur = styleLst.newCursor();
XSLFFillProperties fp = null;
- if (cur.toChild(childIdx)) {
+ if (cur.toChild(Math.toIntExact(childIdx))) {
fp = XSLFPropertiesDelegate.getFillDelegate(cur.getObject());
}
cur.dispose();
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndWidth;
import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal;
import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
-import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
/**
* Represents a single (non-group) shape in a .pptx slide show
return null;
}
// 1-based index of a line style within the style matrix
- int idx = (int)lnRef.getIdx();
+ int idx = Math.toIntExact(lnRef.getIdx());
XSLFTheme theme = getSheet().getTheme();
if (theme == null) {
if (lnRef == null) {
return null;
}
- int idx = (int)lnRef.getIdx();
+ int idx = Math.toIntExact(lnRef.getIdx());
CTSchemeColor phClr = lnRef.getSchemeClr();
if(idx <= 0){
return null;
break;
}
- final int width = (int) Math.rint(pgsize.getWidth() * scale / lenSide);
- final int height = (int) Math.rint(pgsize.getHeight() * scale / lenSide);
+ final int width = Math.toIntExact(Math.round(pgsize.getWidth() * scale / lenSide));
+ final int height = Math.toIntExact(Math.round(pgsize.getHeight() * scale / lenSide));
for (int slideNo : slidenum) {
@Override
public String getAuthor(long authorId) {
- return comments.getAuthors().getAuthorArray((int)authorId);
+ return comments.getAuthors().getAuthorArray(Math.toIntExact(authorId));
}
@Override
}
}
final double width = maxColumnWidths.get(column).getMaxColumnWidth(useMergedCells);
- return (int) (256*width);
+ return Math.toIntExact(Math.round(256*width));
}
}
current.size = def.getBytesRead();
- current.compressedSize = (int) def.getBytesWritten();
+ current.compressedSize = Math.toIntExact(def.getBytesWritten());
current.crc = crc.getValue();
written += current.compressedSize;
if (f == null) {
return null;
} else if (f.getT() == STCellFormulaType.SHARED) {
- return convertSharedFormula((int)f.getSi(), fpb == null ? XSSFEvaluationWorkbook.create(getSheet().getWorkbook()) : fpb);
+ return convertSharedFormula(Math.toIntExact(f.getSi()),
+ fpb == null ? XSSFEvaluationWorkbook.create(getSheet().getWorkbook()) : fpb);
} else {
return f.getStringValue();
}
XSSFCellStyle style = null;
if(_stylesSource.getNumCellStyles() > 0){
long idx = _cell.isSetS() ? _cell.getS() : 0;
- style = _stylesSource.getStyleAt((int)idx);
+ style = _stylesSource.getStyleAt(Math.toIntExact(idx));
}
return style;
}
}
CalculationChain calcChain = getSheet().getWorkbook().getCalculationChain();
- int sheetId = (int)getSheet().sheet.getSheetId();
+ int sheetId = Math.toIntExact(getSheet().sheet.getSheetId());
//remove the reference in the calculation chain
if(calcChain != null) calcChain.removeItem(sheetId, getReference());
public BorderStyle getBorderBottom() {
if(!_cellXf.getApplyBorder()) return BorderStyle.NONE;
- int idx = (int)_cellXf.getBorderId();
+ int idx = Math.toIntExact(_cellXf.getBorderId());
CTBorder ct = _stylesSource.getBorderAt(idx).getCTBorder();
STBorderStyle.Enum ptrn = ct.isSetBottom() ? ct.getBottom().getStyle() : null;
if (ptrn == null) {
public BorderStyle getBorderLeft() {
if(!_cellXf.getApplyBorder()) return BorderStyle.NONE;
- int idx = (int)_cellXf.getBorderId();
+ int idx = Math.toIntExact(_cellXf.getBorderId());
CTBorder ct = _stylesSource.getBorderAt(idx).getCTBorder();
STBorderStyle.Enum ptrn = ct.isSetLeft() ? ct.getLeft().getStyle() : null;
if (ptrn == null) {
public BorderStyle getBorderRight() {
if(!_cellXf.getApplyBorder()) return BorderStyle.NONE;
- int idx = (int)_cellXf.getBorderId();
+ int idx = Math.toIntExact(_cellXf.getBorderId());
CTBorder ct = _stylesSource.getBorderAt(idx).getCTBorder();
STBorderStyle.Enum ptrn = ct.isSetRight() ? ct.getRight().getStyle() : null;
if (ptrn == null) {
public BorderStyle getBorderTop() {
if(!_cellXf.getApplyBorder()) return BorderStyle.NONE;
- int idx = (int)_cellXf.getBorderId();
+ int idx = Math.toIntExact(_cellXf.getBorderId());
CTBorder ct = _stylesSource.getBorderAt(idx).getCTBorder();
STBorderStyle.Enum ptrn = ct.isSetTop() ? ct.getTop().getStyle() : null;
if (ptrn == null) {
public XSSFColor getBottomBorderXSSFColor() {
if(!_cellXf.getApplyBorder()) return null;
- int idx = (int)_cellXf.getBorderId();
+ int idx = Math.toIntExact(_cellXf.getBorderId());
XSSFCellBorder border = _stylesSource.getBorderAt(idx);
return border.getBorderColor(BorderSide.BOTTOM);
public XSSFColor getLeftBorderXSSFColor() {
if(!_cellXf.getApplyBorder()) return null;
- int idx = (int)_cellXf.getBorderId();
+ int idx = Math.toIntExact(_cellXf.getBorderId());
XSSFCellBorder border = _stylesSource.getBorderAt(idx);
return border.getBorderColor(BorderSide.LEFT);
public XSSFColor getRightBorderXSSFColor() {
if(!_cellXf.getApplyBorder()) return null;
- int idx = (int)_cellXf.getBorderId();
+ int idx = Math.toIntExact(_cellXf.getBorderId());
XSSFCellBorder border = _stylesSource.getBorderAt(idx);
return border.getBorderColor(BorderSide.RIGHT);
public XSSFColor getTopBorderXSSFColor() {
if(!_cellXf.getApplyBorder()) return null;
- int idx = (int)_cellXf.getBorderId();
+ int idx = Math.toIntExact(_cellXf.getBorderId());
XSSFCellBorder border = _stylesSource.getBorderAt(idx);
return border.getBorderColor(BorderSide.TOP);
private CTBorder getCTBorder(){
CTBorder ct;
if(_cellXf.getApplyBorder()) {
- int idx = (int)_cellXf.getBorderId();
+ int idx = Math.toIntExact(_cellXf.getBorderId());
XSSFCellBorder cf = _stylesSource.getBorderAt(idx);
ct = (CTBorder)cf.getCTBorder().copy();
}
public int getDx1() {
- return (int) getCell1().getColOff();
+ return Math.toIntExact(getCell1().getColOff());
}
/**
}
public int getDy1() {
- return (int) getCell1().getRowOff();
+ return Math.toIntExact(getCell1().getRowOff());
}
/**
}
public int getDy2() {
- return (int) getCell2().getRowOff();
+ return Math.toIntExact(getCell2().getRowOff());
}
/**
}
public int getDx2() {
- return (int) getCell2().getColOff();
+ return Math.toIntExact(getCell2().getColOff());
}
/**
*/
@Override
public int getRowNum() {
- return (int) (_row.getR() - 1);
+ return Math.toIntExact(_row.getR() - 1);
}
/**
StylesTable stylesSource = getSheet().getWorkbook().getStylesSource();
if(stylesSource.getNumCellStyles() > 0) {
- return stylesSource.getStyleAt((int)_row.getS());
+ return stylesSource.getStyleAt(Math.toIntExact(_row.getS()));
} else {
return null;
}
if (width > maxColumnWidth) {
width = maxColumnWidth;
}
- setColumnWidth(column, (int)(width));
+ setColumnWidth(column, Math.toIntExact(Math.round(width)));
columnHelper.setColBestFit(column, true);
}
}
CTBreak[] brkArray = ctPageBreak.getBrkArray();
int[] breaks = new int[brkArray.length];
for (int i = 0 ; i < brkArray.length ; i++) {
- breaks[i] = (int) brkArray[i].getId() - 1;
+ breaks[i] = Math.toIntExact(brkArray[i].getId() - 1);
}
return breaks;
}
public int getColumnWidth(int columnIndex) {
CTCol col = columnHelper.getColumn(columnIndex, false);
double width = col == null || !col.isSetWidth() ? getDefaultColumnWidth() : col.getWidth();
- return (int)(width*256);
+ return Math.toIntExact(Math.round(width*256));
}
/**
@Override
public int getDefaultColumnWidth() {
CTSheetFormatPr pr = worksheet.getSheetFormatPr();
- return pr == null ? 8 : (int)pr.getBaseColWidth();
+ return pr == null ? 8 : Math.toIntExact(pr.getBaseColWidth());
}
/**
//col must exist
short outlineLevel=col.getOutlineLevel();
col.setOutlineLevel((short)(outlineLevel+1));
- index=(int)col.getMax();
+ index = Math.toIntExact(col.getMax());
}
worksheet.setColsArray(0,ctCols);
setSheetFormatPrOutlineLevelCol();
// split to 3 records
CTCol ciMid = columnHelper.cloneCol(cols, ci);
CTCol ciEnd = columnHelper.cloneCol(cols, ci);
- int lastcolumn = (int) ciMax;
+ int lastcolumn = Math.toIntExact(ciMax);
ci.setMax(targetColumnIx - 1);
}
idx++;
}
- return (int) columnInfo.getMax();
+ return Math.toIntExact(columnInfo.getMax());
}
private boolean isAdjacentBefore(CTCol col, CTCol otherCol) {
CTCol col = columnHelper.getColumn(columnIndex, false);
int colInfoIx = columnHelper.getIndexOfColumn(cols, col);
- int idx = findColInfoIdx((int) col.getMax(), colInfoIx);
+ int idx = findColInfoIdx(Math.toIntExact(col.getMax()), colInfoIx);
if (idx == -1) {
return;
}
}
// Write collapse flag (stored in a single col info record after this
// outline group)
- setColumn((int) columnInfo.getMax() + 1, null, null,
+ setColumn(Math.toIntExact(columnInfo.getMax() + 1), null, null,
Boolean.FALSE, Boolean.FALSE);
}
if (col != null) {
short outlineLevel = col.getOutlineLevel();
col.setOutlineLevel((short) (outlineLevel - 1));
- index = (int) col.getMax();
+ index = Math.toIntExact(col.getMax());
if (col.getOutlineLevel() <= 0) {
int colIndex = columnHelper.getIndexOfColumn(cols, col);
// the sheet has (i.e. sheet 1 -> comments 1)
try {
sheetComments = (CommentsTable)createRelationship(
- XSSFRelation.SHEET_COMMENTS, XSSFFactory.getInstance(), (int)sheet.getSheetId());
+ XSSFRelation.SHEET_COMMENTS, XSSFFactory.getInstance(), Math.toIntExact(sheet.getSheetId()));
} catch(PartAlreadyExistsException e) {
// Technically a sheet doesn't need the same number as
// it's comments, and clearly someone has already pinched
sf.setRef(effectiveRef);
}
- sharedFormulas.put((int)f.getSi(), sf);
+ sharedFormulas.put(Math.toIntExact(f.getSi()), sf);
}
if (f != null && f.getT() == STCellFormulaType.ARRAY && f.getRef() != null) {
arrayFormulas.add(CellRangeAddress.valueOf(f.getRef()));
nextCell.getColumnIndex(), ref.getLastColumn());
nextF.setRef(nextRef.formatAsString());
- sharedFormulas.put((int)nextF.getSi(), nextF);
+ sharedFormulas.put(Math.toIntExact(nextF.getSi()), nextF);
break DONE;
}
}
if (shiftedFormula != null) {
f.setStringValue(shiftedFormula);
if(f.getT() == STCellFormulaType.SHARED){
- int si = (int)f.getSi();
+ int si = Math.toIntExact(f.getSi());
CTCellFormula sf = sheet.getSharedFormula(si);
sf.setStringValue(shiftedFormula);
updateRefInCTCellFormula(row, formulaShifter, sf);
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
+
+import java.util.Iterator;
import java.util.Set;
import java.util.HashSet;
-import org.apache.commons.collections4.IteratorUtils;
/**
* Tests CellRangeUtil.
private static Set<CellAddress> getCellAddresses(CellRangeAddress[] ranges) {
final Set<CellAddress> set = new HashSet<>();
for (final CellRangeAddress range : ranges) {
- set.addAll(IteratorUtils.toList(range.iterator()));
+ for (Iterator<CellAddress> iter = range.iterator(); iter.hasNext(); ) {
+ set.add(iter.next());
+ }
}
return set;
}