View Javadoc
1   /**
2    * This file Copyright (c) 2014-2017 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   * @deprecated since 2.1.8 - use {@link org.jcodec.common.ByteBufferSeekableByteChannel} instead.
49   */
50  @Deprecated
51  public class InputStreamSeekableChannel implements SeekableByteChannel {
52  
53      private ReadableByteChannel ch;
54      private long position = 0;
55      private InputStream is;
56  
57      public InputStreamSeekableChannel(InputStream is) throws FileNotFoundException {
58          this.is = new BufferedInputStream(is);
59          this.ch = Channels.newChannel(this.is);
60          this.is.mark(512 * 1024);
61      }
62  
63      @Override
64      public int read(ByteBuffer arg0) throws IOException {
65          int num = ch.read(arg0);
66          position += num;
67          return num;
68      }
69  
70      @Override
71      public void close() throws IOException {
72          ch.close();
73      }
74  
75      @Override
76      public boolean isOpen() {
77          return ch.isOpen();
78      }
79  
80      @Override
81      public int write(ByteBuffer arg0) throws IOException {
82          throw new UnsupportedOperationException();
83      }
84  
85      @Override
86      public long position() throws IOException {
87          return position;
88      }
89  
90      @Override
91      public SeekableByteChannel position(long newPosition) throws IOException {
92          if (position == newPosition) {
93              return this;
94          }
95  
96          long skip = newPosition - position;
97          if (skip < 0) {
98              is.reset();
99              is.skip(newPosition);
100             position = newPosition;
101             // throw new UnsupportedOperationException("not going backward to " + newPosition + " from " + position);
102         }
103         is.skip(skip);
104         return this;
105     }
106 
107     @Override
108     public long size() throws IOException {
109         // ... 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).
110         return Integer.MAX_VALUE;
111     }
112 
113     @Override
114     public SeekableByteChannel truncate(long size) throws IOException {
115         throw new UnsupportedOperationException("no truncate support here.");
116     }
117 }