1 /**
2 * This file Copyright (c) 2014-2015 Magnolia International
3 * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4 *
5 *
6 * This file is dual-licensed under both the Magnolia
7 * Network Agreement and the GNU General Public License.
8 * You may elect to use one or the other of these licenses.
9 *
10 * This file is distributed in the hope that it will be
11 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14 * Redistribution, except as permitted by whichever of the GPL
15 * or MNA you select, is prohibited.
16 *
17 * 1. For the GPL license (GPL), you can redistribute and/or
18 * modify this file under the terms of the GNU General
19 * Public License, Version 3, as published by the Free Software
20 * Foundation. You should have received a copy of the GNU
21 * General Public License, Version 3 along with this program;
22 * if not, write to the Free Software Foundation, Inc., 51
23 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 * 2. For the Magnolia Network Agreement (MNA), this file
26 * and the accompanying materials are made available under the
27 * terms of the MNA which accompanies this distribution, and
28 * is available at http://www.magnolia-cms.com/mna.html
29 *
30 * Any modifications to this file must keep this entire header
31 * intact.
32 *
33 */
34 package info.magnolia.dam.preview.jcodec;
35
36 import java.io.BufferedInputStream;
37 import java.io.FileNotFoundException;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.nio.ByteBuffer;
41 import java.nio.channels.Channels;
42 import java.nio.channels.ReadableByteChannel;
43
44 import org.jcodec.common.SeekableByteChannel;
45
46 /**
47 * This implementation is not thread safe. It's just a simple buffer for forward reading, writing or setting position back is not supported. Don't even think about it.
48 */
49 public class InputStreamSeekableChannel implements SeekableByteChannel {
50
51 private ReadableByteChannel ch;
52 private long position = 0;
53 private InputStream is;
54
55 public InputStreamSeekableChannel(InputStream is) throws FileNotFoundException {
56 this.is = new BufferedInputStream(is);
57 this.ch = Channels.newChannel(this.is);
58 this.is.mark(512 * 1024);
59 }
60
61 @Override
62 public int read(ByteBuffer arg0) throws IOException {
63 int num = ch.read(arg0);
64 position += num;
65 return num;
66 }
67
68 @Override
69 public void close() throws IOException {
70 ch.close();
71 }
72
73 @Override
74 public boolean isOpen() {
75 return ch.isOpen();
76 }
77
78 @Override
79 public int write(ByteBuffer arg0) throws IOException {
80 throw new UnsupportedOperationException();
81 }
82
83 @Override
84 public long position() throws IOException {
85 return position;
86 }
87
88 @Override
89 public SeekableByteChannel position(long newPosition) throws IOException {
90 if (position == newPosition) {
91 return this;
92 }
93
94 long skip = newPosition - position;
95 if (skip < 0) {
96 is.reset();
97 is.skip(newPosition);
98 position = newPosition;
99 // throw new UnsupportedOperationException("not going backward to " + newPosition + " from " + position);
100 }
101 is.skip(skip);
102 return this;
103 }
104
105 @Override
106 public long size() throws IOException {
107 // ... which is pbly not true ... we should not try to read too many frames here or use some more complicated impl to know full length (not possible w/o video metadata => change of imaging API).
108 return Integer.MAX_VALUE;
109 }
110
111 @Override
112 public SeekableByteChannel truncate(long size) throws IOException {
113 throw new UnsupportedOperationException("no truncate support here.");
114 }
115 }