|
| 1 | +/* |
| 2 | + * Copyright 2019 Google LLC |
| 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 | +package com.google.cloud.storage.contrib.nio; |
| 17 | + |
| 18 | +import com.google.common.base.Preconditions; |
| 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.ReadableByteChannel; |
| 25 | +import java.nio.channels.SeekableByteChannel; |
| 26 | +import java.nio.channels.WritableByteChannel; |
| 27 | + |
| 28 | +class CloudStorageReadFileChannel extends FileChannel { |
| 29 | + private static final String READ_ONLY = "This FileChannel is read-only"; |
| 30 | + private final SeekableByteChannel readChannel; |
| 31 | + |
| 32 | + CloudStorageReadFileChannel(SeekableByteChannel readChannel) { |
| 33 | + Preconditions.checkNotNull(readChannel); |
| 34 | + this.readChannel = readChannel; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public int read(ByteBuffer dst) throws IOException { |
| 39 | + return readChannel.read(dst); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public synchronized long read(ByteBuffer[] dsts, int offset, int length) throws IOException { |
| 44 | + long res = 0L; |
| 45 | + for (int i = offset; i < offset + length; i++) { |
| 46 | + res += readChannel.read(dsts[i]); |
| 47 | + } |
| 48 | + return res; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public int write(ByteBuffer src) throws IOException { |
| 53 | + throw new UnsupportedOperationException(READ_ONLY); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public long write(ByteBuffer[] srcs, int offset, int length) throws IOException { |
| 58 | + throw new UnsupportedOperationException(READ_ONLY); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public long position() throws IOException { |
| 63 | + return readChannel.position(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public FileChannel position(long newPosition) throws IOException { |
| 68 | + readChannel.position(newPosition); |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public long size() throws IOException { |
| 74 | + return readChannel.size(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public FileChannel truncate(long size) throws IOException { |
| 79 | + throw new UnsupportedOperationException(READ_ONLY); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void force(boolean metaData) throws IOException { |
| 84 | + throw new UnsupportedOperationException(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public synchronized long transferTo( |
| 89 | + long transferFromPosition, long count, WritableByteChannel target) throws IOException { |
| 90 | + long res = 0L; |
| 91 | + long originalPosition = position(); |
| 92 | + try { |
| 93 | + position(transferFromPosition); |
| 94 | + int blockSize = (int) Math.min(count, 0xfffffL); |
| 95 | + int bytesRead = 0; |
| 96 | + ByteBuffer buffer = ByteBuffer.allocate(blockSize); |
| 97 | + while (res < count && bytesRead >= 0) { |
| 98 | + buffer.position(0); |
| 99 | + bytesRead = read(buffer); |
| 100 | + if (bytesRead > 0) { |
| 101 | + buffer.position(0); |
| 102 | + buffer.limit(bytesRead); |
| 103 | + target.write(buffer); |
| 104 | + res += bytesRead; |
| 105 | + } |
| 106 | + } |
| 107 | + return res; |
| 108 | + } finally { |
| 109 | + position(originalPosition); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException { |
| 115 | + throw new UnsupportedOperationException(READ_ONLY); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public synchronized int read(ByteBuffer dst, long readFromPosition) throws IOException { |
| 120 | + long originalPosition = position(); |
| 121 | + try { |
| 122 | + position(readFromPosition); |
| 123 | + int res = readChannel.read(dst); |
| 124 | + return res; |
| 125 | + } finally { |
| 126 | + position(originalPosition); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public int write(ByteBuffer src, long position) throws IOException { |
| 132 | + throw new UnsupportedOperationException(READ_ONLY); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public MappedByteBuffer map(MapMode mode, long position, long size) throws IOException { |
| 137 | + throw new UnsupportedOperationException(); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public FileLock lock(long position, long size, boolean shared) throws IOException { |
| 142 | + throw new UnsupportedOperationException(); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public FileLock tryLock(long position, long size, boolean shared) throws IOException { |
| 147 | + throw new UnsupportedOperationException(); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + protected void implCloseChannel() throws IOException { |
| 152 | + readChannel.close(); |
| 153 | + } |
| 154 | +} |
0 commit comments