Java examples for java.lang:String UTF
Take a basic PDF string and produce a string from its bytes as an UTF16-BE encoding.
/*//from w w w . j a v a 2 s. com * 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 */ //package com.java2s; 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; } }