Skip to content

Commit 7229565

Browse files
authored
---
yaml --- r: 28181 b: refs/heads/autosynth-spanner c: b697a8b h: refs/heads/master i: 28179: f72da44
1 parent d97dd0d commit 7229565

7 files changed

Lines changed: 1006 additions & 1 deletion

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ refs/heads/autosynth-language: e73905aa7672afa47240e65b25c087207f4594f9
137137
refs/heads/autosynth-os-login: 123ba209c5769d0ee067e0ce5848bec13b42a4f4
138138
refs/heads/autosynth-redis: 6bedce4d7c7c6ca6a22e83ad1780e08fdc565a9e
139139
refs/heads/autosynth-scheduler: 57f9fdb1e7de30c85f4ec7198931a07f50603e55
140-
refs/heads/autosynth-spanner: 03c1786b55cd79171e9ac73909d81ee49b7d8452
140+
refs/heads/autosynth-spanner: b697a8b47a7eb0eb08d958221445fadc07a0aa14
141141
refs/heads/autosynth-speech: 64692f6db11364f663921be02c08072b966b6e7b
142142
refs/heads/autosynth-tasks: eb03eeab747e925175890db923945384d89b273a
143143
refs/heads/autosynth-texttospeech: 2c442fe0b7f089fbab266edfe4dd83c532e82dd0

branches/autosynth-spanner/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.io.IOException;
4343
import java.io.InputStream;
4444
import java.net.URI;
45+
import java.nio.channels.FileChannel;
4546
import java.nio.channels.SeekableByteChannel;
4647
import java.nio.file.AccessMode;
4748
import java.nio.file.AtomicMoveNotSupportedException;
@@ -305,6 +306,48 @@ public SeekableByteChannel newByteChannel(
305306
}
306307
}
307308

309+
/**
310+
* Open a file for reading OR writing. The {@link FileChannel} that is returned will only allow
311+
* reads or writes depending on the {@link OpenOption}s that are specified. If any of the
312+
* following have been specified, the {@link FileChannel} will be write-only: {@link
313+
* StandardOpenOption#CREATE}
314+
*
315+
* <ul>
316+
* <li>{@link StandardOpenOption#CREATE}
317+
* <li>{@link StandardOpenOption#CREATE_NEW}
318+
* <li>{@link StandardOpenOption#WRITE}
319+
* <li>{@link StandardOpenOption#TRUNCATE_EXISTING}
320+
* </ul>
321+
*
322+
* In all other cases the {@link FileChannel} will be read-only.
323+
*
324+
* @param path The path to the file to open or create
325+
* @param options The options specifying how the file should be opened, and whether the {@link
326+
* FileChannel} should be read-only or write-only.
327+
* @param attrs (not supported, the values will be ignored)
328+
* @throws IOException
329+
*/
330+
@Override
331+
public FileChannel newFileChannel(
332+
Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
333+
checkNotNull(path);
334+
initStorage();
335+
CloudStorageUtil.checkNotNullArray(attrs);
336+
if (options.contains(StandardOpenOption.CREATE_NEW)) {
337+
Files.createFile(path, attrs);
338+
} else if (options.contains(StandardOpenOption.CREATE) && !Files.exists(path)) {
339+
Files.createFile(path, attrs);
340+
}
341+
if (options.contains(StandardOpenOption.WRITE)
342+
|| options.contains(StandardOpenOption.CREATE)
343+
|| options.contains(StandardOpenOption.CREATE_NEW)
344+
|| options.contains(StandardOpenOption.TRUNCATE_EXISTING)) {
345+
return new CloudStorageWriteFileChannel(newWriteChannel(path, options));
346+
} else {
347+
return new CloudStorageReadFileChannel(newReadChannel(path, options));
348+
}
349+
}
350+
308351
private SeekableByteChannel newReadChannel(Path path, Set<? extends OpenOption> options)
309352
throws IOException {
310353
initStorage();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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

Comments
 (0)