Sleuth Kit Java Bindings (JNI)  4.3
Java bindings for using The Sleuth Kit
ReadContentInputStream.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2013 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.datamodel;
20 
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 
29 public class ReadContentInputStream extends InputStream {
30 
31  private long currentOffset;
32  private long contentSize;
33  private Content content;
34  private static final Logger logger = Logger.getLogger(ReadContentInputStream.class.getName());
35 
36  public ReadContentInputStream(Content content) {
37  this.content = content;
38  this.currentOffset = 0;
39  this.contentSize = content.getSize();
40  }
41 
42  @Override
43  public int read() throws IOException {
44  byte[] buff = new byte[1];
45  return (read(buff) != -1) ? buff[0] : -1;
46  }
47 
48  @Override
49  public int read(byte[] b) throws IOException {
50  return read(b, 0, b.length);
51  }
52 
53  @Override
54  public int read(byte[] b, int off, int len) throws IOException {
55 
56  final int buffLen = b.length;
57  //must return 0 for zero-length arrays
58  if (buffLen == 0 || len == 0) {
59  return 0;
60  }
61 
62  //would get an error from TSK if we try to read an empty file
63  if (contentSize == 0) {
64  return -1;
65  }
66 
67  // check off. Must be in bounds of buffer
68  if (off < 0 || off >= buffLen) {
69  return -1;
70  }
71 
72  //eof, no data remains to be read
73  if (currentOffset >= contentSize) {
74  return -1;
75  }
76 
77  // Is the file big enough for the full request?
78  int lenToRead = (int) Math.min(contentSize - currentOffset, len);
79 
80  // is the buffer big enough?
81  lenToRead = Math.min(lenToRead, buffLen - off);
82 
83  byte[] retBuf = null;
84  if (off == 0) {
85  //write directly to user buffer
86  retBuf = b;
87  } else {
88  //write to a temp buffer, then copy to user buffer
89  retBuf = new byte[lenToRead];
90  }
91  try {
92  final int lenRead = content.read(retBuf, currentOffset, lenToRead);
93 
94  if (lenRead == 0 || lenRead == -1) {
95  //error or no more bytes to read, report EOF
96  return -1;
97  } else {
98  currentOffset += lenRead;
99 
100  //if read into user-specified offset, copy back from temp buffer to user
101  if (off != 0) {
102  System.arraycopy(retBuf, 0, b, off, lenRead);
103  }
104 
105  return lenRead;
106  }
107  } catch (TskCoreException ex) {
108  logger.log(Level.WARNING, ("Error reading content into stream: " //NON-NLS
109  + content.getId()) + ": " + content.getName()
110  + ", at offset " + currentOffset + ", length to read: " + lenToRead, ex); //NON-NLS
111  throw new IOException(ex);
112  }
113 
114  }
115 
116  @Override
117  public int available() throws IOException {
118  long len = contentSize - currentOffset;
119  if (len < 0) {
120  return 0;
121  }
122  return (int) len;
123  }
124 
125  @Override
126  public long skip(long n) throws IOException {
127  //more efficient skip() implementation than superclass
128  //as it does not involve reads
129  long toSkip = Math.min(n, contentSize - currentOffset); //allow to skip to EOF
130  currentOffset += toSkip;
131  return toSkip;
132  //0 1 2 3 4 5 len: 6
133  }
134 
135  @Override
136  public void close() throws IOException {
137  super.close();
138  //nothing to be done currently, file handles are closed when content is gc'ed
139  }
140 
141  @Override
142  public boolean markSupported() {
143  return false;
144  }
145 
147 
152  public long getLength() {
153  return contentSize;
154  }
155 
161  public long getCurPosition() {
162  return currentOffset;
163  }
164 
173  public long seek(long newPosition) {
174  if (newPosition < 0) {
175  throw new IllegalArgumentException("Illegal negative new position in the stream");
176  }
177 
178  currentOffset = Math.min(newPosition, contentSize);
179  return currentOffset;
180 
181  }
182 }
int read(byte[] buf, long offset, long len)
long getLength()
additional methods to facilitate stream seeking

Copyright © 2011-2015 Brian Carrier. (carrier -at- sleuthkit -dot- org)
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.