• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.messaging.util.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 
CountedDataInputStream(InputStream in)35     protected CountedDataInputStream(InputStream in) {
36         super(in);
37     }
38 
getReadByteCount()39     public int getReadByteCount() {
40         return mCount;
41     }
42 
43     @Override
read(byte[] b)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
read(byte[] b, int off, int len)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
read()58     public int read() throws IOException {
59         int r = in.read();
60         mCount += (r >= 0) ? 1 : 0;
61         return r;
62     }
63 
64     @Override
skip(long length)65     public long skip(long length) throws IOException {
66         long skip = in.skip(length);
67         mCount += skip;
68         return skip;
69     }
70 
skipOrThrow(long length)71     public void skipOrThrow(long length) throws IOException {
72         if (skip(length) != length) {
73             throw new EOFException();
74         }
75     }
76 
skipTo(long target)77     public void skipTo(long target) throws IOException {
78         long cur = mCount;
79         long diff = target - cur;
80         assert(diff >= 0);
81         skipOrThrow(diff);
82     }
83 
readOrThrow(byte[] b, int off, int len)84     public void readOrThrow(byte[] b, int off, int len) throws IOException {
85         int r = read(b, off, len);
86         if (r != len) {
87             throw new EOFException();
88         }
89     }
90 
readOrThrow(byte[] b)91     public void readOrThrow(byte[] b) throws IOException {
92         readOrThrow(b, 0, b.length);
93     }
94 
setByteOrder(ByteOrder order)95     public void setByteOrder(ByteOrder order) {
96         mByteBuffer.order(order);
97     }
98 
getByteOrder()99     public ByteOrder getByteOrder() {
100         return mByteBuffer.order();
101     }
102 
readShort()103     public short readShort() throws IOException {
104         readOrThrow(mByteArray, 0 , 2);
105         mByteBuffer.rewind();
106         return mByteBuffer.getShort();
107     }
108 
readUnsignedShort()109     public int readUnsignedShort() throws IOException {
110         return readShort() & 0xffff;
111     }
112 
readInt()113     public int readInt() throws IOException {
114         readOrThrow(mByteArray, 0 , 4);
115         mByteBuffer.rewind();
116         return mByteBuffer.getInt();
117     }
118 
readUnsignedInt()119     public long readUnsignedInt() throws IOException {
120         return readInt() & 0xffffffffL;
121     }
122 
readLong()123     public long readLong() throws IOException {
124         readOrThrow(mByteArray, 0 , 8);
125         mByteBuffer.rewind();
126         return mByteBuffer.getLong();
127     }
128 
readString(int n)129     public String readString(int n) throws IOException {
130         byte buf[] = new byte[n];
131         readOrThrow(buf);
132         return new String(buf, "UTF8");
133     }
134 
readString(int n, Charset charset)135     public String readString(int n, Charset charset) throws IOException {
136         byte buf[] = new byte[n];
137         readOrThrow(buf);
138         return new String(buf, charset);
139     }
140 }
141