Java Security stringToByte_8859_1(String str, boolean useUTF8)

Here you can find the source of stringToByte_8859_1(String str, boolean useUTF8)

Description

Used to convert username-value, passwd or realm to 8859_1 encoding if all chars in string are within the 8859_1 (Latin 1) encoding range.

License

Open Source License

Parameter

Parameter Description
a non-null String

Return

a non-nuill byte array containing the correct character encoding for username, paswd or realm.

Declaration

public static byte[] stringToByte_8859_1(String str, boolean useUTF8) throws SaslException 

Method Source Code


//package com.java2s;
/*//from  w w  w  . ja  v  a  2s  . co  m
 * Copyright (c) 2000, 2009 Oracle and/or its affiliates, 2014 Ron Rieve. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Ron Rieve designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Ron Rieve in the LICENSE file that accompanied this code.
 *
 * This code 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 General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

import java.io.UnsupportedEncodingException;

import javax.security.sasl.*;

public class Main {
    /**
     * Used to convert username-value, passwd or realm to 8859_1 encoding
     * if all chars in string are within the 8859_1 (Latin 1) encoding range.
     *
     * @param a non-null String
     * @return a non-nuill byte array containing the correct character encoding
     * for username, paswd or realm.
     */
    public static byte[] stringToByte_8859_1(String str, boolean useUTF8) throws SaslException {

        char[] buffer = str.toCharArray();

        try {
            if (useUTF8) {
                for (int i = 0; i < buffer.length; i++) {
                    if (buffer[i] > '\u00FF') {
                        return str.getBytes("UTF8");
                    }
                }
            }
            return str.getBytes("8859_1");
        } catch (UnsupportedEncodingException e) {
            throw new SaslException("cannot encode string in UTF8 or 8859-1 (Latin-1)", e);
        }
    }
}

Related

  1. kinit(String username, char[] password)
  2. logout()
  3. parseDirectives(byte[] buf)
  4. secretKey(final String key)
  5. serializeKerberosTicket(KerberosTicket tgt)
  6. ticketToCreds(KerberosTicket kerbTicket)
  7. tryDestroy(Object destroyable)