-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cloud-storage-contrib-nio: Add support for newFileChannel #4718
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ec543ea
add CloudStorageFileChannel
olavloite c46e001
#4702 add support for newFileChannel(...)
olavloite 831dbf7
removed erroneously committed file
olavloite 7f38d08
fixed formatting
olavloite e87fbc9
added synchronization to file channel classes
olavloite b08147d
changed copyright years and removed unnecessary formatting changes
olavloite 693f852
removed unnecessary cast
olavloite 0561c82
make methods synchronized + clearify parameter
olavloite cf73070
make methods synchronized + remove redundant validation
olavloite 8d1a382
fixed formatting
olavloite 5c2c732
put position(originalPosition) in finally-block
olavloite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
...d-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageReadFileChannel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.cloud.storage.contrib.nio; | ||
|
||
import com.google.common.base.Preconditions; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.MappedByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.channels.FileLock; | ||
import java.nio.channels.ReadableByteChannel; | ||
import java.nio.channels.SeekableByteChannel; | ||
import java.nio.channels.WritableByteChannel; | ||
|
||
class CloudStorageReadFileChannel extends FileChannel { | ||
private static final String READ_ONLY = "This FileChannel is read-only"; | ||
private final SeekableByteChannel readChannel; | ||
|
||
CloudStorageReadFileChannel(SeekableByteChannel readChannel) { | ||
Preconditions.checkNotNull(readChannel); | ||
this.readChannel = readChannel; | ||
} | ||
|
||
@Override | ||
public int read(ByteBuffer dst) throws IOException { | ||
return readChannel.read(dst); | ||
} | ||
|
||
@Override | ||
public synchronized long read(ByteBuffer[] dsts, int offset, int length) throws IOException { | ||
long res = 0L; | ||
for (int i = offset; i < offset + length; i++) { | ||
res += readChannel.read(dsts[i]); | ||
} | ||
return res; | ||
} | ||
|
||
@Override | ||
public int write(ByteBuffer src) throws IOException { | ||
throw new UnsupportedOperationException(READ_ONLY); | ||
} | ||
|
||
@Override | ||
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException { | ||
throw new UnsupportedOperationException(READ_ONLY); | ||
} | ||
|
||
@Override | ||
public long position() throws IOException { | ||
return readChannel.position(); | ||
} | ||
|
||
@Override | ||
public FileChannel position(long newPosition) throws IOException { | ||
readChannel.position(newPosition); | ||
return this; | ||
} | ||
|
||
@Override | ||
public long size() throws IOException { | ||
return readChannel.size(); | ||
} | ||
|
||
@Override | ||
public FileChannel truncate(long size) throws IOException { | ||
throw new UnsupportedOperationException(READ_ONLY); | ||
} | ||
|
||
@Override | ||
public void force(boolean metaData) throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public synchronized long transferTo( | ||
long transferFromPosition, long count, WritableByteChannel target) throws IOException { | ||
long res = 0L; | ||
long originalPosition = position(); | ||
position(transferFromPosition); | ||
int blockSize = (int) Math.min(count, 0xfffffL); | ||
int bytesRead = 0; | ||
ByteBuffer buffer = ByteBuffer.allocate(blockSize); | ||
while (res < count && bytesRead >= 0) { | ||
buffer.position(0); | ||
bytesRead = read(buffer); | ||
if (bytesRead > 0) { | ||
buffer.position(0); | ||
buffer.limit(bytesRead); | ||
target.write(buffer); | ||
res += bytesRead; | ||
} | ||
} | ||
position(originalPosition); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please put |
||
return res; | ||
} | ||
|
||
@Override | ||
public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException { | ||
throw new UnsupportedOperationException(READ_ONLY); | ||
} | ||
|
||
@Override | ||
public synchronized int read(ByteBuffer dst, long readFromPosition) throws IOException { | ||
long originalPosition = position(); | ||
position(readFromPosition); | ||
int res = readChannel.read(dst); | ||
position(originalPosition); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please put |
||
return res; | ||
} | ||
|
||
@Override | ||
public int write(ByteBuffer src, long position) throws IOException { | ||
throw new UnsupportedOperationException(READ_ONLY); | ||
} | ||
|
||
@Override | ||
public MappedByteBuffer map(MapMode mode, long position, long size) throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public FileLock lock(long position, long size, boolean shared) throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public FileLock tryLock(long position, long size, boolean shared) throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
protected void implCloseChannel() throws IOException { | ||
readChannel.close(); | ||
} | ||
} |
180 changes: 180 additions & 0 deletions
180
...-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageWriteFileChannel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.cloud.storage.contrib.nio; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.MappedByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.channels.FileLock; | ||
import java.nio.channels.ReadableByteChannel; | ||
import java.nio.channels.SeekableByteChannel; | ||
import java.nio.channels.WritableByteChannel; | ||
|
||
class CloudStorageWriteFileChannel extends FileChannel { | ||
private static final String WRITE_ONLY = "This FileChannel is write-only"; | ||
private SeekableByteChannel writeChannel; | ||
private boolean valid = true; | ||
|
||
CloudStorageWriteFileChannel(SeekableByteChannel writeChannel) { | ||
this.writeChannel = writeChannel; | ||
} | ||
|
||
private void checkValid() throws IOException { | ||
if (!valid) { | ||
// These methods are only supported to be called once, because the underlying channel does not | ||
// support changing the position. | ||
throw new IOException( | ||
"This FileChannel is no longer valid. " | ||
+ "A Cloud Storage FileChannel is invalidated after calling one of " | ||
+ "the methods FileChannel#write(ByteBuffer, long) or " | ||
+ "FileChannel#transferFrom(ReadableByteChannel, long, long)"); | ||
} | ||
if (!writeChannel.isOpen()) { | ||
throw new IOException("This FileChannel is closed"); | ||
} | ||
} | ||
|
||
@Override | ||
public int read(ByteBuffer dst) throws IOException { | ||
throw new UnsupportedOperationException(WRITE_ONLY); | ||
} | ||
|
||
@Override | ||
public long read(ByteBuffer[] dsts, int offset, int length) throws IOException { | ||
throw new UnsupportedOperationException(WRITE_ONLY); | ||
} | ||
|
||
@Override | ||
public synchronized int write(ByteBuffer src) throws IOException { | ||
checkValid(); | ||
return writeChannel.write(src); | ||
} | ||
|
||
@Override | ||
public synchronized long write(ByteBuffer[] srcs, int offset, int length) throws IOException { | ||
checkValid(); | ||
long res = 0L; | ||
for (int i = offset; i < offset + length; i++) { | ||
res += writeChannel.write(srcs[i]); | ||
} | ||
return res; | ||
} | ||
|
||
@Override | ||
public synchronized long position() throws IOException { | ||
checkValid(); | ||
return writeChannel.position(); | ||
} | ||
|
||
@Override | ||
public synchronized FileChannel position(long newPosition) throws IOException { | ||
if (newPosition != position()) { | ||
writeChannel.position(newPosition); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public synchronized long size() throws IOException { | ||
checkValid(); | ||
return writeChannel.size(); | ||
} | ||
|
||
@Override | ||
public synchronized FileChannel truncate(long size) throws IOException { | ||
checkValid(); | ||
writeChannel.truncate(size); | ||
return this; | ||
} | ||
|
||
@Override | ||
public void force(boolean metaData) throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public long transferTo(long position, long count, WritableByteChannel target) throws IOException { | ||
throw new UnsupportedOperationException(WRITE_ONLY); | ||
} | ||
|
||
@Override | ||
public synchronized long transferFrom(ReadableByteChannel src, long position, long count) | ||
throws IOException { | ||
if (position != position()) { | ||
throw new UnsupportedOperationException( | ||
"This FileChannel only supports transferFrom at the current position"); | ||
} | ||
int blockSize = (int) Math.min(count, 0xfffffL); | ||
long res = 0L; | ||
int bytesRead = 0; | ||
ByteBuffer buffer = ByteBuffer.allocate(blockSize); | ||
while (res < count && bytesRead >= 0) { | ||
buffer.position(0); | ||
bytesRead = src.read(buffer); | ||
if (bytesRead > 0) { | ||
buffer.position(0); | ||
buffer.limit(bytesRead); | ||
write(buffer); | ||
res += bytesRead; | ||
} | ||
} | ||
// The channel is no longer valid as the position has been updated, and there is no way of | ||
// resetting it, but this way we at least support the write-at-position and transferFrom | ||
// methods being called once. | ||
this.valid = false; | ||
return res; | ||
} | ||
|
||
@Override | ||
public int read(ByteBuffer dst, long position) throws IOException { | ||
throw new UnsupportedOperationException(WRITE_ONLY); | ||
} | ||
|
||
@Override | ||
public synchronized int write(ByteBuffer src, long position) throws IOException { | ||
if (position != position()) { | ||
throw new UnsupportedOperationException( | ||
"This FileChannel only supports write at the current position"); | ||
} | ||
int res = writeChannel.write(src); | ||
// The channel is no longer valid as the position has been updated, and there is no way of | ||
// resetting it, but this way we at least support the write-at-position and transferFrom | ||
// methods being called once. | ||
this.valid = false; | ||
return res; | ||
} | ||
|
||
@Override | ||
public MappedByteBuffer map(MapMode mode, long position, long size) throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public FileLock lock(long position, long size, boolean shared) throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public FileLock tryLock(long position, long size, boolean shared) throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
protected void implCloseChannel() throws IOException { | ||
writeChannel.close(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the null check for attrs below if this is the case? attrs is optional in File.createFile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I chose to do so to keep it consistent with the other methods in
CloudStorageFileSystemProvider
that accept an array ofFileAtribute
s as an input parameter. These do the exact same check, even though the input value is ignored.(See
newByteChannel(...)``createDirectory(...)
)Furthermore,
newFileChannel
method can also be used to read an existing file, so it does not always call File.createFile.