All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kv-3.5.2.src.com.sleepycat.utilint.StringUtils Maven / Gradle / Ivy

Go to download

Oracle NoSQL Database Client - supplies build and runtime support for the client side of the Oracle NoSQL Database. Note that a running Oracle NoSQL Database Server (store) is required to do anything meaningful with this client.

There is a newer version: 4.4.6
Show newest version
/*-
 *
 *  This file is part of Oracle NoSQL Database
 *  Copyright (C) 2011, 2015 Oracle and/or its affiliates.  All rights reserved.
 *
 * If you have received this file as part of Oracle NoSQL Database the
 * following applies to the work as a whole:
 *
 *   Oracle NoSQL Database server software 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 NoSQL Database 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.
 *
 * If you have received this file as part of Oracle NoSQL Database Client or
 * distributed separately the following applies:
 *
 *   Oracle NoSQL Database client software is free software: you can
 *   redistribute it and/or modify it under the terms of the Apache License
 *   as published by the Apache Software Foundation, version 2.0.
 *
 * You should have received a copy of the GNU Affero General Public License
 * and/or the Apache License in the LICENSE file along with Oracle NoSQL
 * Database client or server distribution.  If not, see
 * 
 * or
 * .
 *
 * An active Oracle commercial licensing agreement for this product supersedes
 * these licenses and in such case the license notices, but not the copyright
 * notice, may be removed by you in connection with your distribution that is
 * in accordance with the commercial licensing terms.
 *
 * For more information please contact:
 *
 * [email protected]
 *
 */

package com.sleepycat.utilint;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CharacterCodingException;

public class StringUtils {

    private final static Charset ASCII = Charset.forName("US-ASCII");
    private final static Charset UTF8 = Charset.forName("UTF-8");

    /**
     * In all src and test code, the String(byte[], ...) constructor and
     * String.getBytes method must always be passed a Charset, to avoid
     * portability issues.  Otherwise, portability issues will occur when
     * running on a JVM plataform with a non-western default charset, the
     * EBCDIC encoding (on z/OS), etc.  [#20967]
     * 

* In most cases, the UTF8 or ASCII charset should be used for portability. * UTF8 should be used when any character may be represented. ASCII can be * used when all characters are in the ASCII range. The default charset * should only be used when handling user-input data directly, e.g., * console input/output or user-visible files. *

* Rather than passing the charset as a string (getBytes("UTF-8")), the * Charset objects defined here should be passed (getBytes(UTF8)). Not * only is using a Charset object slightly more efficient because it avoids * a lookup, even more importantly it avoids having to clutter code with a * catch for java.io.UnsupportedEncodingException, which should never be * thrown for the "UTF-8" or "US-ASCII" charsets. */ public static byte[] toUTF8(String str) { try { return str.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { /* Should never happen. */ throw new RuntimeException(e); } } /** * @return a buffer with position set to 0 */ public static ByteBuffer toUTF8(CharBuffer chars) { try { final CharsetEncoder utf8Encoder = UTF8.newEncoder(); return utf8Encoder.encode(chars); } catch (CharacterCodingException e) { // Should never happen. throw new RuntimeException(e); } } public static String fromUTF8(byte[] bytes) { try { return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException e) { /* Should never happen. */ throw new RuntimeException(e); } } public static String fromUTF8(byte[] bytes, int offset, int len) { try { return new String(bytes, offset, len, "UTF-8"); } catch (UnsupportedEncodingException e) { /* Should never happen. */ throw new RuntimeException(e); } } /** * @return a buffer with position set to 0 */ public static CharBuffer fromUTF8(ByteBuffer bytes) { try { final CharsetDecoder utf8Decoder = UTF8.newDecoder(); return utf8Decoder.decode(bytes); } catch (CharacterCodingException e) { // Should never happen. throw new RuntimeException(e); } } 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); } } public static String fromASCII(byte[] bytes) { try { return new String(bytes, "US-ASCII"); } catch (UnsupportedEncodingException e) { /* Should never happen. */ throw new RuntimeException(e); } } public static String fromASCII(byte[] bytes, int offset, int len) { try { return new String(bytes, offset, len, "US-ASCII"); } catch (UnsupportedEncodingException e) { /* Should never happen. */ throw new RuntimeException(e); } } /** * @return a buffer with position set to 0 */ public static CharBuffer fromASCII(ByteBuffer bytes) { try { final CharsetDecoder asciiDecoder = ASCII.newDecoder(); return asciiDecoder.decode(bytes); } catch (CharacterCodingException e) { // Should never happen. throw new RuntimeException(e); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy