Method from org.apache.pdfbox.cos.COSString Detail: |
public Object accept(ICOSVisitor visitor) throws COSVisitorException {
return visitor.visitFromString( this );
}
visitor pattern double dispatch method. |
public void append(byte[] data) throws IOException {
out.write( data );
this.str = null;
}
This will append a byte[] to the string. |
public void append(int in) throws IOException {
out.write( in );
this.str = null;
}
This will append a byte to the string. |
public static COSString createFromHexString(String hex) throws IOException {
COSString retval = new COSString();
StringBuffer hexBuffer = new StringBuffer( hex.trim() );
//if odd number then the last hex digit is assumed to be 0
if( hexBuffer.length() % 2 == 1 )
{
hexBuffer.append( "0" );
}
for( int i=0; i< hexBuffer.length();)
{
String hexChars = "" + hexBuffer.charAt( i++ ) + hexBuffer.charAt( i++ );
try
{
retval.append( Integer.parseInt( hexChars, 16 ) );
}
catch( NumberFormatException e )
{
throw new IOException( "Error: Expected hex number, actual='" + hexChars + "'" );
}
}
return retval;
}
This will create a COS string from a string of hex characters. |
public boolean equals(Object obj) {
if (obj instanceof COSString)
{
obj = ((COSString) obj).getString();
return this.getString().equals(obj);
}
return false;
}
|
public byte[] getBytes() {
return out.toByteArray();
}
This will get the bytes of the string. |
public String getHexString() {
StringBuffer retval = new StringBuffer( out.size() * 2 );
byte[] data = getBytes();
for( int i=0; i< data.length; i++ )
{
retval.append( COSHEXTable.HEX_TABLE[ (data[i]+256)%256 ] );
}
return retval.toString();
}
This will take this string and create a hex representation of the bytes that make the string. |
public String getString() {
if (this.str != null)
{
return this.str;
}
String retval;
String encoding = "ISO-8859-1";
byte[] data = getBytes();
int start = 0;
if( data.length > 2 )
{
if( data[0] == (byte)0xFF && data[1] == (byte)0xFE )
{
encoding = "UTF-16LE";
start=2;
}
else if( data[0] == (byte)0xFE && data[1] == (byte)0xFF )
{
encoding = "UTF-16BE";
start=2;
}
}
try
{
retval = new String( getBytes(), start, data.length-start, encoding );
}
catch( UnsupportedEncodingException e )
{
//should never happen
e.printStackTrace();
retval = new String( getBytes() );
}
this.str = retval;
return retval;
}
This will get the string that this object wraps. |
public int hashCode() {
return getString().hashCode();
}
|
public void reset() {
out.reset();
this.str = null;
}
This will reset the internal buffer. |
public void setForceLiteralForm(boolean v) {
forceLiteralForm = v;
}
Forces the string to be written in literal form instead of hexadecimal form. |
public String toString() {
return "COSString{" + this.getString() + "}";
}
|
public void writePDF(OutputStream output) throws IOException {
boolean outsideASCII = false;
//Lets first check if we need to escape this string.
byte[] bytes = getBytes();
for( int i=0; i< bytes.length && !outsideASCII; i++ )
{
//if the byte is negative then it is an eight bit byte and is
//outside the ASCII range.
outsideASCII = bytes[i] < 0;
}
if( !outsideASCII || forceLiteralForm )
{
output.write(STRING_OPEN);
for( int i=0; i< bytes.length; i++ )
{
int b = (bytes[i]+256)%256;
switch( b )
{
case '(':
case ')':
case '\\':
{
output.write(ESCAPE);
output.write(b);
break;
}
case 10: //LF
{
output.write( LF_ESCAPE );
break;
}
case 13: // CR
{
output.write( CR_ESCAPE );
break;
}
case '\t':
{
output.write( HT_ESCAPE );
break;
}
case '\b':
{
output.write( BS_ESCAPE );
break;
}
case '\f':
{
output.write( FF_ESCAPE );
break;
}
default:
{
output.write( b );
}
}
}
output.write(STRING_CLOSE);
}
else
{
output.write(HEX_STRING_OPEN);
for(int i=0; i< bytes.length; i++ )
{
output.write( COSHEXTable.TABLE[ (bytes[i]+256)%256 ] );
}
output.write(HEX_STRING_CLOSE);
}
}
This will output this string as a PDF object. |