View Javadoc
1   /*
2   Copyright (c) 2016 James Ahlborn
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package com.healthmarketscience.jackcess.util;
18  
19  import java.io.IOException;
20  import java.nio.ByteBuffer;
21  import java.nio.MappedByteBuffer;
22  import java.nio.channels.FileChannel;
23  import java.nio.channels.FileLock;
24  import java.nio.channels.NonWritableChannelException;
25  import java.nio.channels.ReadableByteChannel;
26  import java.nio.channels.WritableByteChannel;
27  import com.healthmarketscience.jackcess.Database;
28  
29  /**
30   * Wrapper for existing FileChannel which is read-only.
31   * <p>
32   * Implementation note: this class is optimized for use with {@link Database}.
33   * Therefore not all methods may be implemented.
34   *
35   * @author James Ahlborn
36   * @usage _advanced_class_
37   */
38  public class ReadOnlyFileChannel extends FileChannel
39  {
40    private final FileChannel _delegate;
41  
42    public ReadOnlyFileChannel(FileChannel delegate) {
43      _delegate = delegate;
44    }
45  
46    @Override
47    public int read(ByteBuffer dst) throws IOException {
48      return _delegate.read(dst);
49    }
50  
51    @Override
52    public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
53      return _delegate.read(dsts, offset, length);
54    }
55  
56    @Override
57    public int read(ByteBuffer dst, long position) throws IOException {
58      return _delegate.read(dst, position);
59    }
60  
61    @Override
62    public long position() throws IOException {
63      return _delegate.position();
64    }
65  
66    @Override
67    public FileChannel position(long newPosition) throws IOException {
68      _delegate.position(newPosition);
69      return this;
70    }
71  
72    @Override
73    public long size() throws IOException {
74      return _delegate.size();
75    }
76  
77    @Override
78    public FileChannel truncate(long size) {
79      throw new NonWritableChannelException();
80    }
81  
82    @Override
83    public void force(boolean metaData) {
84      // do nothing
85    }
86  
87    @Override
88    public long transferTo(long position, long count, WritableByteChannel target)
89      throws IOException
90    {
91      return _delegate.transferTo(position, count, target);
92    }
93  
94    @Override
95    public long transferFrom(ReadableByteChannel src, long position, long count)
96    {
97      throw new NonWritableChannelException();
98    }
99  
100   @Override
101   public int write(ByteBuffer src, long position) {
102     throw new NonWritableChannelException();
103   }
104 
105 
106   @Override
107   public int write(ByteBuffer src) {
108     throw new NonWritableChannelException();
109   }
110 
111   @Override
112   public long write(ByteBuffer[] srcs, int offset, int length)
113   {
114     throw new NonWritableChannelException();
115   }
116 
117   @Override
118   public MappedByteBuffer map(MapMode mode, long position, long size)
119   {
120     throw new UnsupportedOperationException();
121   }
122 
123   @Override
124   public FileLock lock(long position, long size, boolean shared)
125   {
126     throw new UnsupportedOperationException();
127   }
128 
129   @Override
130   public FileLock tryLock(long position, long size, boolean shared)
131   {
132     throw new UnsupportedOperationException();
133   }
134 
135   @Override
136   protected void implCloseChannel() throws IOException {
137     _delegate.close();
138   }
139 }