Java UTF8 asUTF16BEEncoded(String basicString)

Here you can find the source of asUTF16BEEncoded(String basicString)

Description

Take a basic PDF string and produce a string from its bytes as an UTF16-BE encoding.

License

Open Source License

Parameter

Parameter Description
basicString the basic PDF string, as offered by PDFObject#getStringValue()

Return

the decoding of the string's bytes in UTF16-BE

Declaration

public static String asUTF16BEEncoded(String basicString) 

Method Source Code


//package com.java2s;
/*/*from  w ww  .  jav  a  2  s. c o  m*/
 * Copyright 2008 Pirion Systems Pty Ltd, 139 Warry St,
 * Fortitude Valley, Queensland, Australia
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.io.*;

public class Main {
    /**
     * Take a basic PDF string and produce a string from its bytes as an
     * UTF16-BE encoding. The first 2 bytes are presumed to be the big-endian
     * byte markers, 0xFE and 0xFF; that is not checked by this method.
     *
     * @param basicString the basic PDF string, as offered by {@link
     *  PDFObject#getStringValue()}
     * @return the decoding of the string's bytes in UTF16-BE
     */
    public static String asUTF16BEEncoded(String basicString) {
        try {
            return new String(asBytes(basicString), 2, basicString.length() - 2, "UTF-16BE");
        } catch (UnsupportedEncodingException e) {
            // UTF-16BE should always be available
            throw new RuntimeException("No UTF-16BE charset!");
        }
    }

    /**
     * Get the corresponding byte array for a basic string. This is effectively
     * the char[] array cast to bytes[], as chars in basic strings only use the
     * least significant byte.
     *
     * @param basicString the basic PDF string, as offered by {@link
     *  PDFObject#getStringValue()}
     * @return the bytes corresponding to its characters
     */
    public static byte[] asBytes(String basicString) {
        final byte[] b = new byte[basicString.length()];
        for (int i = 0; i < b.length; ++i) {
            b[i] = (byte) basicString.charAt(i);
        }
        return b;
    }
}

Related

  1. addLineIds(String inFile, String outFile)
  2. appendToFile(String outputFile, String contents)
  3. asStringUTF8(byte[] bytes)
  4. asUtf8(byte[] bytes)
  5. asUTF8(InputStream in)
  6. asUTF8bytes(String s)
  7. asUtf8ByteStream(final String string)