github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/clients/android/src/org/camlistore/Util.java (about)

     1  /*
     2  Copyright 2011 Google 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  
    17  package org.camlistore;
    18  
    19  import java.io.BufferedInputStream;
    20  import java.io.ByteArrayOutputStream;
    21  import java.io.File;
    22  import java.io.FileDescriptor;
    23  import java.io.FileInputStream;
    24  import java.io.FileOutputStream;
    25  import java.io.IOException;
    26  import java.io.InputStream;
    27  import java.security.MessageDigest;
    28  import java.security.NoSuchAlgorithmException;
    29  import java.util.concurrent.locks.ReentrantLock;
    30  
    31  import android.os.AsyncTask;
    32  import android.os.Looper;
    33  import android.util.Base64;
    34  import android.util.Log;
    35  
    36  public class Util {
    37      private static final String TAG = "Camli_Util";
    38  
    39      public static String slurp(InputStream in) throws IOException {
    40          StringBuilder sb = new StringBuilder();
    41          byte[] b = new byte[4096];
    42          for (int n; (n = in.read(b)) != -1;) {
    43              sb.append(new String(b, 0, n));
    44          }
    45          return sb.toString();
    46      }
    47  
    48      public static byte[] slurpToByteArray(InputStream inputStream) throws IOException {
    49          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    50          byte[] buffer = new byte[4096];
    51          for (int numRead; (numRead = inputStream.read(buffer)) != -1;) {
    52              outputStream.write(buffer, 0, numRead);
    53          }
    54          return outputStream.toByteArray();
    55      }
    56  
    57      public static void copyFile(File fromFile, File toFile) throws IOException {
    58          FileInputStream inputStream = new FileInputStream(fromFile);
    59          FileOutputStream outputStream = new FileOutputStream(toFile);
    60          byte[] buffer = new byte[4096];
    61          for (int numRead; (numRead = inputStream.read(buffer)) != -1;)
    62              outputStream.write(buffer, 0, numRead);
    63          inputStream.close();
    64          outputStream.close();
    65      }
    66  
    67      public static void runAsync(final Runnable r) {
    68          new AsyncTask<Void, Void, Void>() {
    69              @Override
    70              protected Void doInBackground(Void... unused) {
    71                  r.run();
    72                  return null;
    73              }
    74          }.execute();
    75      }
    76  
    77      public static boolean onMainThread() {
    78          return Looper.myLooper() == Looper.getMainLooper();
    79      }
    80  
    81      public static void assertMainThread() {
    82          if (!onMainThread()) {
    83              throw new RuntimeException("Assert: unexpected call off the main thread");
    84          }
    85      }
    86  
    87      public static void assertNotMainThread() {
    88          if (onMainThread()) {
    89              throw new RuntimeException("Assert: unexpected call on main thread");
    90          }
    91      }
    92  
    93      // Asserts that |lock| is held by the current thread.
    94      public static void assertLockIsHeld(ReentrantLock lock) {
    95          if (!lock.isHeldByCurrentThread()) {
    96              throw new RuntimeException("Assert: mandatory lock isn't held by current thread");
    97          }
    98      }
    99  
   100      // Asserts that |lock| is not held by the current thread.
   101      public static void assertLockIsNotHeld(ReentrantLock lock) {
   102          if (lock.isHeldByCurrentThread()) {
   103              throw new RuntimeException("Assert: lock is held by current thread but shouldn't be");
   104          }
   105      }
   106  
   107      private static final String HEX = "0123456789abcdef";
   108  
   109      public static String getHex(byte[] raw) {
   110          if (raw == null) {
   111              return null;
   112          }
   113          final StringBuilder hex = new StringBuilder(2 * raw.length);
   114          for (final byte b : raw) {
   115              hex.append(HEX.charAt((b & 0xF0) >> 4)).append(
   116                      HEX.charAt((b & 0x0F)));
   117          }
   118          return hex.toString();
   119      }
   120  
   121      // Requires that the fd be seeked to the beginning.
   122      public static String getSha1(FileDescriptor fd) {
   123          MessageDigest md;
   124          try {
   125              md = MessageDigest.getInstance("SHA-1");
   126          } catch (NoSuchAlgorithmException e) {
   127              throw new RuntimeException(e);
   128          }
   129          byte[] b = new byte[4096];
   130          FileInputStream fis = new FileInputStream(fd);
   131          InputStream is = new BufferedInputStream(fis, 4096);
   132          try {
   133              for (int n; (n = is.read(b)) != -1;) {
   134                  md.update(b, 0, n);
   135              }
   136          } catch (IOException e) {
   137              Log.w(TAG, "IOException while computing SHA-1");
   138              return null;
   139          }
   140          byte[] sha1hash = new byte[40];
   141          sha1hash = md.digest();
   142          return getHex(sha1hash);
   143      }
   144  
   145      public static String getBasicAuthHeaderValue(String username, String password) {
   146          return "Basic " + Base64.encodeToString((username + ":" + password).getBytes(),
   147                                                  Base64.NO_WRAP);
   148      }
   149  }