2 * Copyright 2006 ProductiveMe Inc.
3 * Copyright 2013 JetBrains s.r.o.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 import java.io.DataInput;
21 import java.io.IOException;
27 public class BitsUtil {
28 public static int revertBytesOfShort( short shortValue ){
29 return ((shortValue << 8) & 0xff00) + ((shortValue >> 8) & 0xff);
31 public static long revertBytesOfInt( int intValue ){
32 long result = (intValue & 0x000000ff);
34 result += ((intValue & 0x0000ff00) << 8) + ((intValue & 0x00ff0000) >> 8) + ((intValue >> 24) & 0xff);
37 public static int unsignedByte( byte byteValue ){
38 int result = byteValue;
39 return (result & 0xff);
41 private static String toHexString( long value, int size ){
42 String strValue = Long.toHexString( value );
43 if ( strValue.length() > size ){
44 strValue = strValue.substring( strValue.length() - size );
46 StringBuffer buffer = new StringBuffer( strValue.length() + 1 + size );
47 buffer.append( "0x" );
48 int dif = size - strValue.length();
49 for ( int i = 0; i < dif; ++i ){
52 buffer.append( strValue );
53 return buffer.toString();
56 public static String intToHexString( long value ){
57 return toHexString( value, 8 );
59 public static String shortToHexString( int value ){
60 return toHexString( value, 4 );
62 public static String byteToHexString( int value ){
63 return toHexString( value, 2 );
66 public static char readChar(DataInput stream) throws IOException {
67 int b1 = stream.readByte();
68 int b2 = stream.readByte();
69 return (char) (b1 + (b2 << 8));