You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by se...@apache.org on 2011/07/11 20:37:29 UTC
svn commit: r1145277 -
/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmBuffer.java
Author: sergey
Date: Mon Jul 11 18:37:28 2011
New Revision: 1145277
URL: http://svn.apache.org/viewvc?rev=1145277&view=rev
Log:
allow SprmBuffer to append byte[] (public method)
Modified:
poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmBuffer.java
Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmBuffer.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmBuffer.java?rev=1145277&r1=1145276&r2=1145277&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmBuffer.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmBuffer.java Mon Jul 11 18:37:28 2011
@@ -17,10 +17,10 @@
package org.apache.poi.hwpf.sprm;
-import org.apache.poi.util.LittleEndian;
-
import java.util.Arrays;
+import org.apache.poi.util.LittleEndian;
+
public final class SprmBuffer
implements Cloneable
{
@@ -44,24 +44,33 @@ public final class SprmBuffer
_offset = 0;
}
- private int findSprm(short opcode)
- {
- int operation = SprmOperation.getOperationFromOpcode(opcode);
- int type = SprmOperation.getTypeFromOpcode(opcode);
+ public SprmOperation findSprm( short opcode )
+ {
+ int operation = SprmOperation.getOperationFromOpcode( opcode );
+ int type = SprmOperation.getTypeFromOpcode( opcode );
+
+ SprmIterator si = new SprmIterator( _buf, 2 );
+ while ( si.hasNext() )
+ {
+ SprmOperation i = si.next();
+ if ( i.getOperation() == operation && i.getType() == type )
+ return i;
+ }
+ return null;
+ }
- SprmIterator si = new SprmIterator(_buf, 2);
- while(si.hasNext())
+ private int findSprmOffset( short opcode )
{
- SprmOperation i = si.next();
- if(i.getOperation() == operation && i.getType() == type)
- return i.getGrpprlOffset();
+ SprmOperation sprmOperation = findSprm( opcode );
+ if ( sprmOperation == null )
+ return -1;
+
+ return sprmOperation.getGrpprlOffset();
}
- return -1;
- }
public void updateSprm(short opcode, byte operand)
{
- int grpprlOffset = findSprm(opcode);
+ int grpprlOffset = findSprmOffset(opcode);
if(grpprlOffset != -1)
{
_buf[grpprlOffset] = operand;
@@ -72,7 +81,7 @@ public final class SprmBuffer
public void updateSprm(short opcode, short operand)
{
- int grpprlOffset = findSprm(opcode);
+ int grpprlOffset = findSprmOffset(opcode);
if(grpprlOffset != -1)
{
LittleEndian.putShort(_buf, grpprlOffset, operand);
@@ -83,7 +92,7 @@ public final class SprmBuffer
public void updateSprm(short opcode, int operand)
{
- int grpprlOffset = findSprm(opcode);
+ int grpprlOffset = findSprmOffset(opcode);
if(grpprlOffset != -1)
{
LittleEndian.putInt(_buf, grpprlOffset, operand);
@@ -139,11 +148,17 @@ public final class SprmBuffer
return (Arrays.equals(_buf, sprmBuf._buf));
}
- public void append(byte[] grpprl)
- {
- ensureCapacity(grpprl.length);
- System.arraycopy(grpprl, 0, _buf, _offset, grpprl.length);
- }
+ public void append( byte[] grpprl )
+ {
+ append( grpprl, 0 );
+ }
+
+ public void append( byte[] grpprl, int offset )
+ {
+ ensureCapacity( grpprl.length - offset );
+ System.arraycopy( grpprl, offset, _buf, _offset, grpprl.length - offset );
+ _offset += grpprl.length - offset;
+ }
public Object clone()
throws CloneNotSupportedException
@@ -154,14 +169,18 @@ public final class SprmBuffer
return retVal;
}
- private void ensureCapacity(int addition)
- {
- if (_offset + addition >= _buf.length)
+ private void ensureCapacity( int addition )
{
- // add 6 more than they need for use the next iteration
- byte[] newBuf = new byte[_offset + addition + 6];
- System.arraycopy(_buf, 0, newBuf, 0, _buf.length);
- _buf = newBuf;
+ if ( _offset + addition >= _buf.length )
+ {
+ // add 6 more than they need for use the next iteration
+ //
+ // commented - buffer shall not contain any additional bytes --
+ // sergey
+ // byte[] newBuf = new byte[_offset + addition + 6];
+ byte[] newBuf = new byte[_offset + addition];
+ System.arraycopy( _buf, 0, newBuf, 0, _buf.length );
+ _buf = newBuf;
+ }
}
- }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org