Path: blob/master/src/java.security.jgss/share/classes/sun/security/jgss/GSSToken.java
41159 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.jgss;2627import java.io.InputStream;28import java.io.OutputStream;29import java.io.IOException;30import java.io.EOFException;31import sun.security.util.*;3233/**34* Utilities for processing GSS Tokens.35*36*/3738public abstract class GSSToken {3940/**41* Copies an integer value to a byte array in little endian form.42* @param value the integer value to write43* @param array the byte array into which the integer must be copied. It44* is assumed that the array will be large enough to hold the 4 bytes of45* the integer.46*/47public static final void writeLittleEndian(int value, byte[] array) {48writeLittleEndian(value, array, 0);49}5051/**52* Copies an integer value to a byte array in little endian form.53* @param value the integer value to write54* @param array the byte array into which the integer must be copied. It55* is assumed that the array will be large enough to hold the 4 bytes of56* the integer.57* @param pos the position at which to start writing58*/59public static final void writeLittleEndian(int value, byte[] array,60int pos) {61array[pos++] = (byte)(value);62array[pos++] = (byte)((value>>>8));63array[pos++] = (byte)((value>>>16));64array[pos++] = (byte)((value>>>24));65}6667public static final void writeBigEndian(int value, byte[] array) {68writeBigEndian(value, array, 0);69}7071public static final void writeBigEndian(int value, byte[] array,72int pos) {73array[pos++] = (byte)((value>>>24));74array[pos++] = (byte)((value>>>16));75array[pos++] = (byte)((value>>>8));76array[pos++] = (byte)(value);77}7879/**80* Reads an integer value from a byte array in little endian form. This81* method allows the reading of two byte values as well as four bytes82* values both of which are needed in the Kerberos v5 GSS-API mechanism.83*84* @param data the array containing the bytes of the integer value85* @param pos the offset in the array86* @param size the number of bytes to read from the array.87* @return the integer value88*/89public static final int readLittleEndian(byte[] data, int pos, int size) {90int retVal = 0;91int shifter = 0;92while (size > 0) {93retVal += (data[pos] & 0xff) << shifter;94shifter += 8;95pos++;96size--;97}98return retVal;99}100101public static final int readBigEndian(byte[] data, int pos, int size) {102int retVal = 0;103int shifter = (size-1)*8;104while (size > 0) {105retVal += (data[pos] & 0xff) << shifter;106shifter -= 8;107pos++;108size--;109}110return retVal;111}112113/**114* Writes a two byte integer value to a OutputStream.115*116* @param val the integer value. It will lose the high-order two bytes.117* @param os the OutputStream to write to118* @throws IOException if an error occurs while writing to the OutputStream119*/120public static final void writeInt(int val, OutputStream os)121throws IOException {122os.write(val>>>8);123os.write(val);124}125126/**127* Writes a two byte integer value to a byte array.128*129* @param val the integer value. It will lose the high-order two bytes.130* @param dest the byte array to write to131* @param pos the offset to start writing to132*/133public static final int writeInt(int val, byte[] dest, int pos) {134dest[pos++] = (byte)(val>>>8);135dest[pos++] = (byte)val;136return pos;137}138139/**140* Reads a two byte integer value from an InputStream.141*142* @param is the InputStream to read from143* @return the integer value144* @throws IOException if some errors occurs while reading the integer145* bytes.146*/147public static final int readInt(InputStream is) throws IOException {148return (((0xFF & is.read()) << 8)149| (0xFF & is.read()));150}151152/**153* Reads a two byte integer value from a byte array.154*155* @param src the byte arra to read from156* @param pos the offset to start reading from157* @return the integer value158*/159public static final int readInt(byte[] src, int pos) {160return ((0xFF & src[pos])<<8 | (0xFF & src[pos+1]));161}162163/**164* Blocks till the required number of bytes have been read from the165* input stream.166*167* @param is the InputStream to read from168* @param buffer the buffer to store the bytes into169* @throws EOFException if EOF is reached before all bytes are170* read.171* @throws IOException is an error occurs while reading172*/173public static final void readFully(InputStream is, byte[] buffer)174throws IOException {175readFully(is, buffer, 0, buffer.length);176}177178/**179* Blocks till the required number of bytes have been read from the180* input stream.181*182* @param is the InputStream to read from183* @param buffer the buffer to store the bytes into184* @param offset the offset to start storing at185* @param len the number of bytes to read186* @throws EOFException if EOF is reached before all bytes are187* read.188* @throws IOException is an error occurs while reading189*/190public static final void readFully(InputStream is,191byte[] buffer, int offset, int len)192throws IOException {193int temp;194while (len > 0) {195temp = is.read(buffer, offset, len);196if (temp == -1)197throw new EOFException("Cannot read all "198+ len199+ " bytes needed to form this token!");200offset += temp;201len -= temp;202}203}204205public static final void debug(String str) {206System.err.print(str);207}208209public static final String getHexBytes(byte[] bytes) {210return getHexBytes(bytes, 0, bytes.length);211}212213public static final String getHexBytes(byte[] bytes, int len) {214return getHexBytes(bytes, 0, len);215}216217public static final String getHexBytes(byte[] bytes, int pos, int len) {218StringBuilder sb = new StringBuilder();219for (int i = pos; i < (pos+len); i++) {220int b1 = (bytes[i]>>4) & 0x0f;221int b2 = bytes[i] & 0x0f;222223sb.append(Integer.toHexString(b1));224sb.append(Integer.toHexString(b2));225sb.append(' ');226}227return sb.toString();228}229230}231232233