You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReadOnlyFileChannel.java 3.6KB

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