private static void writeString(byte[] bytes, boolean forceHex, OutputStream output)
            throws IOException {
        // check for non-ASCII characters
        boolean isASCII = true;
        if (!forceHex) {
            for (byte b : bytes) {
                // if the byte is negative then it is an eight bit byte and is outside the ASCII range
                if (b < 0) {
                    isASCII = false;
                    break;
                }
                // PDFBOX-3107 EOL markers within a string are troublesome
                if (b == 0x0d || b == 0x0a) {
                    isASCII = false;
                    break;
                }
            }
        }

        if (isASCII && !forceHex) {
            // write ASCII string
            output.write('(');
            for (byte b : bytes) {
                switch (b) {
                    case '(':
                    case ')':
                    case '\\':
                        output.write('\\');
                        output.write(b);
                        break;
                    default:
                        output.write(b);
                        break;
                }
            }
            output.write(')');
        } else {
            // write hex string
            output.write('<');
            Hex.writeHexBytes(bytes, output);
            output.write('>');
        }
    }
This will output the given text/byte string as a PDF object.

@param output The stream to write to.
@throws IOException If there is an error writing to the stream.
tika-fuzzing/src/main/java/org/apache/tika/fuzzing/pdf/EvilCOSWriter.java