Java Convert via ByteBuffer toASCII(String str)

Here you can find the source of toASCII(String str)

Description

to ASCII

License

Open Source License

Declaration

public static byte[] toASCII(String str) 

Method Source Code


//package com.java2s;
/*-/*from   w ww. j a v  a  2  s  .  c om*/
 *
 *  This file is part of Oracle Berkeley DB Java Edition
 *  Copyright (C) 2002, 2015 Oracle and/or its affiliates.  All rights reserved.
 *
 *  Oracle Berkeley DB Java Edition is free software: you can redistribute it
 *  and/or modify it under the terms of the GNU Affero General Public License
 *  as published by the Free Software Foundation, version 3.
 *
 *  Oracle Berkeley DB Java Edition 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 Affero
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License in
 *  the LICENSE file along with Oracle Berkeley DB Java Edition.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 *  An active Oracle commercial licensing agreement for this product
 *  supercedes this license.
 *
 *  For more information please contact:
 *
 *  Vice President Legal, Development
 *  Oracle America, Inc.
 *  5OP-10
 *  500 Oracle Parkway
 *  Redwood Shores, CA 94065
 *
 *  or
 *
 *  berkeleydb-info_us@oracle.com
 *
 *  [This line intentionally left blank.]
 *  [This line intentionally left blank.]
 *  [This line intentionally left blank.]
 *  [This line intentionally left blank.]
 *  [This line intentionally left blank.]
 *  [This line intentionally left blank.]
 *  EOF
 *
 */

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

import java.nio.charset.CharsetEncoder;
import java.nio.charset.CharacterCodingException;

public class Main {
    private final static Charset ASCII = Charset.forName("US-ASCII");

    public static byte[] toASCII(String str) {
        try {
            return str.getBytes("US-ASCII");
        } catch (UnsupportedEncodingException e) {
            /* Should never happen. */
            throw new RuntimeException(e);
        }
    }

    /**
     * @return a buffer with position set to 0
     */
    public static ByteBuffer toASCII(CharBuffer chars) {
        try {
            final CharsetEncoder asciiEncoder = ASCII.newEncoder();
            return asciiEncoder.encode(chars);
        } catch (CharacterCodingException e) {
            // Should never happen.
            throw new RuntimeException(e);
        }
    }
}

Related

  1. to32BitsLongArray(byte[] data, boolean bigEndian)
  2. toArray(final byte[] byteArray, final int sizeLimit)
  3. toArray(int value)
  4. toArray(IntBuffer src, int[] dst, int offset)
  5. toArray(long length)
  6. toAsciiBytes(String s)
  7. toASCIIString(URI u)
  8. toAttributeValue(Object value)
  9. toBigDecimal(byte[] bytes, int scale)