Home | History | Annotate | Download | only in exif
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      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 com.android.gallery3d.exif;
     18 
     19 import java.io.EOFException;
     20 import java.io.FilterInputStream;
     21 import java.io.IOException;
     22 import java.io.InputStream;
     23 import java.nio.ByteBuffer;
     24 import java.nio.ByteOrder;
     25 import java.nio.charset.Charset;
     26 
     27 class CountedDataInputStream extends FilterInputStream {
     28 
     29     private int mCount = 0;
     30 
     31     // allocate a byte buffer for a long value;
     32     private final byte mByteArray[] = new byte[8];
     33     private final ByteBuffer mByteBuffer = ByteBuffer.wrap(mByteArray);
     34 
     35     protected CountedDataInputStream(InputStream in) {
     36         super(in);
     37     }
     38 
     39     public int getReadByteCount() {
     40         return mCount;
     41     }
     42 
     43     @Override
     44     public int read(byte[] b) throws IOException {
     45         int r = in.read(b);
     46         mCount += (r >= 0) ? r : 0;
     47         return r;
     48     }
     49 
     50     @Override
     51     public int read(byte[] b, int off, int len) throws IOException {
     52         int r = in.read(b, off, len);
     53         mCount += (r >= 0) ? r : 0;
     54         return r;
     55     }
     56 
     57     @Override
     58     public int read() throws IOException {
     59         int r = in.read();
     60         mCount += (r >= 0) ? 1 : 0;
     61         return r;
     62     }
     63 
     64     @Override
     65     public long skip(long length) throws IOException {
     66         long skip = in.skip(length);
     67         mCount += skip;
     68         return skip;
     69     }
     70 
     71     public void skipOrThrow(long length) throws IOException {
     72         if (skip(length) != length) throw new EOFException();
     73     }
     74 
     75     public void skipTo(long target) throws IOException {
     76         long cur = mCount;
     77         long diff = target - cur;
     78         assert(diff >= 0);
     79         skipOrThrow(diff);
     80     }
     81 
     82     public void readOrThrow(byte[] b, int off, int len) throws IOException {
     83         int r = read(b, off, len);
     84         if (r != len) throw new EOFException();
     85     }
     86 
     87     public void readOrThrow(byte[] b) throws IOException {
     88         readOrThrow(b, 0, b.length);
     89     }
     90 
     91     public void setByteOrder(ByteOrder order) {
     92         mByteBuffer.order(order);
     93     }
     94 
     95     public ByteOrder getByteOrder() {
     96         return mByteBuffer.order();
     97     }
     98 
     99     public short readShort() throws IOException {
    100         readOrThrow(mByteArray, 0 ,2);
    101         mByteBuffer.rewind();
    102         return mByteBuffer.getShort();
    103     }
    104 
    105     public int readUnsignedShort() throws IOException {
    106         return readShort() & 0xffff;
    107     }
    108 
    109     public int readInt() throws IOException {
    110         readOrThrow(mByteArray, 0 , 4);
    111         mByteBuffer.rewind();
    112         return mByteBuffer.getInt();
    113     }
    114 
    115     public long readUnsignedInt() throws IOException {
    116         return readInt() & 0xffffffffL;
    117     }
    118 
    119     public long readLong() throws IOException {
    120         readOrThrow(mByteArray, 0 , 8);
    121         mByteBuffer.rewind();
    122         return mByteBuffer.getLong();
    123     }
    124 
    125     public String readString(int n) throws IOException {
    126         byte buf[] = new byte[n];
    127         readOrThrow(buf);
    128         return new String(buf, "UTF8");
    129     }
    130 
    131     public String readString(int n, Charset charset) throws IOException {
    132         byte buf[] = new byte[n];
    133         readOrThrow(buf);
    134         return new String(buf, charset);
    135     }
    136 }