BASE642


点击(此处)折叠或打开

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements. See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  *
  9.  * http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */


  17. import java.io.UnsupportedEncodingException;
  18. import java.nio.charset.Charset;



  19. /**
  20.  * Converts String to and from bytes using the encodings required by the Java specification. These encodings are
  21.  * specified in <a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">
  22.  * Standard charsets</a>.
  23.  *
  24.  *

    This class is immutable and thread-safe.


  25.  *
  26.  * @see CharEncoding
  27.  * @see <a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  28.  * @version $Id: StringUtils.java 1378740 2012-08-29 21:18:47Z tn $
  29.  * @since 1.4
  30.  */
  31. public class StringUtils {

  32.     /**
  33.      * Calls {@link String#getBytes(Charset)}
  34.      *
  35.      * @param string
  36.      * The string to encode (if null, return null).
  37.      * @param charset
  38.      * The {@link Charset} to encode the {@code String}
  39.      * @return the encoded bytes
  40.      */
  41.     private static byte[] getBytes(String string, Charset charset) {
  42.         if (string == null) {
  43.             return null;
  44.         }
  45.         return string.getBytes(charset);
  46.     }

  47.     /**
  48.      * Encodes the given string into a sequence of bytes using the ISO-8859-1 charset, storing the result into a new
  49.      * byte array.
  50.      *
  51.      * @param string
  52.      * the String to encode, may be {@code null}
  53.      * @return encoded bytes, or {@code null} if the input string was {@code null}
  54.      * @throws NullPointerException
  55.      * Thrown if {@link Charsets#ISO_8859_1} is not initialized, which should never happen since it is
  56.      * required by the Java platform specification.
  57.      * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
  58.      * @see <a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  59.      * @see #getBytesUnchecked(String, String)
  60.      */
  61.     public static byte[] getBytesIso8859_1(String string) {
  62.         return getBytes(string, Charsets.ISO_8859_1);
  63.     }


  64.     /**
  65.      * Encodes the given string into a sequence of bytes using the named charset, storing the result into a new byte
  66.      * array.
  67.      *


  68.      * This method catches {@link UnsupportedEncodingException} and rethrows it as {@link IllegalStateException}, which
  69.      * should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.
  70.      *
  71.      *
  72.      * @param string
  73.      * the String to encode, may be {@code null}
  74.      * @param charsetName
  75.      * The name of a required {@link java.nio.charset.Charset}
  76.      * @return encoded bytes, or {@code null} if the input string was {@code null}
  77.      * @throws IllegalStateException
  78.      * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen for a
  79.      * required charset name.
  80.      * @see CharEncoding
  81.      * @see String#getBytes(String)
  82.      */
  83.     public static byte[] getBytesUnchecked(String string, String charsetName) {
  84.         if (string == null) {
  85.             return null;
  86.         }
  87.         try {
  88.             return string.getBytes(charsetName);
  89.         } catch (UnsupportedEncodingException e) {
  90.             throw StringUtils.newIllegalStateException(charsetName, e);
  91.         }
  92.     }

  93.     /**
  94.      * Encodes the given string into a sequence of bytes using the US-ASCII charset, storing the result into a new byte
  95.      * array.
  96.      *
  97.      * @param string
  98.      * the String to encode, may be {@code null}
  99.      * @return encoded bytes, or {@code null} if the input string was {@code null}
  100.      * @throws NullPointerException
  101.      * Thrown if {@link Charsets#US_ASCII} is not initialized, which should never happen since it is
  102.      * required by the Java platform specification.
  103.      * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
  104.      * @see <a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  105.      * @see #getBytesUnchecked(String, String)
  106.      */
  107.     public static byte[] getBytesUsAscii(String string) {
  108.         return getBytes(string, Charsets.US_ASCII);
  109.     }

  110.     /**
  111.      * Encodes the given string into a sequence of bytes using the UTF-16 charset, storing the result into a new byte
  112.      * array.
  113.      *
  114.      * @param string
  115.      * the String to encode, may be {@code null}
  116.      * @return encoded bytes, or {@code null} if the input string was {@code null}
  117.      * @throws NullPointerException
  118.      * Thrown if {@link Charsets#UTF_16} is not initialized, which should never happen since it is
  119.      * required by the Java platform specification.
  120.      * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
  121.      * @see <a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  122.      * @see #getBytesUnchecked(String, String)
  123.      */
  124.     public static byte[] getBytesUtf16(String string) {
  125.         return getBytes(string, Charsets.UTF_16);
  126.     }

  127.     /**
  128.      * Encodes the given string into a sequence of bytes using the UTF-16BE charset, storing the result into a new byte
  129.      * array.
  130.      *
  131.      * @param string
  132.      * the String to encode, may be {@code null}
  133.      * @return encoded bytes, or {@code null} if the input string was {@code null}
  134.      * @throws NullPointerException
  135.      * Thrown if {@link Charsets#UTF_16BE} is not initialized, which should never happen since it is
  136.      * required by the Java platform specification.
  137.      * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
  138.      * @see <a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  139.      * @see #getBytesUnchecked(String, String)
  140.      */
  141.     public static byte[] getBytesUtf16Be(String string) {
  142.         return getBytes(string, Charsets.UTF_16BE);
  143.     }

  144.     /**
  145.      * Encodes the given string into a sequence of bytes using the UTF-16LE charset, storing the result into a new byte
  146.      * array.
  147.      *
  148.      * @param string
  149.      * the String to encode, may be {@code null}
  150.      * @return encoded bytes, or {@code null} if the input string was {@code null}
  151.      * @throws NullPointerException
  152.      * Thrown if {@link Charsets#UTF_16LE} is not initialized, which should never happen since it is
  153.      * required by the Java platform specification.
  154.      * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
  155.      * @see <a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  156.      * @see #getBytesUnchecked(String, String)
  157.      */
  158.     public static byte[] getBytesUtf16Le(String string) {
  159.         return getBytes(string, Charsets.UTF_16LE);
  160.     }

  161.     /**
  162.      * Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte
  163.      * array.
  164.      *
  165.      * @param string
  166.      * the String to encode, may be {@code null}
  167.      * @return encoded bytes, or {@code null} if the input string was {@code null}
  168.      * @throws NullPointerException
  169.      * Thrown if {@link Charsets#UTF_8} is not initialized, which should never happen since it is
  170.      * required by the Java platform specification.
  171.      * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
  172.      * @see <a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  173.      * @see #getBytesUnchecked(String, String)
  174.      */
  175.     public static byte[] getBytesUtf8(String string) {
  176.         return getBytes(string, Charsets.UTF_8);
  177.     }

  178.     private static IllegalStateException newIllegalStateException(String charsetName, UnsupportedEncodingException e) {
  179.         return new IllegalStateException(charsetName + ": " + e);
  180.     }

  181.     /**
  182.      * Constructs a new <code>String</code> by decoding the specified array of bytes using the given charset.
  183.      *
  184.      * @param bytes
  185.      * The bytes to be decoded into characters
  186.      * @param charset
  187.      * The {@link Charset} to encode the {@code String}
  188.      * @return A new <code>String</code> decoded from the specified array of bytes using the given charset,
  189.      * or {@code null} if the input byte array was {@code null}.
  190.      * @throws NullPointerException
  191.      * Thrown if {@link Charsets#UTF_8} is not initialized, which should never happen since it is
  192.      * required by the Java platform specification.
  193.      */
  194.     private static String newString(byte[] bytes, Charset charset) {
  195.         return bytes == null ? null : new String(bytes, charset);
  196.     }

  197.     /**
  198.      * Constructs a new <code>String</code> by decoding the specified array of bytes using the given charset.
  199.      *


  200.      * This method catches {@link UnsupportedEncodingException} and re-throws it as {@link IllegalStateException}, which
  201.      * should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.
  202.      *
  203.      *
  204.      * @param bytes
  205.      * The bytes to be decoded into characters, may be {@code null}
  206.      * @param charsetName
  207.      * The name of a required {@link java.nio.charset.Charset}
  208.      * @return A new <code>String</code> decoded from the specified array of bytes using the given charset,
  209.      * or {@code null} if the input byte array was {@code null}.
  210.      * @throws IllegalStateException
  211.      * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen for a
  212.      * required charset name.
  213.      * @see CharEncoding
  214.      * @see String#String(byte[], String)
  215.      */
  216.     public static String newString(byte[] bytes, String charsetName) {
  217.         if (bytes == null) {
  218.             return null;
  219.         }
  220.         try {
  221.             return new String(bytes, charsetName);
  222.         } catch (UnsupportedEncodingException e) {
  223.             throw StringUtils.newIllegalStateException(charsetName, e);
  224.         }
  225.     }

  226.     /**
  227.      * Constructs a new <code>String</code> by decoding the specified array of bytes using the ISO-8859-1 charset.
  228.      *
  229.      * @param bytes
  230.      * The bytes to be decoded into characters, may be {@code null}
  231.      * @return A new <code>String</code> decoded from the specified array of bytes using the ISO-8859-1 charset, or
  232.      * {@code null} if the input byte array was {@code null}.
  233.      * @throws NullPointerException
  234.      * Thrown if {@link Charsets#ISO_8859_1} is not initialized, which should never happen since it is
  235.      * required by the Java platform specification.
  236.      * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
  237.      */
  238.     public static String newStringIso8859_1(byte[] bytes) {
  239.         return new String(bytes, Charsets.ISO_8859_1);
  240.     }

  241.     /**
  242.      * Constructs a new <code>String</code> by decoding the specified array of bytes using the US-ASCII charset.
  243.      *
  244.      * @param bytes
  245.      * The bytes to be decoded into characters
  246.      * @return A new <code>String</code> decoded from the specified array of bytes using the US-ASCII charset,
  247.      * or {@code null} if the input byte array was {@code null}.
  248.      * @throws NullPointerException
  249.      * Thrown if {@link Charsets#US_ASCII} is not initialized, which should never happen since it is
  250.      * required by the Java platform specification.
  251.      * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
  252.      */
  253.     public static String newStringUsAscii(byte[] bytes) {
  254.         return new String(bytes, Charsets.US_ASCII);
  255.     }

  256.     /**
  257.      * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16 charset.
  258.      *
  259.      * @param bytes
  260.      * The bytes to be decoded into characters
  261.      * @return A new <code>String</code> decoded from the specified array of bytes using the UTF-16 charset
  262.      * or {@code null} if the input byte array was {@code null}.
  263.      * @throws NullPointerException
  264.      * Thrown if {@link Charsets#UTF_16} is not initialized, which should never happen since it is
  265.      * required by the Java platform specification.
  266.      * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
  267.      */
  268.     public static String newStringUtf16(byte[] bytes) {
  269.         return new String(bytes, Charsets.UTF_16);
  270.     }

  271.     /**
  272.      * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16BE charset.
  273.      *
  274.      * @param bytes
  275.      * The bytes to be decoded into characters
  276.      * @return A new <code>String</code> decoded from the specified array of bytes using the UTF-16BE charset,
  277.      * or {@code null} if the input byte array was {@code null}.
  278.      * @throws NullPointerException
  279.      * Thrown if {@link Charsets#UTF_16BE} is not initialized, which should never happen since it is
  280.      * required by the Java platform specification.
  281.      * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
  282.      */
  283.     public static String newStringUtf16Be(byte[] bytes) {
  284.         return new String(bytes, Charsets.UTF_16BE);
  285.     }

  286.     /**
  287.      * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16LE charset.
  288.      *
  289.      * @param bytes
  290.      * The bytes to be decoded into characters
  291.      * @return A new <code>String</code> decoded from the specified array of bytes using the UTF-16LE charset,
  292.      * or {@code null} if the input byte array was {@code null}.
  293.      * @throws NullPointerException
  294.      * Thrown if {@link Charsets#UTF_16LE} is not initialized, which should never happen since it is
  295.      * required by the Java platform specification.
  296.      * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
  297.      */
  298.     public static String newStringUtf16Le(byte[] bytes) {
  299.         return new String(bytes, Charsets.UTF_16LE);
  300.     }

  301.     /**
  302.      * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-8 charset.
  303.      *
  304.      * @param bytes
  305.      * The bytes to be decoded into characters
  306.      * @return A new <code>String</code> decoded from the specified array of bytes using the UTF-8 charset,
  307.      * or {@code null} if the input byte array was {@code null}.
  308.      * @throws NullPointerException
  309.      * Thrown if {@link Charsets#UTF_8} is not initialized, which should never happen since it is
  310.      * required by the Java platform specification.
  311.      * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
  312.      */
  313.     public static String newStringUtf8(byte[] bytes) {
  314.         return newString(bytes, Charsets.UTF_8);
  315.     }

  316. }


  317. class CharEncoding {
  318.    
  319.     public static final String ISO_8859_1 = "ISO-8859-1";
  320.    
  321.     public static final String US_ASCII = "US-ASCII";
  322.     
  323.     public static final String UTF_16 = "UTF-16";
  324.    
  325.     public static final String UTF_16BE = "UTF-16BE";

  326.     public static final String UTF_16LE = "UTF-16LE";

  327.     public static final String UTF_8 = "UTF-8";
  328. }


  329. class Charsets {

  330.     public static Charset toCharset(Charset charset) {
  331.         return charset == null ? Charset.defaultCharset() : charset;
  332.     }
  333.     public static Charset toCharset(String charset) {
  334.         return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
  335.     }
  336.     public static final Charset ISO_8859_1 = Charset.forName(CharEncoding.ISO_8859_1);
  337.    
  338.     public static final Charset US_ASCII = Charset.forName(CharEncoding.US_ASCII);
  339.    
  340.     public static final Charset UTF_16 = Charset.forName(CharEncoding.UTF_16);
  341.     
  342.     public static final Charset UTF_16BE = Charset.forName(CharEncoding.UTF_16BE);
  343.     
  344.     public static final Charset UTF_16LE = Charset.forName(CharEncoding.UTF_16LE);

  345.     public static final Charset UTF_8 = Charset.forName(CharEncoding.UTF_8);
  346. }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31347383/viewspace-2122414/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/31347383/viewspace-2122414/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值