You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2014/12/18 22:23:22 UTC

svn commit: r1646537 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java

Author: tilman
Date: Thu Dec 18 21:23:22 2014
New Revision: 1646537

URL: http://svn.apache.org/r1646537
Log:
PDFBOX-2576: refactor: remove double code

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java?rev=1646537&r1=1646536&r2=1646537&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java Thu Dec 18 21:23:22 2014
@@ -231,11 +231,7 @@ public abstract class BaseParser
             long genOffset = pdfSource.getOffset();
             COSBase generationNumber = parseDirObject();
             skipSpaces();
-            char r = (char)pdfSource.read();
-            if( r != 'R' )
-            {
-                throw new IOException("expected='R' actual='" + r + "' at offset " + pdfSource.getOffset());
-            }
+            readExpectedChar('R');
             if (!(number instanceof COSInteger))
             {
                 throw new IOException("expected number, actual=" + number + " at offset " + numOffset);
@@ -264,23 +260,15 @@ public abstract class BaseParser
      */
     protected COSDictionary parseCOSDictionary() throws IOException
     {
-        char c = (char)pdfSource.read();
-        if( c != '<')
-        {
-            throw new IOException( "expected='<' actual='" + c + "' at offset " + pdfSource.getOffset());
-        }
-        c = (char)pdfSource.read();
-        if( c != '<')
-        {
-            throw new IOException( "expected='<' actual='" + c + "' at offset " + pdfSource.getOffset());
-        }
+        readExpectedChar('<');
+        readExpectedChar('<');
         skipSpaces();
         COSDictionary obj = new COSDictionary();
         boolean done = false;
         while( !done )
         {
             skipSpaces();
-            c = (char)pdfSource.peek();
+            char c = (char)pdfSource.peek();
             if( c == '>')
             {
                 done = true;
@@ -358,16 +346,8 @@ public abstract class BaseParser
                 }
             }
         }
-        char ch = (char)pdfSource.read();
-        if( ch != '>' )
-        {
-            throw new IOException( "expected='>' actual='" + ch + "' at offset " + pdfSource.getOffset());
-        }
-        ch = (char)pdfSource.read();
-        if( ch != '>' )
-        {
-            throw new IOException( "expected='>' actual='" + ch + "' at offset " + pdfSource.getOffset());
-        }
+        readExpectedChar('>');
+        readExpectedChar('>');
         return obj;
     }
 
@@ -1002,7 +982,8 @@ public abstract class BaseParser
                 do 
                 {
                     c = pdfSource.read();
-                } while ( c != '>' && c >= 0 );
+                } 
+                while ( c != '>' && c >= 0 );
                 
                 // might have reached EOF while looking for the closing bracket
                 // this can happen for malformed PDFs only. Make sure that there is
@@ -1018,7 +999,7 @@ public abstract class BaseParser
         }
         return COSString.parseHex(sBuf.toString());
     }
-    
+   
     /**
      * This will parse a PDF array object.
      *
@@ -1028,11 +1009,7 @@ public abstract class BaseParser
      */
     protected COSArray parseCOSArray() throws IOException
     {
-        char ch = (char)pdfSource.read();
-        if( ch != '[')
-        {
-            throw new IOException( "expected='[' actual='" + ch + "' at offset " + pdfSource.getOffset());
-        }
+        readExpectedChar('[');
         COSArray po = new COSArray();
         COSBase pbo;
         skipSpaces();
@@ -1111,14 +1088,10 @@ public abstract class BaseParser
      */
     protected COSName parseCOSName() throws IOException
     {
-        int c = pdfSource.read();
-        if( (char)c != '/')
-        {
-            throw new IOException("expected='/' actual='" + (char)c + "'-" + c + " at offset " + pdfSource.getOffset());
-        }
+        readExpectedChar('/');
         // costruisce il nome
         StringBuilder buffer = new StringBuilder();
-        c = pdfSource.read();
+        int c = pdfSource.read();
         while( c != -1 )
         {
             char ch = (char)c;
@@ -1421,6 +1394,22 @@ public abstract class BaseParser
     }
 
     /**
+     * Read one char and throw an exception if it is not the expected value.
+     *
+     * @param ec the char value that is expected.
+     * @throws IOException if the read char is not the expected value or if an
+     * I/O error occurs.
+     */
+    protected void readExpectedChar(char ec) throws IOException
+    {
+        char c = (char) pdfSource.read();
+        if (c != ec)
+        {
+            throw new IOException("expected='" + ec + "' actual='" + c + "' at offset " + pdfSource.getOffset());
+        }
+    }
+    
+    /**
      * This will read the next string from the stream up to a certain length.
      *
      * @param length The length to stop reading at.