Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.paypal.base.codec.binary;
 
 
Converts String to and from bytes using the encodings required by the Java specification. These encodings are specified in Standard charsets

Author(s):
Gary Gregory
Version:
$Id: StringUtils.java 1170351 2011-09-13 21:09:09Z ggregory $
Since:
1.4
See also:
com.paypal.base.codec.CharEncoding
Standard charsets
 
 public class StringUtils {

    
Encodes the given string into a sequence of bytes using the ISO-8859-1 charset, storing the result into a new byte array.

Parameters:
string the String to encode, may be null
Returns:
encoded bytes, or null if the input string was null
Throws:
java.lang.IllegalStateException Thrown when the charset is missing, which should be never according the the Java specification.
See also:
Standard charsets
getBytesUnchecked(java.lang.String,java.lang.String)
 
     public static byte[] getBytesIso8859_1(String string) {
         return StringUtils.getBytesUnchecked(string.);
     }

    
Encodes the given string into a sequence of bytes using the US-ASCII charset, storing the result into a new byte array.

Parameters:
string the String to encode, may be null
Returns:
encoded bytes, or null if the input string was null
Throws:
java.lang.IllegalStateException Thrown when the charset is missing, which should be never according the the Java specification.
See also:
Standard charsets
getBytesUnchecked(java.lang.String,java.lang.String)
 
     public static byte[] getBytesUsAscii(String string) {
         return StringUtils.getBytesUnchecked(string.);
     }

    
Encodes the given string into a sequence of bytes using the UTF-16 charset, storing the result into a new byte array.

Parameters:
string the String to encode, may be null
Returns:
encoded bytes, or null if the input string was null
Throws:
java.lang.IllegalStateException Thrown when the charset is missing, which should be never according the the Java specification.
See also:
Standard charsets
getBytesUnchecked(java.lang.String,java.lang.String)
 
     public static byte[] getBytesUtf16(String string) {
         return StringUtils.getBytesUnchecked(string.);
     }

    
Encodes the given string into a sequence of bytes using the UTF-16BE charset, storing the result into a new byte array.

Parameters:
string the String to encode, may be null
Returns:
encoded bytes, or null if the input string was null
Throws:
java.lang.IllegalStateException Thrown when the charset is missing, which should be never according the the Java specification.
See also:
Standard charsets
getBytesUnchecked(java.lang.String,java.lang.String)
 
     public static byte[] getBytesUtf16Be(String string) {
         return StringUtils.getBytesUnchecked(string.);
     }

    
Encodes the given string into a sequence of bytes using the UTF-16LE charset, storing the result into a new byte array.

Parameters:
string the String to encode, may be null
Returns:
encoded bytes, or null if the input string was null
Throws:
java.lang.IllegalStateException Thrown when the charset is missing, which should be never according the the Java specification.
See also:
Standard charsets
getBytesUnchecked(java.lang.String,java.lang.String)
    public static byte[] getBytesUtf16Le(String string) {
        return StringUtils.getBytesUnchecked(string.);
    }

    
Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte array.

Parameters:
string the String to encode, may be null
Returns:
encoded bytes, or null if the input string was null
Throws:
java.lang.IllegalStateException Thrown when the charset is missing, which should be never according the the Java specification.
See also:
Standard charsets
getBytesUnchecked(java.lang.String,java.lang.String)
    public static byte[] getBytesUtf8(String string) {
        return StringUtils.getBytesUnchecked(string.);
    }

    
Encodes the given string into a sequence of bytes using the named charset, storing the result into a new byte array.

This method catches java.io.UnsupportedEncodingException and rethrows it as java.lang.IllegalStateException, which should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.

Parameters:
string the String to encode, may be null
charsetName The name of a required java.nio.charset.Charset
Returns:
encoded bytes, or null if the input string was null
Throws:
java.lang.IllegalStateException Thrown when a java.io.UnsupportedEncodingException is caught, which should never happen for a required charset name.
See also:
com.paypal.base.codec.CharEncoding
java.lang.String.getBytes(java.lang.String)
    public static byte[] getBytesUnchecked(String stringString charsetName) {
        if (string == null) {
            return null;
        }
        try {
            return string.getBytes(charsetName);
        } catch (UnsupportedEncodingException e) {
            throw StringUtils.newIllegalStateException(charsetNamee);
        }
    }
        return new IllegalStateException(charsetName + ": " + e);
    }

    
Constructs a new String by decoding the specified array of bytes using the given charset.

This method catches java.io.UnsupportedEncodingException and re-throws it as java.lang.IllegalStateException, which should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.

Parameters:
bytes The bytes to be decoded into characters, may be null
charsetName The name of a required java.nio.charset.Charset
Returns:
A new String decoded from the specified array of bytes using the given charset, or null if the input byte array was null.
Throws:
java.lang.IllegalStateException Thrown when a java.io.UnsupportedEncodingException is caught, which should never happen for a required charset name.
See also:
com.paypal.base.codec.CharEncoding
String.java.lang.String.(byte[],java.lang.String)
    public static String newString(byte[] bytesString charsetName) {
        if (bytes == null) {
            return null;
        }
        try {
            return new String(bytescharsetName);
        } catch (UnsupportedEncodingException e) {
            throw StringUtils.newIllegalStateException(charsetNamee);
        }
    }

    
Constructs a new String by decoding the specified array of bytes using the ISO-8859-1 charset.

Parameters:
bytes The bytes to be decoded into characters, may be null
Returns:
A new String decoded from the specified array of bytes using the ISO-8859-1 charset, or null if the input byte array was null.
Throws:
java.lang.IllegalStateException Thrown when a java.io.UnsupportedEncodingException is caught, which should never happen since the charset is required.
    public static String newStringIso8859_1(byte[] bytes) {
        return StringUtils.newString(bytes.);
    }

    
Constructs a new String by decoding the specified array of bytes using the US-ASCII charset.

Parameters:
bytes The bytes to be decoded into characters
Returns:
A new String decoded from the specified array of bytes using the US-ASCII charset, or null if the input byte array was null.
Throws:
java.lang.IllegalStateException Thrown when a java.io.UnsupportedEncodingException is caught, which should never happen since the charset is required.
    public static String newStringUsAscii(byte[] bytes) {
        return StringUtils.newString(bytes.);
    }

    
Constructs a new String by decoding the specified array of bytes using the UTF-16 charset.

Parameters:
bytes The bytes to be decoded into characters
Returns:
A new String decoded from the specified array of bytes using the UTF-16 charset or null if the input byte array was null.
Throws:
java.lang.IllegalStateException Thrown when a java.io.UnsupportedEncodingException is caught, which should never happen since the charset is required.
    public static String newStringUtf16(byte[] bytes) {
        return StringUtils.newString(bytes.);
    }

    
Constructs a new String by decoding the specified array of bytes using the UTF-16BE charset.

Parameters:
bytes The bytes to be decoded into characters
Returns:
A new String decoded from the specified array of bytes using the UTF-16BE charset, or null if the input byte array was null.
Throws:
java.lang.IllegalStateException Thrown when a java.io.UnsupportedEncodingException is caught, which should never happen since the charset is required.
    public static String newStringUtf16Be(byte[] bytes) {
        return StringUtils.newString(bytes.);
    }

    
Constructs a new String by decoding the specified array of bytes using the UTF-16LE charset.

Parameters:
bytes The bytes to be decoded into characters
Returns:
A new String decoded from the specified array of bytes using the UTF-16LE charset, or null if the input byte array was null.
Throws:
java.lang.IllegalStateException Thrown when a java.io.UnsupportedEncodingException is caught, which should never happen since the charset is required.
    public static String newStringUtf16Le(byte[] bytes) {
        return StringUtils.newString(bytes.);
    }

    
Constructs a new String by decoding the specified array of bytes using the UTF-8 charset.

Parameters:
bytes The bytes to be decoded into characters
Returns:
A new String decoded from the specified array of bytes using the UTF-8 charset, or null if the input byte array was null.
Throws:
java.lang.IllegalStateException Thrown when a java.io.UnsupportedEncodingException is caught, which should never happen since the charset is required.
    public static String newStringUtf8(byte[] bytes) {
        return StringUtils.newString(bytes.);
    }
New to GrepCode? Check out our FAQ X