📄 baseparser.java
字号:
COSBase retval = null;
skipSpaces();
int nextByte = pdfSource.peek();
char c = (char)nextByte;
switch(c)
{
case '<':
{
int leftBracket = pdfSource.read();//pull off first left bracket
c = (char)pdfSource.peek(); //check for second left bracket
pdfSource.unread( leftBracket );
if(c == '<')
{
retval = parseCOSDictionary();
skipSpaces();
}
else
{
retval = parseCOSString();
}
break;
}
case '[': // array
{
retval = parseCOSArray();
break;
}
case '(':
retval = parseCOSString();
break;
case '/': // name
retval = parseCOSName();
break;
case 'n': // null
{
String nullString = readString();
if( !nullString.equals( "null") )
{
throw new IOException("Expected='null' actual='" + nullString + "'");
}
retval = COSNull.NULL;
break;
}
case 't':
{
byte[] trueBytes = new byte[4];
int amountRead = pdfSource.read( trueBytes, 0, 4 );
String trueString = new String( trueBytes, 0, amountRead );
if( trueString.equals( "true" ) )
{
retval = COSBoolean.TRUE;
}
else
{
throw new IOException( "expected true actual='" + trueString + "' " + pdfSource );
}
break;
}
case 'f':
{
byte[] falseBytes = new byte[5];
int amountRead = pdfSource.read( falseBytes, 0, 5 );
String falseString = new String( falseBytes, 0, amountRead );
if( falseString.equals( "false" ) )
{
retval = COSBoolean.FALSE;
}
else
{
throw new IOException( "expected false actual='" + falseString + "' " + pdfSource );
}
break;
}
case 'R':
pdfSource.read();
retval = new COSObject(null);
break;
case (char)-1:
return null;
default:
{
if( Character.isDigit(c) || c == '-' || c == '+' || c == '.')
{
StringBuffer buf = new StringBuffer();
int ic = pdfSource.read();
c = (char)ic;
while( Character.isDigit( c )||
c == '-' ||
c == '+' ||
c == '.' ||
c == 'E' ||
c == 'e' )
{
buf.append( c );
ic = pdfSource.read();
c = (char)ic;
}
if( ic != -1 )
{
pdfSource.unread( ic );
}
retval = COSNumber.get( buf.toString() );
}
else
{
//This is not suppose to happen, but we will allow for it
//so we are more compatible with POS writers that don't
//follow the spec
String badString = readString();
//throw new IOException( "Unknown dir object c='" + c +
//"' peek='" + (char)pdfSource.peek() + "' " + pdfSource );
if( badString == null || badString.length() == 0 )
{
int peek = pdfSource.peek();
// we can end up in an infinite loop otherwise
throw new IOException( "Unknown dir object c='" + c +
"' cInt=" + (int)c + " peek='" + (char)peek + "' peekInt=" + peek + " " + pdfSource );
}
}
}
}
return retval;
}
/**
* This will read the next string from the stream.
*
* @return The string that was read from the stream.
*
* @throws IOException If there is an error reading from the stream.
*/
protected String readString() throws IOException
{
skipSpaces();
StringBuffer buffer = new StringBuffer();
int c = pdfSource.read();
while( !isEndOfName((char)c) && !isClosing(c) && c != -1 )
{
buffer.append( (char)c );
c = pdfSource.read();
}
if (c != -1)
{
pdfSource.unread(c);
}
return buffer.toString();
}
/**
* This will read bytes until the end of line marker occurs.
*
* @param theString The next expected string in the stream.
*
* @return The characters between the current position and the end of the line.
*
* @throws IOException If there is an error reading from the stream or theString does not match what was read.
*/
protected String readExpectedString( String theString ) throws IOException
{
int c = pdfSource.read();
while( isWhitespace(c) && c != -1)
{
c = pdfSource.read();
}
StringBuffer buffer = new StringBuffer( theString.length() );
int charsRead = 0;
while( !isEOL(c) && c != -1 && charsRead < theString.length() )
{
char next = (char)c;
buffer.append( next );
if( theString.charAt( charsRead ) == next )
{
charsRead++;
}
else
{
throw new IOException( "Error: Expected to read '" + theString +
"' instead started reading '" +buffer.toString() + "'" );
}
c = pdfSource.read();
}
while( isEOL(c) && c != -1 )
{
c = pdfSource.read();
}
if (c != -1)
{
pdfSource.unread(c);
}
return buffer.toString();
}
/**
* This will read the next string from the stream up to a certain length.
*
* @param length The length to stop reading at.
*
* @return The string that was read from the stream of length 0 to length.
*
* @throws IOException If there is an error reading from the stream.
*/
protected String readString( int length ) throws IOException
{
skipSpaces();
int c = pdfSource.read();
//average string size is around 2 and the normal string buffer size is
//about 16 so lets save some space.
StringBuffer buffer = new StringBuffer(length);
while( !isWhitespace(c) && !isClosing(c) && c != -1 && buffer.length() < length &&
c != '[' &&
c != '<' &&
c != '(' &&
c != '/' )
{
buffer.append( (char)c );
c = pdfSource.read();
}
if (c != -1)
{
pdfSource.unread(c);
}
return buffer.toString();
}
/**
* This will tell if the next character is a closing brace( close of PDF array ).
*
* @return true if the next byte is ']', false otherwise.
*
* @throws IOException If an IO error occurs.
*/
protected boolean isClosing() throws IOException
{
return isClosing(pdfSource.peek());
}
/**
* This will tell if the next character is a closing brace( close of PDF array ).
*
* @param c The character to check against end of line
* @return true if the next byte is ']', false otherwise.
*/
protected boolean isClosing(int c)
{
return c == ']';
}
/**
* This will read bytes until the end of line marker occurs.
*
* @return The characters between the current position and the end of the line.
*
* @throws IOException If there is an error reading from the stream.
*/
protected String readLine() throws IOException
{
int c = pdfSource.read();
while(isWhitespace(c) && c != -1)
{
c = pdfSource.read();
}
StringBuffer buffer = new StringBuffer( 11 );
while( !isEOL(c) && c != -1 )
{
buffer.append( (char)c );
c = pdfSource.read();
}
while( isEOL(c) && c != -1 )
{
c = pdfSource.read();
}
if (c != -1)
{
pdfSource.unread(c);
}
return buffer.toString();
}
/**
* This will tell if the next byte to be read is an end of line byte.
*
* @return true if the next byte is 0x0A or 0x0D.
*
* @throws IOException If there is an error reading from the stream.
*/
protected boolean isEOL() throws IOException
{
return isEOL(pdfSource.peek());
}
/**
* This will tell if the next byte to be read is an end of line byte.
*
* @param c The character to check against end of line
* @return true if the next byte is 0x0A or 0x0D.
*/
protected boolean isEOL(int c)
{
return c == 10 || c == 13;
}
/**
* This will tell if the next byte is whitespace or not.
*
* @return true if the next byte in the stream is a whitespace character.
*
* @throws IOException If there is an error reading from the stream.
*/
protected boolean isWhitespace() throws IOException
{
return isWhitespace( pdfSource.peek() );
}
/**
* This will tell if the next byte is whitespace or not.
*
* @param c The character to check against whitespace
*
* @return true if the next byte in the stream is a whitespace character.
*/
protected boolean isWhitespace( int c )
{
return c == 0 || c == 9 || c == 12 || c == 10
|| c == 13 || c == 32;
}
/**
* This will skip all spaces and comments that are present.
*
* @throws IOException If there is an error reading from the stream.
*/
protected void skipSpaces() throws IOException
{
//log( "skipSpaces() " + pdfSource );
int c = pdfSource.read();
// identical to, but faster as: isWhiteSpace(c) || c == 37
while(c == 0 || c == 9 || c == 12 || c == 10
|| c == 13 || c == 32 || c == 37)//37 is the % character, a comment
{
if ( c == 37 )
{
// skip past the comment section
c = pdfSource.read();
while(!isEOL(c) && c != -1)
{
c = pdfSource.read();
}
}
else
{
c = pdfSource.read();
}
}
if (c != -1)
{
pdfSource.unread(c);
}
//log( "skipSpaces() done peek='" + (char)pdfSource.peek() + "'" );
}
/**
* This will read an integer from the stream.
*
* @return The integer that was read from the stream.
*
* @throws IOException If there is an error reading from the stream.
*/
protected int readInt() throws IOException
{
skipSpaces();
int retval = 0;
int lastByte = 0;
StringBuffer intBuffer = new StringBuffer();
while( (lastByte = pdfSource.read() ) != 32 &&
lastByte != 10 &&
lastByte != 13 &&
lastByte != 0 && //See sourceforge bug 853328
lastByte != -1 )
{
intBuffer.append( (char)lastByte );
}
try
{
retval = Integer.parseInt( intBuffer.toString() );
}
catch( NumberFormatException e )
{
throw new IOException( "Error: Expected an integer type, actual='" + intBuffer + "'" );
}
return retval;
}
/**
* This will add an xref.
*
* @param xref The xref to add.
*/
public void addXref( PDFXref xref )
{
xrefs.add(xref);
}
/**
* This will get all of the xrefs.
*
* @return A list of all xrefs.
*/
public List getXrefs()
{
return xrefs;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -