Author: jukka Date: Sat Feb 20 18:30:41 2010 New Revision: 912181 URL: http://svn.apache.org/viewvc?rev=912181&view=rev Log: PDFBOX-626: Reduce the memory impact of the COS object model
Use LinkedHashMap in COSDictionary instead of the previous HashMap + ArrayList approach. Fix remaining generics warnings in org.apache.pdfbox.cos (and in some related classes) Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/Overlay.java pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSDictionary.java pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSDocument.java pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSName.java pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSStream.java pdfbox/trunk/src/main/java/org/apache/pdfbox/encryption/DocumentEncryption.java pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfparser/PDFObjectStreamParser.java pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfparser/PDFStreamParser.java pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfviewer/PDFTreeModel.java pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfwriter/ContentStreamWriter.java pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/COSDictionaryMap.java pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDExtendedGraphicsState.java pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceNAttributes.java pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAppearanceDictionary.java pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java pdfbox/trunk/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/Overlay.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/Overlay.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/Overlay.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/Overlay.java Sat Feb 20 18:30:41 2010 @@ -444,18 +444,13 @@ if( sourceDict != null ) { - Iterator iterKeys = sourceDict.keyList().iterator(); - while (iterKeys.hasNext()) + for (Map.Entry<COSName, COSBase> entry : sourceDict.entrySet()) { - COSName key = (COSName) iterKeys.next(); - COSName mappedKey = (COSName) objectNameMap.get(key.getName()); - if (mappedKey == null) + COSName mappedKey = (COSName) objectNameMap.get(entry.getKey().getName()); + if (mappedKey != null) { - // object not needet - continue; + destDict.setItem(mappedKey, entry.getValue()); } - - destDict.setItem(mappedKey, sourceDict.getItem(key)); } } } Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSDictionary.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSDictionary.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSDictionary.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSDictionary.java Sat Feb 20 18:30:41 2010 @@ -20,11 +20,10 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; - -import java.util.Iterator; +import java.util.Set; import org.apache.pdfbox.exceptions.COSVisitorException; @@ -42,14 +41,11 @@ private static final String PATH_SEPARATOR = "/"; /** - * These are all of the items in the dictionary. + * The name-value pairs of this dictionary. The pairs are kept in the + * order they were added to the dictionary. */ - private Map items = new HashMap(); - - /** - * Used to store original sequence of keys, for testing. - */ - private List keys = new ArrayList(); + private final Map<COSName, COSBase> items = + new LinkedHashMap<COSName, COSBase>(); /** * Constructor. @@ -66,8 +62,7 @@ */ public COSDictionary( COSDictionary dict ) { - items = new HashMap( dict.items ); - keys = new ArrayList( dict.keys ); + items.putAll( dict.items ); } /** @@ -96,22 +91,19 @@ */ public COSName getKeyForValue( Object value ) { - COSName key = null; - Iterator iter = items.entrySet().iterator(); - while( key == null && iter.hasNext() ) + for( Map.Entry<COSName, COSBase> entry : items.entrySet() ) { - Map.Entry next = (Map.Entry)iter.next(); - Object nextValue = next.getValue(); + Object nextValue = entry.getValue(); if( nextValue.equals( value ) || (nextValue instanceof COSObject && ((COSObject)nextValue).getObject().equals( value)) ) { - key = (COSName)next.getKey(); + return entry.getKey(); } } - return key; + return null; } /** @@ -121,7 +113,7 @@ */ public int size() { - return keys.size(); + return items.size(); } /** @@ -130,7 +122,6 @@ public void clear() { items.clear(); - keys.clear(); } /** @@ -203,7 +194,7 @@ */ public COSBase getDictionaryObject( COSName key ) { - COSBase retval = (COSBase)items.get( key ); + COSBase retval = items.get( key ); if( retval instanceof COSObject ) { retval = ((COSObject)retval).getObject(); @@ -230,11 +221,6 @@ } else { - if (!items.containsKey(key)) - { - // insert only if not already there - keys.add(key); - } items.put( key, value ); } } @@ -1200,7 +1186,6 @@ */ public void removeItem( COSName key ) { - keys.remove( key ); items.remove( key ); } @@ -1213,23 +1198,42 @@ */ public COSBase getItem( COSName key ) { - return (COSBase)items.get( key ); + return items.get( key ); } - - - - /** * This will get the keys for all objects in the dictionary in the sequence that * they were added. * + * @deprecated Use the {...@link #entrySet()} method instead. * @return a list of the keys in the sequence of insertion + */ + public List<COSName> keyList() + { + return new ArrayList<COSName>(items.keySet()); + } + + /** + * Returns the names of the entries in this dictionary. The returned + * set is in the order the entries were added to the dictionary. * + * @since Apache PDFBox 1.1.0 + * @return names of the entries in this dictionary */ - public List keyList() + public Set<COSName> keySet() { - return keys; + return items.keySet(); + } + + /** + * Returns the name-value entries in this dictionary. The returned + * set is in the order the entries were added to the dictionary. + * + * @since Apache PDFBox 1.1.0 + * @return name-value entries in this dictionary + */ + public Set<Map.Entry<COSName, COSBase>> entrySet() { + return items.entrySet(); } /** @@ -1237,7 +1241,7 @@ * * @return All the values for the dictionary. */ - public Collection getValues() + public Collection<COSBase> getValues() { return items.values(); } @@ -1263,19 +1267,17 @@ */ public void addAll( COSDictionary dic ) { - Iterator dicKeys = dic.keyList().iterator(); - while( dicKeys.hasNext() ) + for( Map.Entry<COSName, COSBase> entry : dic.entrySet() ) { - COSName key = (COSName)dicKeys.next(); - COSBase value = dic.getItem( key ); /* * If we're at a second trailer, we have a linearized * pdf file, meaning that the first Size entry represents * all of the objects so we don't need to grab the second. */ - if(!key.getName().equals("Size") || !keys.contains(COSName.getPDFName("Size"))) + if(!entry.getKey().getName().equals("Size") + || !items.containsKey(COSName.getPDFName("Size"))) { - setItem( key, value ); + setItem( entry.getKey(), entry.getValue() ); } } } @@ -1289,14 +1291,11 @@ */ public void mergeInto( COSDictionary dic ) { - Iterator dicKeys = dic.keyList().iterator(); - while( dicKeys.hasNext() ) + for( Map.Entry<COSName, COSBase> entry : dic.entrySet() ) { - COSName key = (COSName)dicKeys.next(); - COSBase value = dic.getItem( key ); - if( getItem( key ) == null ) + if( getItem( entry.getKey() ) == null ) { - setItem( key, value ); + setItem( entry.getKey(), entry.getValue() ); } } } @@ -1335,14 +1334,13 @@ */ public String toString() { - String retVal = "COSDictionary{"; - for (int i = 0; i<size(); i++) + StringBuilder retVal = new StringBuilder("COSDictionary{"); + for( COSName key : items.keySet() ) { - COSName key = (COSName)keyList().get(i); - retVal = retVal + "(" + key + ":" + getDictionaryObject(key).toString() + ") "; + retVal.append("(" + key + ":" + getDictionaryObject(key).toString() + ") "); } - retVal = retVal + "}"; - return retVal; + retVal.append("}"); + return retVal.toString(); } Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSDocument.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSDocument.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSDocument.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSDocument.java Sat Feb 20 18:30:41 2010 @@ -21,7 +21,6 @@ import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; @@ -56,12 +55,14 @@ * Maps ObjectKeys to a COSObject. Note that references to these objects * are also stored in COSDictionary objects that map a name to a specific object. */ - private Map objectPool = new HashMap(); + private final Map<COSObjectKey, COSObject> objectPool = + new HashMap<COSObjectKey, COSObject>(); /** * Maps object and generation ids to object byte offsets. */ - private Map xrefTable = new HashMap(); + private final Map<COSObjectKey, Integer> xrefTable = + new HashMap<COSObjectKey, Integer>(); /** * Document trailer dictionary. @@ -150,11 +151,8 @@ */ public COSObject getObjectByType( COSName type ) throws IOException { - COSObject retval = null; - Iterator iter = objectPool.values().iterator(); - while( iter.hasNext() && retval == null) + for( COSObject object : objectPool.values() ) { - COSObject object = (COSObject)iter.next(); COSBase realObject = object.getObject(); if( realObject instanceof COSDictionary ) @@ -165,7 +163,7 @@ COSName objectType = (COSName)dic.getItem( COSName.TYPE ); if( objectType != null && objectType.equals( type ) ) { - retval = object; + return object; } } catch (ClassCastException e) @@ -174,7 +172,7 @@ } } } - return retval; + return null; } /** @@ -185,7 +183,7 @@ * @return This will return an object with the specified type. * @throws IOException If there is an error getting the object */ - public List getObjectsByType( String type ) throws IOException + public List<COSObject> getObjectsByType( String type ) throws IOException { return getObjectsByType( COSName.getPDFName( type ) ); } @@ -198,14 +196,11 @@ * @return This will return an object with the specified type. * @throws IOException If there is an error getting the object */ - public List getObjectsByType( COSName type ) throws IOException + public List<COSObject> getObjectsByType( COSName type ) throws IOException { - List retval = new ArrayList(); - Iterator iter = objectPool.values().iterator(); - while( iter.hasNext() ) + List<COSObject> retval = new ArrayList<COSObject>(); + for( COSObject object : objectPool.values() ) { - COSObject object = (COSObject)iter.next(); - COSBase realObject = object.getObject(); if( realObject instanceof COSDictionary ) { @@ -232,10 +227,8 @@ */ public void print() { - Iterator iter = objectPool.values().iterator(); - while( iter.hasNext() ) + for( COSObject object : objectPool.values() ) { - COSObject object = (COSObject)iter.next(); System.out.println( object); } } @@ -341,9 +334,9 @@ * * @return A list of all objects. */ - public List getObjects() + public List<COSObject> getObjects() { - return new ArrayList(objectPool.values()); + return new ArrayList<COSObject>(objectPool.values()); } /** @@ -449,17 +442,13 @@ */ public void dereferenceObjectStreams() throws IOException { - Iterator objStm = getObjectsByType( "ObjStm" ).iterator(); - while( objStm.hasNext() ) + for( COSObject objStream : getObjectsByType( "ObjStm" ) ) { - COSObject objStream = (COSObject)objStm.next(); COSStream stream = (COSStream)objStream.getObject(); PDFObjectStreamParser parser = new PDFObjectStreamParser( stream, this ); parser.parse(); - Iterator compressedObjects = parser.getObjects().iterator(); - while( compressedObjects.hasNext() ) + for( COSObject next : parser.getObjects() ) { - COSObject next = (COSObject)compressedObjects.next(); COSObjectKey key = new COSObjectKey( next ); COSObject obj = getObjectFromPool( key ); obj.setObject( next.getObject() ); @@ -505,7 +494,7 @@ */ public void setXRef(COSObjectKey objKey, int offset) { - xrefTable.put(objKey, new Integer(offset)); + xrefTable.put(objKey, offset); } /** @@ -513,7 +502,7 @@ * to byte offsets in the file. * @return mapping of ObjectsKeys to byte offsets */ - public Map getXrefTable() + public Map<COSObjectKey, Integer> getXrefTable() { return xrefTable; } @@ -528,10 +517,8 @@ public void parseXrefStreams() throws IOException { COSDictionary trailerDict = new COSDictionary(); - Iterator xrefIter = getObjectsByType( "XRef" ).iterator(); - while( xrefIter.hasNext() ) + for( COSObject xrefStream : getObjectsByType( "XRef" ) ) { - COSObject xrefStream = (COSObject)xrefIter.next(); COSStream stream = (COSStream)xrefStream.getObject(); trailerDict.addAll(stream); PDFXrefStreamParser parser = new PDFXrefStreamParser(stream, this); Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSName.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSName.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSName.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSName.java Sat Feb 20 18:30:41 2010 @@ -34,20 +34,21 @@ * @author <a href="mailto:b...@benlitchfield.com">Ben Litchfield</a> * @version $Revision: 1.42 $ */ -public final class COSName extends COSBase implements Comparable +public final class COSName extends COSBase implements Comparable<COSName> { /** * Note: This is synchronized because a HashMap must be synchronized if accessed by * multiple threads. */ - private static Map nameMap = Collections.synchronizedMap( new WeakHashMap(8192) ); - + private static Map<String, COSName> nameMap = + Collections.synchronizedMap( new WeakHashMap<String, COSName>(8192) ); + /** * All common COSName values are stored in a simple HashMap. They are already defined as * static constants and don't need to be synchronized for multithreaded environments. */ - private static Map commonNameMap = new HashMap(); - + private static Map<String, COSName> commonNameMap = + new HashMap<String, COSName>(); /** * A common COSName value. @@ -523,11 +524,11 @@ if( aName != null ) { // Is it a common COSName ?? - name = (COSName)commonNameMap.get( aName ); + name = commonNameMap.get( aName ); if( name == null ) { // It seems to be a document specific COSName - name = (COSName)nameMap.get( aName ); + name = nameMap.get( aName ); if( name == null ) { //name is added to the synchronized map in the constructor @@ -614,9 +615,8 @@ /** * {...@inheritdoc} */ - public int compareTo(Object o) + public int compareTo(COSName other) { - COSName other = (COSName)o; return this.name.compareTo( other.name ); } Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSStream.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSStream.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSStream.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSStream.java Sat Feb 20 18:30:41 2010 @@ -113,7 +113,7 @@ * * @throws IOException If there is an error parsing the stream. */ - public List getStreamTokens() throws IOException + public List<Object> getStreamTokens() throws IOException { PDFStreamParser parser = new PDFStreamParser( this ); parser.parse(); Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/encryption/DocumentEncryption.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/encryption/DocumentEncryption.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/encryption/DocumentEncryption.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/encryption/DocumentEncryption.java Sat Feb 20 18:30:41 2010 @@ -26,6 +26,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Set; import org.apache.pdfbox.cos.COSArray; @@ -350,18 +351,15 @@ private void decryptDictionary( COSDictionary dictionary, long objNum, long genNum ) throws CryptographyException, IOException { - Iterator keys = dictionary.keyList().iterator(); - while( keys.hasNext() ) + for( Map.Entry<COSName, COSBase> entry : dictionary.entrySet() ) { - COSName key = (COSName)keys.next(); - Object value = dictionary.getItem( key ); //if we are a signature dictionary and contain a Contents entry then //we don't decrypt it. - if( !(key.getName().equals( "Contents" ) && - value instanceof COSString && + if( !(entry.getKey().getName().equals( "Contents" ) && + entry.getValue() instanceof COSString && potentialSignatures.contains( dictionary ))) { - decrypt( value, objNum, genNum ); + decrypt( entry.getValue(), objNum, genNum ); } } } Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfparser/PDFObjectStreamParser.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfparser/PDFObjectStreamParser.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfparser/PDFObjectStreamParser.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfparser/PDFObjectStreamParser.java Sat Feb 20 18:30:41 2010 @@ -43,8 +43,8 @@ private static final Log log = LogFactory.getLog(PDFObjectStreamParser.class); - private List streamObjects = null; - private List objectNumbers = null; + private List<COSObject> streamObjects = null; + private List<Integer> objectNumbers = null; private COSStream stream; /** @@ -74,8 +74,8 @@ { //need to first parse the header. int numberOfObjects = stream.getInt( "N" ); - objectNumbers = new ArrayList( numberOfObjects ); - streamObjects = new ArrayList( numberOfObjects ); + objectNumbers = new ArrayList<Integer>( numberOfObjects ); + streamObjects = new ArrayList<COSObject>( numberOfObjects ); for( int i=0; i<numberOfObjects; i++ ) { int objectNumber = readInt(); @@ -90,7 +90,7 @@ object = new COSObject(cosObject); object.setGenerationNumber( COSInteger.ZERO ); COSInteger objNum = - new COSInteger( ((Integer)objectNumbers.get( objectCounter)).intValue() ); + new COSInteger( objectNumbers.get( objectCounter).intValue() ); object.setObjectNumber( objNum ); streamObjects.add( object ); if(log.isDebugEnabled()) @@ -111,7 +111,7 @@ * * @return All of the objects in the stream. */ - public List getObjects() + public List<COSObject> getObjects() { return streamObjects; } Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfparser/PDFStreamParser.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfparser/PDFStreamParser.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfparser/PDFStreamParser.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfparser/PDFStreamParser.java Sat Feb 20 18:30:41 2010 @@ -45,7 +45,7 @@ */ public class PDFStreamParser extends BaseParser { - private List streamObjects = new ArrayList( 100 ); + private List<Object> streamObjects = new ArrayList<Object>( 100 ); private RandomAccess file; private PDFOperator lastBIToken = null; @@ -115,7 +115,7 @@ * * @return All of the tokens in the stream. */ - public List getTokens() + public List<Object> getTokens() { return streamObjects; } Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfviewer/PDFTreeModel.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfviewer/PDFTreeModel.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfviewer/PDFTreeModel.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfviewer/PDFTreeModel.java Sat Feb 20 18:30:41 2010 @@ -39,6 +39,7 @@ import org.apache.pdfbox.pdmodel.PDDocument; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -119,7 +120,7 @@ else if( parent instanceof COSDictionary ) { COSDictionary dict = ((COSDictionary)parent); - List keys = dict.keyList(); + List<COSName> keys = new ArrayList<COSName>(dict.keySet()); Collections.sort( keys ); Object key = keys.get( index ); Object value = dict.getDictionaryObject( (COSName)key ); @@ -222,7 +223,7 @@ { MapEntry entry = (MapEntry)child; COSDictionary dict = (COSDictionary)parent; - List keys = dict.keyList(); + List<COSName> keys = new ArrayList<COSName>(dict.keySet()); Collections.sort( keys ); for( int i=0; retval == -1 && i<keys.size(); i++ ) { Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java Sat Feb 20 18:30:41 2010 @@ -700,13 +700,12 @@ { getStandardOutput().write(DICT_OPEN); getStandardOutput().writeEOL(); - for (Iterator i = obj.keyList().iterator(); i.hasNext();) + for (Map.Entry<COSName, COSBase> entry : obj.entrySet()) { - COSName name = (COSName) i.next(); - COSBase value = obj.getItem(name); + COSBase value = entry.getValue(); if (value != null) { - name.accept(this); + entry.getKey().accept(this); getStandardOutput().write(SPACE); if( value instanceof COSDictionary ) { Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfwriter/ContentStreamWriter.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfwriter/ContentStreamWriter.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfwriter/ContentStreamWriter.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfwriter/ContentStreamWriter.java Sat Feb 20 18:30:41 2010 @@ -21,6 +21,7 @@ import java.util.Iterator; import java.util.List; +import java.util.Map; import org.apache.pdfbox.cos.COSArray; import org.apache.pdfbox.cos.COSBase; @@ -121,19 +122,14 @@ { COSDictionary obj = (COSDictionary)o; output.write( COSWriter.DICT_OPEN ); - for (Iterator i = obj.keyList().iterator(); i.hasNext();) + for (Map.Entry<COSName, COSBase> entry : obj.entrySet()) { - COSName name = (COSName) i.next(); - COSBase value = obj.getItem(name); - if (value != null) + if (entry.getValue() != null) { - writeObject( name ); + writeObject( entry.getKey() ); output.write( SPACE ); - - writeObject( value ); - + writeObject( entry.getValue() ); output.write( SPACE ); - } } output.write( COSWriter.DICT_CLOSE ); @@ -147,10 +143,8 @@ output.write( "BI".getBytes() ); ImageParameters params = op.getImageParameters(); COSDictionary dic = params.getDictionary(); - Iterator iter = dic.keyList().iterator(); - while( iter.hasNext() ) + for( COSName key : dic.keySet() ) { - COSName key = (COSName)iter.next(); Object value = dic.getDictionaryObject( key ); key.writePDF( output ); output.write( SPACE ); Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/PDResources.java Sat Feb 20 18:30:41 2010 @@ -104,10 +104,8 @@ Map actuals = new HashMap(); retval = new COSDictionaryMap( actuals, fonts ); - Iterator fontNames = fonts.keyList().iterator(); - while( fontNames.hasNext() ) + for( COSName fontName : fonts.keySet() ) { - COSName fontName = (COSName)fontNames.next(); COSBase font = fonts.getDictionaryObject( fontName ); //data-000174.pdf contains a font that is a COSArray, looks to be an error in the //PDF, we will just ignore entries that are not dictionaries. @@ -153,10 +151,8 @@ Map actuals = new HashMap(); retval = new COSDictionaryMap( actuals, xobjects ); - Iterator imageNames = xobjects.keyList().iterator(); - while( imageNames.hasNext() ) + for( COSName objName : xobjects.keySet() ) { - COSName objName = (COSName)imageNames.next(); COSBase cosObject = xobjects.getDictionaryObject(objName); PDXObject xobject = PDXObject.createXObject( cosObject ); if( xobject !=null ) @@ -190,10 +186,8 @@ Map actuals = new HashMap(); retval = new COSDictionaryMap( actuals, images ); - Iterator imageNames = images.keyList().iterator(); - while( imageNames.hasNext() ) + for( COSName imageName : images.keySet() ) { - COSName imageName = (COSName)imageNames.next(); COSStream image = (COSStream)(images.getDictionaryObject(imageName)); COSName subType =(COSName)image.getDictionaryObject(COSName.SUBTYPE); @@ -237,10 +231,8 @@ { Map actuals = new HashMap(); retval = new COSDictionaryMap( actuals, colorspaces ); - Iterator csNames = colorspaces.keyList().iterator(); - while( csNames.hasNext() ) + for( COSName csName : colorspaces.keySet() ) { - COSName csName = (COSName)csNames.next(); COSBase cs = colorspaces.getDictionaryObject( csName ); actuals.put( csName.getName(), PDColorSpaceFactory.createColorSpace( cs ) ); } @@ -274,10 +266,8 @@ { Map actuals = new HashMap(); retval = new COSDictionaryMap( actuals, states ); - Iterator names = states.keyList().iterator(); - while( names.hasNext() ) + for( COSName name : states.keySet() ) { - COSName name = (COSName)names.next(); COSDictionary dictionary = (COSDictionary)states.getDictionaryObject( name ); actuals.put( name.getName(), new PDExtendedGraphicsState( dictionary ) ); } Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/COSDictionaryMap.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/COSDictionaryMap.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/COSDictionaryMap.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/common/COSDictionaryMap.java Sat Feb 20 18:30:41 2010 @@ -77,7 +77,7 @@ */ public boolean containsKey(Object key) { - return map.keyList().contains( key ); + return map.keySet().contains( key ); } /** @@ -222,10 +222,8 @@ if( map != null ) { Map actualMap = new HashMap(); - Iterator keyIter = map.keyList().iterator(); - while( keyIter.hasNext() ) + for( COSName key : map.keySet() ) { - COSName key = (COSName)keyIter.next(); COSBase cosObj = map.getDictionaryObject( key ); Object actualObject = null; if( cosObj instanceof COSString ) Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java Sat Feb 20 18:30:41 2010 @@ -27,6 +27,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Set; import org.apache.pdfbox.cos.COSArray; @@ -306,18 +307,15 @@ private void decryptDictionary( COSDictionary dictionary, long objNum, long genNum ) throws CryptographyException, IOException { - Iterator keys = dictionary.keyList().iterator(); - while( keys.hasNext() ) + for( Map.Entry<COSName, COSBase> entry : dictionary.entrySet() ) { - COSName key = (COSName)keys.next(); - Object value = dictionary.getItem( key ); //if we are a signature dictionary and contain a Contents entry then //we don't decrypt it. - if( !(key.getName().equals( "Contents" ) && - value instanceof COSString && + if( !(entry.getKey().getName().equals( "Contents" ) && + entry.getValue() instanceof COSString && potentialSignatures.contains( dictionary ))) { - decrypt( value, objNum, genNum ); + decrypt( entry.getValue(), objNum, genNum ); } } } Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDExtendedGraphicsState.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDExtendedGraphicsState.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDExtendedGraphicsState.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/graphics/PDExtendedGraphicsState.java Sat Feb 20 18:30:41 2010 @@ -104,10 +104,8 @@ */ public void copyIntoGraphicsState( PDGraphicsState gs ) throws IOException { - Iterator keys = graphicsState.keyList().iterator(); - while( keys.hasNext() ) + for( COSName key : graphicsState.keySet() ) { - COSName key = (COSName)keys.next(); if( key.equals( LW ) ) { gs.setLineWidth( getLineWidth().doubleValue() ); Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceNAttributes.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceNAttributes.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceNAttributes.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDDeviceNAttributes.java Sat Feb 20 18:30:41 2010 @@ -84,10 +84,8 @@ colorants = new COSDictionary(); dictionary.setItem( COSName.getPDFName( "Colorants" ), colorants ); } - Iterator iter = colorants.keyList().iterator(); - while( iter.hasNext() ) + for( COSName name : colorants.keySet() ) { - COSName name = (COSName)iter.next(); COSBase value = colorants.getDictionaryObject( name ); actuals.put( name.getName(), PDColorSpaceFactory.createColorSpace( value ) ); } Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAppearanceDictionary.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAppearanceDictionary.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAppearanceDictionary.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAppearanceDictionary.java Sat Feb 20 18:30:41 2010 @@ -25,7 +25,6 @@ import org.apache.pdfbox.pdmodel.common.COSDictionaryMap; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; /** @@ -93,12 +92,10 @@ ((COSDictionary)ap).setItem(COSName.getPDFName( "default" ), aux ); } COSDictionary map = (COSDictionary)ap; - Map actuals = new HashMap(); + Map<String, PDAppearanceStream> actuals = new HashMap<String, PDAppearanceStream>(); Map retval = new COSDictionaryMap( actuals, map ); - Iterator asNames = map.keyList().iterator(); - while( asNames.hasNext() ) + for( COSName asName : map.keySet() ) { - COSName asName = (COSName)asNames.next(); COSStream as = (COSStream)map.getDictionaryObject( asName ); actuals.put( asName.getName(), new PDAppearanceStream( as ) ); } @@ -154,12 +151,10 @@ ((COSDictionary)ap).setItem(COSName.getPDFName( "default" ), aux ); } COSDictionary map = (COSDictionary)ap; - Map actuals = new HashMap(); + Map<String, PDAppearanceStream> actuals = new HashMap<String, PDAppearanceStream>(); retval = new COSDictionaryMap( actuals, map ); - Iterator asNames = map.keyList().iterator(); - while( asNames.hasNext() ) + for( COSName asName : map.keySet() ) { - COSName asName = (COSName)asNames.next(); COSStream as = (COSStream)map.getDictionaryObject( asName ); actuals.put( asName.getName(), new PDAppearanceStream( as ) ); } @@ -205,12 +200,11 @@ ((COSDictionary)ap).setItem(COSName.getPDFName( "default" ), aux ); } COSDictionary map = (COSDictionary)ap; - Map actuals = new HashMap(); + Map<String, PDAppearanceStream> actuals = + new HashMap<String, PDAppearanceStream>(); retval = new COSDictionaryMap( actuals, map ); - Iterator asNames = map.keyList().iterator(); - while( asNames.hasNext() ) + for( COSName asName : map.keySet() ) { - COSName asName = (COSName)asNames.next(); COSStream as = (COSStream)map.getDictionaryObject( asName ); actuals.put( asName.getName(), new PDAppearanceStream( as ) ); } Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java Sat Feb 20 18:30:41 2010 @@ -21,8 +21,6 @@ import org.apache.pdfbox.cos.COSName; import java.io.IOException; -import java.util.Iterator; -import java.util.List; /** * A class for handling the PDF field as a checkbox. @@ -54,10 +52,8 @@ if( n instanceof COSDictionary ) { - List li = ((COSDictionary)n).keyList(); - for( int i=0; i<li.size(); i++ ) + for( COSName name : ((COSDictionary)n).keySet() ) { - COSName name = (COSName)li.get( i ); if( !name.equals( OFF_VALUE )) { value = name; @@ -146,13 +142,11 @@ //N can be a COSDictionary or a COSStream if( n instanceof COSDictionary ) { - Iterator li = ((COSDictionary)n).keyList().iterator(); - while( li.hasNext() ) + for( COSName key :((COSDictionary)n).keySet() ) { - Object key = li.next(); if( !key.equals( OFF_VALUE) ) { - retval = ((COSName)key).getName(); + retval = key.getName(); } } } Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java?rev=912181&r1=912180&r2=912181&view=diff ============================================================================== --- pdfbox/trunk/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java (original) +++ pdfbox/trunk/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java Sat Feb 20 18:30:41 2010 @@ -367,26 +367,26 @@ else if( base instanceof COSStream ) { COSStream originalStream = (COSStream)base; - List keys = originalStream.keyList(); PDStream stream = new PDStream( destination, originalStream.getFilteredStream(), true ); clonedVersion.put( base, stream.getStream() ); - for( int i=0; i<keys.size(); i++ ) + for( Map.Entry<COSName, COSBase> entry : originalStream.entrySet() ) { - COSName key = (COSName)keys.get( i ); - stream.getStream().setItem( key, cloneForNewDocument(destination,originalStream.getItem(key))); + stream.getStream().setItem( + entry.getKey(), + cloneForNewDocument(destination, entry.getValue())); } retval = stream.getStream(); } else if( base instanceof COSDictionary ) { COSDictionary dic = (COSDictionary)base; - List keys = dic.keyList(); retval = new COSDictionary(); clonedVersion.put( base, retval ); - for( int i=0; i<keys.size(); i++ ) + for( Map.Entry<COSName, COSBase> entry : dic.entrySet() ) { - COSName key = (COSName)keys.get( i ); - ((COSDictionary)retval).setItem( key, cloneForNewDocument(destination,dic.getItem(key))); + ((COSDictionary)retval).setItem( + entry.getKey(), + cloneForNewDocument(destination, entry.getValue())); } } else @@ -451,13 +451,13 @@ { // does that make sense??? COSStream originalStream = (COSStream)base; - List keys = originalStream.keyList(); PDStream stream = new PDStream( destination, originalStream.getFilteredStream(), true ); clonedVersion.put( base, stream.getStream() ); - for( int i=0; i<keys.size(); i++ ) + for( Map.Entry<COSName, COSBase> entry : originalStream.entrySet() ) { - COSName key = (COSName)keys.get( i ); - stream.getStream().setItem( key, cloneForNewDocument(destination,originalStream.getItem(key))); + stream.getStream().setItem( + entry.getKey(), + cloneForNewDocument(destination, entry.getValue())); } retval = stream.getStream(); target = retval; @@ -465,18 +465,18 @@ else if( base instanceof COSDictionary ) { COSDictionary dic = (COSDictionary)base; - List keys = dic.keyList(); clonedVersion.put( base, retval ); - for( int i=0; i<keys.size(); i++ ) + for( Map.Entry<COSName, COSBase> entry : dic.entrySet() ) { - COSName key = (COSName)keys.get( i ); + COSName key = entry.getKey(); + COSBase value = entry.getValue(); if (((COSDictionary)target).getItem(key)!=null) { - cloneMerge(destination, dic.getItem(key),((COSDictionary)target).getItem(key)); + cloneMerge(destination, value,((COSDictionary)target).getItem(key)); } else { - ((COSDictionary)target).setItem( key, cloneForNewDocument(destination,dic.getItem(key))); + ((COSDictionary)target).setItem( key, cloneForNewDocument(destination, value)); } } }