* @throws IOException If there is an error reading from the stream.
*/
protected COSString parseCOSString() throws IOException
{
char nextChar = (char)pdfSource.read();
COSString retval = new COSString();
char openBrace;
char closeBrace;
if( nextChar == '(' )
{
openBrace = '(';
closeBrace = ')';
}
else if( nextChar == '<' )
{
openBrace = '<';
closeBrace = '>';
}
else
{
throw new IOException( "parseCOSString string should start with '(' or '<' and not '" +
nextChar + "' " + pdfSource );
}
//This is the number of braces read
//
int braces = 1;
int c = pdfSource.read();
while( braces > 0 && c != -1)
{
char ch = (char)c;
int nextc = -2; // not yet read
//if( log.isDebugEnabled() )
//{
// log.debug( "Parsing COSString character '" + c + "' code=" + (int)c );
//}
if(ch == closeBrace)
{
braces--;
byte[] nextThreeBytes = new byte[3];
int amountRead = pdfSource.read(nextThreeBytes);
//lets handle the special case seen in Bull River Rules and Regulations.pdf
//The dictionary looks like this
// 2 0 obj
// <<
// /Type /Info
// /Creator (PaperPort http://www.scansoft.com)
// /Producer (sspdflib 1.0 http://www.scansoft.com)
// /Title ( (5)
// /Author ()
// /Subject ()
//
// Notice the /Title, the braces are not even but they should
// be. So lets assume that if we encounter an this scenario
// <end_brace><new_line><opening_slash> then that
// means that there is an error in the pdf and assume that
// was the end of the document.
if( amountRead == 3 )
{
if( nextThreeBytes[0] == 0x0d &&
nextThreeBytes[1] == 0x0a &&
nextThreeBytes[2] == 0x2f )
{
braces = 0;
}
}
pdfSource.unread( nextThreeBytes, 0, amountRead );
if( braces != 0 )
{
retval.append( ch );
}
}
else if( ch == openBrace )
{
braces++;
retval.append( ch );
}
else if( ch == '\\' )
{
//patched by ram
char next = (char)pdfSource.read();
switch(next)
{
case 'n':
retval.append( '\n' );
break;
case 'r':
retval.append( '\r' );
break;
case 't':
retval.append( '\t' );
break;
case 'b':
retval.append( '\b' );
break;
case 'f':
retval.append( '\f' );
break;
case '(':
case ')':
case '\\':
retval.append( next );
break;
case 10:
case 13:
//this is a break in the line so ignore it and the newline and continue
c = pdfSource.read();
while( isEOL(c) && c != -1)
{
c = pdfSource.read();
}
nextc = c;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
{
StringBuffer octal = new StringBuffer();
octal.append( next );
c = pdfSource.read();
char digit = (char)c;
if( digit >= '0' && digit <= '7' )
{
octal.append( digit );
c = pdfSource.read();
digit = (char)c;
if( digit >= '0' && digit <= '7' )
{
octal.append( digit );
}
else
{
nextc = c;
}
}
else
{
nextc = c;
}
int character = 0;
try
{
character = Integer.parseInt( octal.toString(), 8 );
}
catch( NumberFormatException e )
{
throw new IOException( "Error: Expected octal character, actual='" + octal + "'" );
}
retval.append( character );
break;
}
default:
{
retval.append( '\\' );
retval.append( next );
//another ficken problem with PDF's, sometimes the \ doesn't really
//mean escape like the PDF spec says it does, sometimes is should be literal
//which is what we will assume here.
//throw new IOException( "Unexpected break sequence '" + next + "' " + pdfSource );
}
}
}
else
{
if( openBrace == '<' )
{
if( isHexDigit(ch) )
{
retval.append( ch );
}
}
else
{
retval.append( ch );
}
}
if (nextc != -2)
{
c = nextc;
}
else
{
c = pdfSource.read();
}
}
if (c != -1)
{
pdfSource.unread(c);
}
if( openBrace == '<' )
{
retval = COSString.createFromHexString( retval.getString() );
}
return retval;
}