• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 Square, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package okio;
17 
18 import java.nio.charset.Charset;
19 
20 final class Util {
21   /** A cheap and type-safe constant for the UTF-8 Charset. */
22   public static final Charset UTF_8 = Charset.forName("UTF-8");
23 
Util()24   private Util() {
25   }
26 
checkOffsetAndCount(long size, long offset, long byteCount)27   public static void checkOffsetAndCount(long size, long offset, long byteCount) {
28     if ((offset | byteCount) < 0 || offset > size || size - offset < byteCount) {
29       throw new ArrayIndexOutOfBoundsException(
30           String.format("size=%s offset=%s byteCount=%s", size, offset, byteCount));
31     }
32   }
33 
reverseBytesShort(short s)34   public static short reverseBytesShort(short s) {
35     int i = s & 0xffff;
36     int reversed = (i & 0xff00) >>> 8
37         |          (i & 0x00ff)  << 8;
38     return (short) reversed;
39   }
40 
reverseBytesInt(int i)41   public static int reverseBytesInt(int i) {
42     return (i & 0xff000000) >>> 24
43         |  (i & 0x00ff0000) >>>  8
44         |  (i & 0x0000ff00)  <<  8
45         |  (i & 0x000000ff)  << 24;
46   }
47 
reverseBytesLong(long v)48   public static long reverseBytesLong(long v) {
49     return (v & 0xff00000000000000L) >>> 56
50         |  (v & 0x00ff000000000000L) >>> 40
51         |  (v & 0x0000ff0000000000L) >>> 24
52         |  (v & 0x000000ff00000000L) >>>  8
53         |  (v & 0x00000000ff000000L)  <<  8
54         |  (v & 0x0000000000ff0000L)  << 24
55         |  (v & 0x000000000000ff00L)  << 40
56         |  (v & 0x00000000000000ffL)  << 56;
57   }
58 
59   /**
60    * Throws {@code t}, even if the declared throws clause doesn't permit it.
61    * This is a terrible – but terribly convenient – hack that makes it easy to
62    * catch and rethrow exceptions after cleanup. See Java Puzzlers #43.
63    */
sneakyRethrow(Throwable t)64   public static void sneakyRethrow(Throwable t) {
65     Util.<Error>sneakyThrow2(t);
66   }
67 
68   @SuppressWarnings("unchecked")
sneakyThrow2(Throwable t)69   private static <T extends Throwable> void sneakyThrow2(Throwable t) throws T {
70     throw (T) t;
71   }
72 
arrayRangeEquals( byte[] a, int aOffset, byte[] b, int bOffset, int byteCount)73   public static boolean arrayRangeEquals(
74       byte[] a, int aOffset, byte[] b, int bOffset, int byteCount) {
75     for (int i = 0; i < byteCount; i++) {
76       if (a[i + aOffset] != b[i + bOffset]) return false;
77     }
78     return true;
79   }
80 }
81