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) throws IOException {
79      throw new NonWritableChannelException();
80    }
81  
82    @Override
83    public void force(boolean metaData) throws IOException {
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      throws IOException 
97    {
98      throw new NonWritableChannelException();
99    }
100 
101   @Override
102   public int write(ByteBuffer src, long position) throws IOException {
103     throw new NonWritableChannelException();
104   }
105 
106 
107   @Override
108   public int write(ByteBuffer src) throws IOException {
109     throw new NonWritableChannelException();
110   }
111 
112   @Override
113   public long write(ByteBuffer[] srcs, int offset, int length) 
114     throws IOException 
115   {
116     throw new NonWritableChannelException();
117   }
118 
119   @Override
120   public MappedByteBuffer map(MapMode mode, long position, long size) 
121     throws IOException 
122   {
123     throw new UnsupportedOperationException();
124   }
125 
126   @Override
127   public FileLock lock(long position, long size, boolean shared) 
128     throws IOException 
129   {
130     throw new UnsupportedOperationException();
131   }
132 
133   @Override
134   public FileLock tryLock(long position, long size, boolean shared) 
135     throws IOException 
136   {
137     throw new UnsupportedOperationException();
138   }
139 
140   @Override
141   protected void implCloseChannel() throws IOException {
142     _delegate.close();
143   }
144 }