1 /*
2 * Licensed to The Apereo Foundation under one or more contributor license
3 * agreements. See the NOTICE file distributed with this work for additional
4 * information regarding copyright ownership.
5 *
6 *
7 * The Apereo Foundation licenses this file to you under the Educational
8 * Community License, Version 2.0 (the "License"); you may not use this file
9 * except in compliance with the License. You may obtain a copy of the License
10 * at:
11 *
12 * http://opensource.org/licenses/ecl2.txt
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 * License for the specific language governing permissions and limitations under
18 * the License.
19 *
20 */
21
22 package org.opencastproject.ingest.impl;
23
24 import com.google.common.base.Preconditions;
25
26 import java.io.FilterInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29
30 public class ZipEntryInputStream extends FilterInputStream {
31
32 /** Length of the zip entry */
33 private long bytesToRead = 0;
34
35 /**
36 * Creates a wrapper around the input stream <code>in</code> and reads the given number of bytes from it.
37 *
38 * @param in
39 * the input stream
40 * @param length
41 * the number of bytes to read
42 */
43 public ZipEntryInputStream(InputStream in, long length) {
44 super(in);
45 bytesToRead = length;
46 Preconditions.checkNotNull(in);
47 }
48
49 @Override
50 public int read() throws IOException {
51 if (bytesToRead == 0)
52 return -1;
53 bytesToRead--;
54 int byteContent = in.read();
55 if (byteContent == -1)
56 throw new IOException("Zip entry is shorter than anticipated");
57 return byteContent;
58 }
59
60 @Override
61 public void close() throws IOException {
62 // This stream mustn't be closed because it handles the Zip entry parts of the given Zip input stream
63 // make sure the given zip input stream is closed after reading
64 }
65
66 }