Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
SevenZipContentReadStream.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 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.autopsy.modules.embeddedfileextractor;
20 
21 import java.io.IOException;
22 import java.util.logging.Level;
23 import net.sf.sevenzipjbinding.IInStream;
24 import net.sf.sevenzipjbinding.SevenZipException;
25 import org.openide.util.NbBundle;
27 import org.sleuthkit.datamodel.ReadContentInputStream;
28 
33 class SevenZipContentReadStream implements IInStream {
34 
35  private ReadContentInputStream wrapped;
36  private long length;
37 
38  private static final Logger logger = Logger.getLogger(SevenZipContentReadStream.class.getName());
39 
40  public SevenZipContentReadStream(ReadContentInputStream wrapped) {
41  this.wrapped = wrapped;
42  this.length = wrapped.getLength();
43  }
44 
45  @Override
46  public long seek(long offset, int origin) throws SevenZipException {
47  long curPosition = wrapped.getCurPosition();
48  long newPosition = curPosition;
49  switch (origin) {
50  case SEEK_CUR:
51  newPosition = wrapped.seek(curPosition + offset);
52  break;
53  case SEEK_END:
54  //(offset <= 0) offset is set from EOF
55  newPosition = wrapped.seek(length + offset);
56  break;
57  case SEEK_SET:
58  newPosition = wrapped.seek(offset);
59  break;
60  default:
61  throw new IllegalArgumentException(
62  NbBundle.getMessage(this.getClass(), "SevenZipContentReadStream.seek.exception.invalidOrigin",
63  origin));
64  }
65 
66  return newPosition;
67 
68  }
69 
70  @Override
71  public int read(byte[] bytes) throws SevenZipException {
72  //Reads at least 1 and maximum data.length from the in-stream.
73  //If data.length == 0 0 should be returned.
74  //If data.length != 0, then return value 0 indicates end-of-stream (EOF). This means no more bytes can be read from the stream.
75  //This function is allowed to read less than number of remaining bytes in stream and less then data.length.
76  if (bytes.length == 0) {
77  return 0;
78  }
79 
80  try {
81  int readBytes = wrapped.read(bytes);
82  if (readBytes < 1) {
83  return 0;
84  }
85  return readBytes;
86 
87  } catch (IOException ex) {
88  // This is only a warning because the file may be deleted or otherwise corrupt
89  String msg = NbBundle.getMessage(this.getClass(), "SevenZipContentReadStream.read.exception.errReadStream");
90  logger.log(Level.WARNING, msg, ex);
91  throw new SevenZipException(msg, ex);
92  }
93  }
94 
100  public void close() throws IOException {
101  wrapped.close();
102  }
103 }

Copyright © 2012-2022 Basis Technology. Generated on: Tue Feb 6 2024
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.