Java Code Examples for org.apache.poi.util.LittleEndian#getShort()

The following examples show how to use org.apache.poi.util.LittleEndian#getShort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BiffViewer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void fillNextBuffer() throws IOException {
	if (_innerHasReachedEOF) {
		return;
	}
	int b0 = _is.read();
	if (b0 == -1) {
		_innerHasReachedEOF = true;
		return;
	}
	_data[0] = (byte) b0;
	_is.readFully(_data, 1, 3);
	int len = LittleEndian.getShort(_data, 2);
	_is.readFully(_data, 4, len);
	_currentPos = 0;
	_currentSize = len + 4;
	_recordCounter++;
}
 
Example 2
Source File: EscherArrayProperty.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * We have this method because the way in which arrays in escher works
 * is screwed for seemly arbitrary reasons.  While most properties are
 * fairly consistent and have a predictable array size, escher arrays
 * have special cases.
 *
 * @param data      The data array containing the escher array information
 * @param offset    The offset into the array to start reading from.
 * @return  the number of bytes used by this complex property.
 */
public int setArrayData(byte[] data, int offset) {
    if (emptyComplexPart){
        setComplexData(new byte[0]);
    } else {
        short numElements = LittleEndian.getShort(data, offset);
        // LittleEndian.getShort(data, offset + 2); // numReserved
        short sizeOfElements = LittleEndian.getShort(data, offset + 4);

        // the code here seems to depend on complexData already being
        // sized correctly via the constructor
        int arraySize = getActualSizeOfElements(sizeOfElements) * numElements;
        if (arraySize == getComplexData().length) {
            // The stored data size in the simple block excludes the header size
            setComplexData(new byte[arraySize + 6]);
            sizeIncludesHeaderSize = false;
        }
        System.arraycopy(data, offset, getComplexData(), 0, getComplexData().length );
    }
    return getComplexData().length;
}
 
Example 3
Source File: EscherChildAnchorRecord.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int fillFields(byte[] data, int offset, EscherRecordFactory recordFactory) {
    int bytesRemaining = readHeader( data, offset );
    int pos            = offset + 8;
    int size           = 0;
    switch (bytesRemaining) {
    case 16: // RectStruct
        field_1_dx1 =  LittleEndian.getInt( data, pos + size );size+=4;
        field_2_dy1 =  LittleEndian.getInt( data, pos + size );size+=4;
        field_3_dx2 =  LittleEndian.getInt( data, pos + size );size+=4;
        field_4_dy2 =  LittleEndian.getInt( data, pos + size );size+=4;
        break;
    case 8: // SmallRectStruct
        field_1_dx1 =  LittleEndian.getShort( data, pos + size );size+=2;
        field_2_dy1 =  LittleEndian.getShort( data, pos + size );size+=2;
        field_3_dx2 =  LittleEndian.getShort( data, pos + size );size+=2;
        field_4_dy2 =  LittleEndian.getShort( data, pos + size );size+=2;
        break;
    default:
        throw new RuntimeException("Invalid EscherChildAnchorRecord - neither 8 nor 16 bytes.");
    }
        
    return 8 + size;
}
 
Example 4
Source File: ImageHeaderWMF.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public ImageHeaderWMF(byte[] data, final int off) {
    int offset = off;
    int key = LittleEndian.getInt(data, offset); offset += LittleEndian.INT_SIZE; //header key
    if (key != APMHEADER_KEY) {
        LOG.log(POILogger.WARN, "WMF file doesn't contain a placeable header - ignore parsing");
        handle = 0;
        left = 0;
        top = 0;
        right = 200;
        bottom = 200;
        inch = Units.POINT_DPI; //default resolution is 72 dpi
        reserved = 0;
        return;
    }

    handle = LittleEndian.getUShort(data, offset); offset += LittleEndian.SHORT_SIZE;
    left = LittleEndian.getShort(data, offset); offset += LittleEndian.SHORT_SIZE;
    top = LittleEndian.getShort(data, offset); offset += LittleEndian.SHORT_SIZE;
    right = LittleEndian.getShort(data, offset); offset += LittleEndian.SHORT_SIZE;
    bottom = LittleEndian.getShort(data, offset); offset += LittleEndian.SHORT_SIZE;

    inch = LittleEndian.getUShort(data, offset); offset += LittleEndian.SHORT_SIZE;
    reserved = LittleEndian.getInt(data, offset); offset += LittleEndian.INT_SIZE;

    checksum = LittleEndian.getShort(data, offset); offset += LittleEndian.SHORT_SIZE;
    if (checksum != getChecksum()){
        LOG.log(POILogger.WARN, "WMF checksum does not match the header data");
    }
}
 
Example 5
Source File: NDocumentInputStream.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public short readShort() {
   checkAvaliable(SIZE_SHORT);
   byte[] data = new byte[SIZE_SHORT];
   readFully(data, 0, SIZE_SHORT);
   return LittleEndian.getShort(data);
}
 
Example 6
Source File: BiffViewer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void formatBufferIfAtEndOfRec() {
	if (_currentPos != _currentSize) {
		return;
	}
	int dataSize = _currentSize-4;
	int sid = LittleEndian.getShort(_data, 0);
	int globalOffset = _overallStreamPos-_currentSize;
	_listener.processRecord(globalOffset, _recordCounter, sid, dataSize, _data);
}
 
Example 7
Source File: HSSFPolygon.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return array of x coordinates
 */
public int[] getXPoints() {
    EscherArrayProperty verticesProp = getOptRecord().lookup(EscherProperties.GEOMETRY__VERTICES);
    if (null == verticesProp){
        return new int[]{};
    }
    int []array = new int[verticesProp.getNumberOfElementsInArray()-1];
    for (int i=0; i< verticesProp.getNumberOfElementsInArray()-1; i++){
        byte[] property = verticesProp.getElement(i);
        short x = LittleEndian.getShort(property, 0);
        array[i] = x;
    }
    return array;
}
 
Example 8
Source File: HSSFPolygon.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return array of y coordinates
 */
public int[] getYPoints() {
    EscherArrayProperty verticesProp = getOptRecord().lookup(EscherProperties.GEOMETRY__VERTICES);
    if (null == verticesProp){
        return new int[]{};
    }
    int []array = new int[verticesProp.getNumberOfElementsInArray()-1];
    for (int i=0; i< verticesProp.getNumberOfElementsInArray()-1; i++){
        byte[] property = verticesProp.getElement(i);
        short x = LittleEndian.getShort(property, 2);
        array[i] = x;
    }
    return array;
}
 
Example 9
Source File: HSSFShape.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return the rotation, in degrees, that is applied to a shape.
 */
public int getRotationDegree(){
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    EscherSimpleProperty property = getOptRecord().lookup(EscherProperties.TRANSFORM__ROTATION);
    if (null == property){
        return 0;
    }
    try {
        LittleEndian.putInt(property.getPropertyValue(), bos);
        return LittleEndian.getShort(bos.toByteArray(), 2);
    } catch (IOException e) {
        LOG.log(POILogger.ERROR, "can't determine rotation degree", e);
        return 0;
    }
}
 
Example 10
Source File: Biff8DecryptingStream.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
   public short readShort() {
       if (shouldSkipEncryptionOnCurrentRecord) {
           readPlain(buffer, 0, LittleEndianConsts.SHORT_SIZE);
           return LittleEndian.getShort(buffer);
       } else {
           return ccis.readShort();
       }
}
 
Example 11
Source File: EscherClientAnchorRecord.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int fillFields(byte[] data, int offset, EscherRecordFactory recordFactory) {
    int bytesRemaining = readHeader( data, offset );
    int pos            = offset + 8;
    int size           = 0;

    // Always find 4 two byte entries. Sometimes find 9
    /*if (bytesRemaining == 4) // Word format only 4 bytes
    {
        // Not sure exactly what the format is quite yet, likely a reference to a PLC
    }
    else */
    if (bytesRemaining != 4) // Word format only 4 bytes
    {
        field_1_flag   =  LittleEndian.getShort( data, pos + size );     size += 2;
        field_2_col1   =  LittleEndian.getShort( data, pos + size );     size += 2;
        field_3_dx1    =  LittleEndian.getShort( data, pos + size );     size += 2;
        field_4_row1   =  LittleEndian.getShort( data, pos + size );     size += 2;
        if(bytesRemaining >= 18) {
            field_5_dy1    =  LittleEndian.getShort( data, pos + size );     size += 2;
            field_6_col2   =  LittleEndian.getShort( data, pos + size );     size += 2;
            field_7_dx2    =  LittleEndian.getShort( data, pos + size );     size += 2;
            field_8_row2   =  LittleEndian.getShort( data, pos + size );     size += 2;
            field_9_dy2    =  LittleEndian.getShort( data, pos + size );     size += 2;
            shortRecord = false;
        } else {
            shortRecord = true;
        }
    }
    bytesRemaining -= size;
    remainingData  =  new byte[bytesRemaining];
    System.arraycopy( data, pos + size, remainingData, 0, bytesRemaining );
    return 8 + size + bytesRemaining;
}
 
Example 12
Source File: EscherBSERecord.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int fillFields(byte[] data, int offset, EscherRecordFactory recordFactory) {
    int bytesRemaining = readHeader( data, offset );
    int pos = offset + 8;
    field_1_blipTypeWin32 = data[pos];
    field_2_blipTypeMacOS = data[pos + 1];
    System.arraycopy( data, pos + 2, field_3_uid, 0, 16 );
    field_4_tag = LittleEndian.getShort( data, pos + 18 );
    field_5_size = LittleEndian.getInt( data, pos + 20 );
    field_6_ref = LittleEndian.getInt( data, pos + 24 );
    field_7_offset = LittleEndian.getInt( data, pos + 28 );
    field_8_usage = data[pos + 32];
    field_9_name = data[pos + 33];
    field_10_unused2 = data[pos + 34];
    field_11_unused3 = data[pos + 35];
    bytesRemaining -= 36;

    int bytesRead = 0;
    if (bytesRemaining > 0) {
        // Some older escher formats skip this last record
        field_12_blipRecord = (EscherBlipRecord) recordFactory.createRecord( data, pos + 36 );
        bytesRead = field_12_blipRecord.fillFields( data, pos + 36, recordFactory );
    }
    pos += 36 + bytesRead;
    bytesRemaining -= bytesRead;

    _remainingData = new byte[bytesRemaining];
    System.arraycopy( data, pos, _remainingData, 0, bytesRemaining );
    return bytesRemaining + 8 + 36 + (field_12_blipRecord == null ? 0 : field_12_blipRecord.getRecordSize()) ;

}
 
Example 13
Source File: Ole10Native.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates an instance and fills the fields based on the data in the given buffer.
 *
 * @param data   The buffer containing the Ole10Native record
 * @param offset The start offset of the record in the buffer
 * @throws Ole10NativeException on invalid or unexcepted data format
 */
public Ole10Native(byte[] data, int offset) throws Ole10NativeException {
    int ofs = offset; // current offset, initialized to start
    
    if (data.length < offset + 2) {
        throw new Ole10NativeException("data is too small");
    }
    
    totalSize = LittleEndian.getInt(data, ofs);
    ofs += LittleEndianConsts.INT_SIZE;
    
    mode = EncodingMode.unparsed;
    if (LittleEndian.getShort(data, ofs) == 2) {
        // some files like equations don't have a valid filename,
        // but somehow encode the formula right away in the ole10 header
        if (Character.isISOControl(data[ofs+LittleEndianConsts.SHORT_SIZE])) {
            mode = EncodingMode.compact;
        } else {
            mode = EncodingMode.parsed;
        }
    }

    int dataSize;
    switch (mode) {
    case parsed: {
        flags1 = LittleEndian.getShort(data, ofs);
        
        // structured format
        ofs += LittleEndianConsts.SHORT_SIZE;
    
        int len = getStringLength(data, ofs);
        label = StringUtil.getFromCompressedUnicode(data, ofs, len - 1);
        ofs += len;
        
        len = getStringLength(data, ofs);
        fileName = StringUtil.getFromCompressedUnicode(data, ofs, len - 1);
        ofs += len;

        flags2 = LittleEndian.getShort(data, ofs);
        ofs += LittleEndianConsts.SHORT_SIZE;
        
        unknown1 = LittleEndian.getShort(data, ofs);
        ofs += LittleEndianConsts.SHORT_SIZE;
      
        len = LittleEndian.getInt(data, ofs);
        ofs += LittleEndianConsts.INT_SIZE;
        command = StringUtil.getFromCompressedUnicode(data, ofs, len - 1);
        ofs += len;
        
        if (totalSize < ofs) {
            throw new Ole10NativeException("Invalid Ole10Native");
        }
      
        dataSize = LittleEndian.getInt(data, ofs);
        ofs += LittleEndianConsts.INT_SIZE;
      
        if (dataSize < 0 || totalSize - (ofs - LittleEndianConsts.INT_SIZE) < dataSize) {
            throw new Ole10NativeException("Invalid Ole10Native");
        }
        break;
    }
    case compact:
        flags1 = LittleEndian.getShort(data, ofs);
        ofs += LittleEndianConsts.SHORT_SIZE;
        dataSize = totalSize - LittleEndianConsts.SHORT_SIZE;
        break;
    default:
    case unparsed:
        dataSize = totalSize;
        break;
    }

    if ((long)dataSize + (long)ofs > (long)data.length) { //cast to avoid overflow
        throw new Ole10NativeException("Invalid Ole10Native: declared data length > available data");
    }
    dataBuffer = new byte[dataSize];
    System.arraycopy(data, ofs, dataBuffer, 0, dataSize);
    ofs += dataSize;
}
 
Example 14
Source File: EscherArrayProperty.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public short getSizeOfElements() {
    return (emptyComplexPart) ? 0 : LittleEndian.getShort( getComplexData(), 4 );
}
 
Example 15
Source File: EscherPropertyFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create new properties from a byte array.
 *
 * @param data              The byte array containing the property
 * @param offset            The starting offset into the byte array
 * @param numProperties     The number of properties to be read
 * @return                  The new properties
 */
public List<EscherProperty> createProperties(byte[] data, int offset, short numProperties) {
    List<EscherProperty> results = new ArrayList<EscherProperty>();

    int pos = offset;

    for (int i = 0; i < numProperties; i++) {
        short propId;
        int propData;
        propId = LittleEndian.getShort( data, pos );
        propData = LittleEndian.getInt( data, pos + 2 );
        short propNumber = (short) ( propId & (short) 0x3FFF );
        boolean isComplex = ( propId & (short) 0x8000 ) != 0;
        // boolean isBlipId = ( propId & (short) 0x4000 ) != 0;

        byte propertyType = EscherProperties.getPropertyType(propNumber);
        EscherProperty ep;
        switch (propertyType) {
            case EscherPropertyMetaData.TYPE_BOOLEAN:
                ep = new EscherBoolProperty( propId, propData );
                break;
            case EscherPropertyMetaData.TYPE_RGB:
                ep = new EscherRGBProperty( propId, propData );
                break;
            case EscherPropertyMetaData.TYPE_SHAPEPATH:
                ep = new EscherShapePathProperty( propId, propData );
                break;
            default:
                if ( !isComplex ) {
                    ep = new EscherSimpleProperty( propId, propData );
                } else if ( propertyType == EscherPropertyMetaData.TYPE_ARRAY) {
                    ep = new EscherArrayProperty( propId, new byte[propData]);
                } else {
                    ep = new EscherComplexProperty( propId, new byte[propData]);
                }
                break;
        }
        results.add( ep );
        pos += 6;
    }

    // Get complex data
    for (EscherProperty p : results) {
        if (p instanceof EscherComplexProperty) {
            if (p instanceof EscherArrayProperty) {
                pos += ((EscherArrayProperty)p).setArrayData(data, pos);
            } else {
                byte[] complexData = ((EscherComplexProperty)p).getComplexData();

                int leftover = data.length - pos;
                if (leftover < complexData.length) {
                    throw new IllegalStateException("Could not read complex escher property, length was " + complexData.length + ", but had only " +
                            leftover + " bytes left");
                }

                System.arraycopy(data, pos, complexData, 0, complexData.length);
                pos += complexData.length;
            }
        }
    }
    return results;
}
 
Example 16
Source File: FontFormatting.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private short getShort(int offset) {
    return LittleEndian.getShort( _rawData, offset);
}
 
Example 17
Source File: DConRefRecord.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Read constructor.
 *
 * @param data byte array containing a DConRef Record, including the header.
 */
public DConRefRecord(byte[] data)
{
    int offset = 0;
    if (!(LittleEndian.getShort(data, offset) == DConRefRecord.sid))
        throw new RecordFormatException("incompatible sid.");
    offset += LittleEndian.SHORT_SIZE;

    //length = LittleEndian.getShort(data, offset);
    offset += LittleEndian.SHORT_SIZE;

    firstRow = LittleEndian.getUShort(data, offset);
    offset += LittleEndian.SHORT_SIZE;
    lastRow = LittleEndian.getUShort(data, offset);
    offset += LittleEndian.SHORT_SIZE;
    firstCol = LittleEndian.getUByte(data, offset);
    offset += LittleEndian.BYTE_SIZE;
    lastCol = LittleEndian.getUByte(data, offset);
    offset += LittleEndian.BYTE_SIZE;
    charCount = LittleEndian.getUShort(data, offset);
    offset += LittleEndian.SHORT_SIZE;
    if (charCount < 2)
        throw new RecordFormatException("Character count must be >= 2");

    charType = LittleEndian.getUByte(data, offset);
    offset += LittleEndian.BYTE_SIZE; //7 bits reserved + 1 bit type

    /*
     * bytelength is the length of the string in bytes, which depends on whether the string is
     * made of single- or double-byte chars. This is given by charType, which equals 0 if
     * single-byte, 1 if double-byte.
     */
    int byteLength = charCount * ((charType & 1) + 1);

    path = LittleEndian.getByteArray(data, offset, byteLength);
    offset += byteLength;

    /*
     * If it's a self reference, the last one or two bytes (depending on char type) are the
     * unused field. Not sure If i need to bother with this...
     */
    if (path[0] == 0x02)
        _unused = LittleEndian.getByteArray(data, offset, (charType + 1));

}
 
Example 18
Source File: EscherRecord.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Reads the 8 byte header information and populates the <code>options</code>
 * and <code>recordId</code> records.
 *
 * @param data      the byte array to read from
 * @param offset    the offset to start reading from
 * @return          the number of bytes remaining in this record.  This
 *                  may include the children if this is a container.
 */
protected int readHeader( byte[] data, int offset ) {
    _options = LittleEndian.getShort( data, offset );
    _recordId = LittleEndian.getShort( data, offset + 2 );
    return LittleEndian.getInt( data, offset + 4 );
}
 
Example 19
Source File: EscherRecord.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Read the options field from header and return instance part of it.
 * @param data      the byte array to read from
 * @param offset    the offset to start reading from
 * @return          value of instance part of options field
 */
protected static short readInstance( byte data[], int offset ) {
    final short options = LittleEndian.getShort( data, offset );
    return fInstance.getShortValue( options );
}