sonar fix - reduce complexity by using constants
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1744006 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
fa00834766
commit
306edb91e5
@ -40,6 +40,36 @@ import org.apache.poi.util.ShortField;
|
|||||||
* The block containing the archive header
|
* The block containing the archive header
|
||||||
*/
|
*/
|
||||||
public final class HeaderBlock implements HeaderBlockConstants {
|
public final class HeaderBlock implements HeaderBlockConstants {
|
||||||
|
private static final byte[] MAGIC_BIFF2 = {
|
||||||
|
0x09, 0x00, // sid=0x0009
|
||||||
|
0x04, 0x00, // size=0x0004
|
||||||
|
0x00, 0x00, // unused
|
||||||
|
0x70, 0x00 // 0x70 = multiple values
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final byte[] MAGIC_BIFF3 = {
|
||||||
|
0x09, 0x02, // sid=0x0209
|
||||||
|
0x06, 0x00, // size=0x0006
|
||||||
|
0x00, 0x00, // unused
|
||||||
|
0x70, 0x00 // 0x70 = multiple values
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final byte[] MAGIC_BIFF4a = {
|
||||||
|
0x09, 0x04, // sid=0x0409
|
||||||
|
0x06, 0x00, // size=0x0006
|
||||||
|
0x00, 0x00, // unused
|
||||||
|
0x70, 0x00 // 0x70 = multiple values
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final byte[] MAGIC_BIFF4b = {
|
||||||
|
0x09, 0x04, // sid=0x0409
|
||||||
|
0x06, 0x00, // size=0x0006
|
||||||
|
0x00, 0x00, // unused
|
||||||
|
0x00, 0x01
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final byte _default_value = ( byte ) 0xFF;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* What big block size the file uses. Most files
|
* What big block size the file uses. Most files
|
||||||
* use 512 bytes, but a few use 4096
|
* use 512 bytes, but a few use 4096
|
||||||
@ -85,8 +115,6 @@ public final class HeaderBlock implements HeaderBlockConstants {
|
|||||||
*/
|
*/
|
||||||
private final byte[] _data;
|
private final byte[] _data;
|
||||||
|
|
||||||
private static final byte _default_value = ( byte ) 0xFF;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* create a new HeaderBlockReader from an InputStream
|
* create a new HeaderBlockReader from an InputStream
|
||||||
*
|
*
|
||||||
@ -113,58 +141,40 @@ public final class HeaderBlock implements HeaderBlockConstants {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private HeaderBlock(byte[] data) throws IOException {
|
private HeaderBlock(byte[] data) throws IOException {
|
||||||
this._data = data;
|
this._data = data.clone();
|
||||||
|
|
||||||
// verify signature
|
// verify signature
|
||||||
long signature = LittleEndian.getLong(_data, _signature_offset);
|
long signature = LittleEndian.getLong(_data, _signature_offset);
|
||||||
|
|
||||||
if (signature != _signature) {
|
if (signature != _signature) {
|
||||||
// Is it one of the usual suspects?
|
// Is it one of the usual suspects?
|
||||||
byte[] OOXML_FILE_HEADER = POIFSConstants.OOXML_FILE_HEADER;
|
if (cmp(POIFSConstants.OOXML_FILE_HEADER, data)) {
|
||||||
if (_data[0] == OOXML_FILE_HEADER[0] &&
|
throw new OfficeXmlFileException("The supplied data appears to be in the Office 2007+ XML. "
|
||||||
_data[1] == OOXML_FILE_HEADER[1] &&
|
+ "You are calling the part of POI that deals with OLE2 Office Documents. "
|
||||||
_data[2] == OOXML_FILE_HEADER[2] &&
|
+ "You need to call a different part of POI to process this data (eg XSSF instead of HSSF)");
|
||||||
_data[3] == OOXML_FILE_HEADER[3]) {
|
|
||||||
throw new OfficeXmlFileException("The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] RAW_XML_FILE_HEADER = POIFSConstants.RAW_XML_FILE_HEADER;
|
if (cmp(POIFSConstants.RAW_XML_FILE_HEADER, data)) {
|
||||||
if (_data[0] == RAW_XML_FILE_HEADER[0] &&
|
throw new NotOLE2FileException("The supplied data appears to be a raw XML file. "
|
||||||
_data[1] == RAW_XML_FILE_HEADER[1] &&
|
+ "Formats such as Office 2003 XML are not supported");
|
||||||
_data[2] == RAW_XML_FILE_HEADER[2] &&
|
|
||||||
_data[3] == RAW_XML_FILE_HEADER[3] &&
|
|
||||||
_data[4] == RAW_XML_FILE_HEADER[4]) {
|
|
||||||
throw new NotOLE2FileException("The supplied data appears to be a raw XML file. Formats such as Office 2003 XML are not supported");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_data[0] == 0x09 && _data[1] == 0x00 && // sid=0x0009
|
|
||||||
_data[2] == 0x04 && _data[3] == 0x00 && // size=0x0004
|
|
||||||
_data[4] == 0x00 && _data[5] == 0x00 && // unused
|
|
||||||
(_data[6] == 0x10 || _data[6] == 0x20 || _data[6] == 0x40) &&
|
|
||||||
_data[7] == 0x00) {
|
|
||||||
// BIFF2 raw stream
|
// BIFF2 raw stream
|
||||||
throw new OldExcelFormatException("The supplied data appears to be in BIFF2 format. " +
|
if (cmp(MAGIC_BIFF2, data)) {
|
||||||
"HSSF only supports the BIFF8 format, try OldExcelExtractor");
|
throw new OldExcelFormatException("The supplied data appears to be in BIFF2 format. "
|
||||||
|
+ "HSSF only supports the BIFF8 format, try OldExcelExtractor");
|
||||||
}
|
}
|
||||||
if (_data[0] == 0x09 && _data[1] == 0x02 && // sid=0x0209
|
|
||||||
_data[2] == 0x06 && _data[3] == 0x00 && // size=0x0006
|
|
||||||
_data[4] == 0x00 && _data[5] == 0x00 && // unused
|
|
||||||
(_data[6] == 0x10 || _data[6] == 0x20 || _data[6] == 0x40) &&
|
|
||||||
_data[7] == 0x00) {
|
|
||||||
// BIFF3 raw stream
|
// BIFF3 raw stream
|
||||||
throw new OldExcelFormatException("The supplied data appears to be in BIFF3 format. " +
|
if (cmp(MAGIC_BIFF3, data)) {
|
||||||
"HSSF only supports the BIFF8 format, try OldExcelExtractor");
|
throw new OldExcelFormatException("The supplied data appears to be in BIFF3 format. "
|
||||||
|
+ "HSSF only supports the BIFF8 format, try OldExcelExtractor");
|
||||||
}
|
}
|
||||||
if (_data[0] == 0x09 && _data[1] == 0x04 && // sid=0x0409
|
|
||||||
_data[2] == 0x06 && _data[3] == 0x00 && // size=0x0006
|
|
||||||
_data[4] == 0x00 && _data[5] == 0x00) { // unused
|
|
||||||
if (((_data[6] == 0x10 || _data[6] == 0x20 || _data[6] == 0x40) &&
|
|
||||||
_data[7] == 0x00) ||
|
|
||||||
(_data[6] == 0x00 && _data[7] == 0x01)) {
|
|
||||||
// BIFF4 raw stream
|
// BIFF4 raw stream
|
||||||
throw new OldExcelFormatException("The supplied data appears to be in BIFF4 format. " +
|
if (cmp(MAGIC_BIFF4a, data) || cmp(MAGIC_BIFF4b, data)) {
|
||||||
"HSSF only supports the BIFF8 format, try OldExcelExtractor");
|
throw new OldExcelFormatException("The supplied data appears to be in BIFF4 format. "
|
||||||
}
|
+ "HSSF only supports the BIFF8 format, try OldExcelExtractor");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Give a generic error if the OLE2 signature isn't found
|
// Give a generic error if the OLE2 signature isn't found
|
||||||
@ -221,7 +231,7 @@ public final class HeaderBlock implements HeaderBlockConstants {
|
|||||||
new IntegerField(0x34, 0, _data);
|
new IntegerField(0x34, 0, _data);
|
||||||
new IntegerField(0x38, 0x1000, _data);
|
new IntegerField(0x38, 0x1000, _data);
|
||||||
|
|
||||||
// Initialise the variables
|
// Initialize the variables
|
||||||
_bat_count = 0;
|
_bat_count = 0;
|
||||||
_sbat_count = 0;
|
_sbat_count = 0;
|
||||||
_xbat_count = 0;
|
_xbat_count = 0;
|
||||||
@ -394,9 +404,7 @@ public final class HeaderBlock implements HeaderBlockConstants {
|
|||||||
* @exception IOException on problems writing to the specified
|
* @exception IOException on problems writing to the specified
|
||||||
* stream
|
* stream
|
||||||
*/
|
*/
|
||||||
void writeData(final OutputStream stream)
|
void writeData(final OutputStream stream) throws IOException {
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
// Update the counts and start positions
|
// Update the counts and start positions
|
||||||
new IntegerField(_bat_count_offset, _bat_count, _data);
|
new IntegerField(_bat_count_offset, _bat_count, _data);
|
||||||
new IntegerField(_property_start_offset, _property_start, _data);
|
new IntegerField(_property_start_offset, _property_start, _data);
|
||||||
@ -413,4 +421,15 @@ public final class HeaderBlock implements HeaderBlockConstants {
|
|||||||
stream.write(0);
|
stream.write(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean cmp(byte[] magic, byte[] data) {
|
||||||
|
int i=0;
|
||||||
|
for (byte m : magic) {
|
||||||
|
byte d = data[i++];
|
||||||
|
if (!(d == m || (m == 0x70 && (d == 0x10 || d == 0x20 || d == 0x40)))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user