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.util;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31
32 /**
33 * input stream to get only a part of a file
34 *
35 */
36 public class ChunkedFileInputStream extends FileInputStream {
37
38 /**
39 * starting offset
40 */
41 private long offset;
42 /**
43 * the current offset
44 */
45 private long currentOffset;
46 /**
47 * ending offset
48 */
49 private long endOffset;
50
51 private static final Logger logger = LoggerFactory.getLogger(ChunkedFileInputStream.class);
52
53 /**
54 * constructor
55 *
56 * @param name the name of the file
57 * @throws FileNotFoundException if the file was not found
58 */
59 public ChunkedFileInputStream(String name) throws FileNotFoundException {
60 this(name != null ? new File(name) : null, 0, 0);
61 }
62
63 /**
64 * constructor
65 *
66 * @param file the file to load
67 * @param offset the starting offset
68 * @param endOffset the ending offset
69 * @throws FileNotFoundException if the requested file was not found
70 */
71 public ChunkedFileInputStream(File file, long offset, long endOffset)
72 throws FileNotFoundException {
73 super(file);
74 this.offset = offset;
75 this.currentOffset = offset;
76 this.endOffset = endOffset == 0 ? file.length() : endOffset;
77 if (offset != 0) {
78 logger.debug("skipping first {} bytes", offset);
79 try {
80 this.skip(offset);
81 } catch (IOException e) {
82 logger.error(e.getMessage(), e);
83 }
84 }
85 }
86
87 /**
88 * read the next byte
89 *
90 * @return the next byte or -1 if the expected offset has been reached
91 */
92 public int read() throws IOException {
93 this.currentOffset++;
94 if (currentOffset > endOffset) {
95 return -1;
96 }
97 return super.read();
98 }
99
100 /**
101 * get the ending offset
102 *
103 * @return the ending offset
104 */
105 public long getEndOffset() {
106 return endOffset;
107 }
108
109 /**
110 * set the ending offset
111 *
112 * @param endOffset the ending offset
113 */
114 public void setEndOffset(long endOffset) {
115 this.endOffset = endOffset;
116 }
117
118 /***
119 * get the starting offset
120 *
121 * @return the starting offset
122 */
123 public long getOffset() {
124 return offset;
125 }
126
127 /**
128 * set the starting offset
129 *
130 * @param offset the starting offset
131 */
132 public void setOffset(long offset) {
133 this.offset = offset;
134 }
135
136 }