All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.itextpdf.text.pdf.StringUtils Maven / Gradle / Ivy

There is a newer version: 5.5.13.3
Show newest version
package com.itextpdf.text.pdf;

import com.itextpdf.text.DocWriter;

public class StringUtils {

    final static private byte[] r = DocWriter.getISOBytes("\\r");
    final static private byte[] n = DocWriter.getISOBytes("\\n");
    final static private byte[] t = DocWriter.getISOBytes("\\t");
    final static private byte[] b = DocWriter.getISOBytes("\\b");
    final static private byte[] f = DocWriter.getISOBytes("\\f");

    private StringUtils() {

    }

    /**
     * Escapes a byte array according to the PDF conventions.
     *
     * @param bytes the byte array to escape
     * @return an escaped byte array
     */
    public static byte[] escapeString(final byte bytes[]) {
        ByteBuffer content = new ByteBuffer();
        escapeString(bytes, content);
        return content.toByteArray();
    }

    /**
     * Escapes a byte array according to the PDF conventions.
     *
     * @param bytes the byte array to escape
     * @param content the content
     */
    public static void escapeString(final byte bytes[], final ByteBuffer content) {
        content.append_i('(');
        for (int k = 0; k < bytes.length; ++k) {
            byte c = bytes[k];
            switch (c) {
                case '\r':
                    content.append(r);
                    break;
                case '\n':
                    content.append(n);
                    break;
                case '\t':
                    content.append(t);
                    break;
                case '\b':
                    content.append(b);
                    break;
                case '\f':
                    content.append(f);
                    break;
                case '(':
                case ')':
                case '\\':
                    content.append_i('\\').append_i(c);
                    break;
                default:
                    content.append_i(c);
            }
        }
        content.append_i(')');
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy