/** * <p>Turns a codepage number into the equivalent character encoding's * name (in Java NIO canonical naming format).</p> * * @param codepage The codepage number * * @return The character encoding's name. If the codepage number is 65001, * the encoding name is "UTF-8". All other positive numbers are mapped to * their Java NIO names, normally either "windows-" followed by the number, * eg "windows-1251", or "cp" followed by the number, e.g. if the codepage * number is 1252 the returned character encoding name will be "cp1252". * * @exception UnsupportedEncodingException if the specified codepage is * less than zero. */ public static String codepageToEncoding(final int codepage) throws UnsupportedEncodingException { return codepageToEncoding(codepage, false); }
public void setJavaValue( String string, int codepage ) throws UnsupportedEncodingException { int cp = ( codepage == -1 ) ? Property.DEFAULT_CODEPAGE : codepage; _value = CodePageUtil.getBytesInCodePage(string + "\0", cp); }
/** * Converts the bytes into a String, based on the equivalent character encoding * to the supplied codepage number. * @param string The byte of the string to convert * @param codepage The codepage number */ public static String getStringFromCodePage(final byte[] string, final int codepage) throws UnsupportedEncodingException { return getStringFromCodePage(string, 0, string.length, codepage); }
/** * Create the StringBuilder from the text and unicode flag */ private static StringBuilder buildInitSB(byte[] text, PieceDescriptor pd) { if (StringUtil.BIG5.equals(pd.getCharset())) { return new StringBuilder(CodePageUtil.cp950ToString(text, 0, text.length)); } String str = new String(text, 0, text.length, (pd.isUnicode()) ? StringUtil.UTF16LE : pd.getCharset()); return new StringBuilder(str); }
/** * Converts a string into bytes, in the equivalent character encoding * to the supplied codepage number. * @param string The string to convert * @param codepage The codepage number */ public static byte[] getBytesInCodePage(final String string, final int codepage) throws UnsupportedEncodingException { String encoding = codepageToEncoding(codepage); return string.getBytes(encoding); }
protected static String getString(byte[] data, CodepageRecord codepage) { int cp = Property.DEFAULT_CODEPAGE; if (codepage != null) { cp = codepage.getCodepage() & 0xffff; } try { return CodePageUtil.getStringFromCodePage(data, cp); } catch (UnsupportedEncodingException uee) { throw new IllegalArgumentException("Unsupported codepage requested", uee); } }
public void setJavaValue( String string ) throws UnsupportedEncodingException { _value = CodePageUtil.getBytesInCodePage(string + "\0", CodePageUtil.CP_UNICODE); }
/** * Converts the bytes into a String, based on the equivalent character encoding * to the supplied codepage number. * @param string The byte of the string to convert * @param codepage The codepage number */ public static String getStringFromCodePage(final byte[] string, final int offset, final int length, final int codepage) throws UnsupportedEncodingException { String encoding = codepageToEncoding(codepage); return new String(string, offset, length, encoding); }
public String getJavaValue( int codepage ) throws UnsupportedEncodingException { int cp = ( codepage == -1 ) ? Property.DEFAULT_CODEPAGE : codepage; String result = CodePageUtil.getStringFromCodePage(_value, cp); final int terminator = result.indexOf( '\0' ); if ( terminator == -1 ) { String msg = "String terminator (\\0) for CodePageString property value not found." + "Continue without trimming and hope for the best."; LOG.log(POILogger.WARN, msg); return result; } if ( terminator != result.length() - 1 ) { String msg = "String terminator (\\0) for CodePageString property value occured before the end of string. "+ "Trimming and hope for the best."; LOG.log(POILogger.WARN, msg ); } return result.substring( 0, terminator ); }
/** * Writes the section's dictionary. * * @param out The output stream to write to. * @param codepage The codepage to be used to write the dictionary items. * @exception IOException if an I/O exception occurs. */ private void writeDictionary(final OutputStream out, final int codepage) throws IOException { final byte padding[] = new byte[4]; final Map<Long,String> dic = getDictionary(); LittleEndian.putUInt(dic.size(), out); int length = LittleEndianConsts.INT_SIZE; for (Map.Entry<Long,String> ls : dic.entrySet()) { LittleEndian.putUInt(ls.getKey(), out); length += LittleEndianConsts.INT_SIZE; final String value = ls.getValue()+"\0"; final byte bytes[] = CodePageUtil.getBytesInCodePage(value, codepage); final int len = (codepage == CodePageUtil.CP_UNICODE) ? value.length() : bytes.length; LittleEndian.putUInt( len, out ); length += LittleEndianConsts.INT_SIZE; out.write(bytes); length += bytes.length; final int pad = (codepage == CodePageUtil.CP_UNICODE) ? ((4 - (length & 0x3)) & 0x3) : 0; out.write(padding, 0, pad); length += pad; } final int pad = (4 - (length & 0x3)) & 0x3; out.write(padding, 0, pad); }
private String decodeValueFromID() { try { switch((int)getID()) { case PropertyIDMap.PID_CODEPAGE: return CodePageUtil.codepageToEncoding(((Number)value).intValue()); case PropertyIDMap.PID_LOCALE: return LocaleUtil.getLocaleFromLCID(((Number)value).intValue()); } } catch (Exception e) { LOG.log(POILogger.WARN, "Can't decode id "+getID()); } return null; }
return CodePageUtil.getStringFromCodePage(b, Property.DEFAULT_CODEPAGE); } catch (UnsupportedEncodingException e) {
public void setJavaValue( String string, int codepage ) throws UnsupportedEncodingException { int cp = ( codepage == -1 ) ? Property.DEFAULT_CODEPAGE : codepage; _value = CodePageUtil.getBytesInCodePage(string + "\0", cp); }
private void checkCodePage(String value) { int cp = getCodepage(); if (cp == -1) { cp = Property.DEFAULT_CODEPAGE; } if (cp == CodePageUtil.CP_UNICODE) { return; } String cps = ""; try { cps = CodePageUtil.codepageToEncoding(cp, false); } catch (UnsupportedEncodingException e) { LOG.log(POILogger.ERROR, "Codepage '"+cp+"' can't be found."); } if (!cps.isEmpty() && Charset.forName(cps).newEncoder().canEncode(value)) { return; } LOG.log(POILogger.DEBUG, "Charset '"+cps+"' can't encode '"+value+"' - switching to unicode."); setCodepage(CodePageUtil.CP_UNICODE); } }
byte buf[] = IOUtils.safelyAllocate(nrBytes, MAX_RECORD_LENGTH); leis.readFully(buf, 0, nrBytes); final String str = CodePageUtil.getStringFromCodePage(buf, 0, nrBytes, cp);
public void setJavaValue( String string ) throws UnsupportedEncodingException { _value = CodePageUtil.getBytesInCodePage(string + "\0", CodePageUtil.CP_UNICODE); }
String csStr = CodePageUtil.codepageToEncoding(codepage > 0 ? codepage : Property.DEFAULT_CODEPAGE); if (!Charset.forName(csStr).newEncoder().canEncode((String)value)) { variantType = Variant.VT_LPWSTR;
/** * Converts the bytes into a String, based on the equivalent character encoding * to the supplied codepage number. * @param string The byte of the string to convert * @param codepage The codepage number */ public static String getStringFromCodePage(final byte[] string, final int codepage) throws UnsupportedEncodingException { return getStringFromCodePage(string, 0, string.length, codepage); }
/** * Writes the section's dictionary. * * @param out The output stream to write to. * @param codepage The codepage to be used to write the dictionary items. * @exception IOException if an I/O exception occurs. */ private void writeDictionary(final OutputStream out, final int codepage) throws IOException { final byte padding[] = new byte[4]; final Map<Long,String> dic = getDictionary(); LittleEndian.putUInt(dic.size(), out); int length = LittleEndianConsts.INT_SIZE; for (Map.Entry<Long,String> ls : dic.entrySet()) { LittleEndian.putUInt(ls.getKey(), out); length += LittleEndianConsts.INT_SIZE; final String value = ls.getValue()+"\0"; final byte bytes[] = CodePageUtil.getBytesInCodePage(value, codepage); final int len = (codepage == CodePageUtil.CP_UNICODE) ? value.length() : bytes.length; LittleEndian.putUInt( len, out ); length += LittleEndianConsts.INT_SIZE; out.write(bytes); length += bytes.length; final int pad = (codepage == CodePageUtil.CP_UNICODE) ? ((4 - (length & 0x3)) & 0x3) : 0; out.write(padding, 0, pad); length += pad; } final int pad = (4 - (length & 0x3)) & 0x3; out.write(padding, 0, pad); }
modules.charset = Charset.forName(CodePageUtil.codepageToEncoding(codepage, true)); break; case MODULE_STREAM_NAME: