use of java.nio.channels.SeekableByteChannel in project lucene-solr by apache.
the class HandleTrackingFS method newByteChannel.
@Override
public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
SeekableByteChannel channel = new FilterSeekableByteChannel(super.newByteChannel(path, options, attrs)) {
boolean closed;
@Override
public void close() throws IOException {
try {
if (!closed) {
closed = true;
onClose(path, this);
}
} finally {
super.close();
}
}
@Override
public String toString() {
return "SeekableByteChannel(" + path.toString() + ")";
}
@Override
public int hashCode() {
return System.identityHashCode(this);
}
@Override
public boolean equals(Object obj) {
return this == obj;
}
};
callOpenHook(path, channel);
return channel;
}
use of java.nio.channels.SeekableByteChannel in project lucene-solr by apache.
the class TestHandleTrackingFS method testOnOpenThrowsException.
/** Test that the delegate gets closed on exception in HandleTrackingFS#onOpen */
public void testOnOpenThrowsException() throws IOException {
// we are using LeakFS under the hood if we don't get closed the test fails
Path path = wrap(createTempDir());
FileSystem fs = new HandleTrackingFS("test://", path.getFileSystem()) {
@Override
protected void onClose(Path path, Object stream) throws IOException {
}
@Override
protected void onOpen(Path path, Object stream) throws IOException {
throw new IOException("boom");
}
}.getFileSystem(URI.create("file:///"));
Path dir = new FilterPath(path, fs);
try {
OutputStream file = Files.newOutputStream(dir.resolve("somefile"));
fail("expected IOException");
} catch (IOException ex) {
// expected
}
try {
SeekableByteChannel channel = Files.newByteChannel(dir.resolve("somefile"));
fail("expected IOException");
} catch (IOException ex) {
// expected
}
try {
InputStream stream = Files.newInputStream(dir.resolve("somefile"));
fail("expected IOException");
} catch (IOException ex) {
// expected
}
fs.close();
try {
DirectoryStream<Path> dirStream = Files.newDirectoryStream(dir);
fail("expected IOException");
} catch (IOException ex) {
// expected
}
fs.close();
}
use of java.nio.channels.SeekableByteChannel in project lucene-solr by apache.
the class TestHandleTrackingFS method testOnCloseThrowsException.
/** Test that the delegate gets closed on exception in HandleTrackingFS#onClose */
public void testOnCloseThrowsException() throws IOException {
// we are using LeakFS under the hood if we don't get closed the test fails
Path path = wrap(createTempDir());
FileSystem fs = new HandleTrackingFS("test://", path.getFileSystem()) {
@Override
protected void onClose(Path path, Object stream) throws IOException {
throw new IOException("boom");
}
@Override
protected void onOpen(Path path, Object stream) throws IOException {
//
}
}.getFileSystem(URI.create("file:///"));
Path dir = new FilterPath(path, fs);
OutputStream file = Files.newOutputStream(dir.resolve("somefile"));
file.write(5);
try {
file.close();
fail("expected IOException");
} catch (IOException ex) {
// expected
}
SeekableByteChannel channel = Files.newByteChannel(dir.resolve("somefile"));
try {
channel.close();
fail("expected IOException");
} catch (IOException ex) {
// expected
}
InputStream stream = Files.newInputStream(dir.resolve("somefile"));
try {
stream.close();
fail("expected IOException");
} catch (IOException ex) {
// expected
}
fs.close();
DirectoryStream<Path> dirStream = Files.newDirectoryStream(dir);
try {
dirStream.close();
fail("expected IOException");
} catch (IOException ex) {
// expected
}
}
use of java.nio.channels.SeekableByteChannel in project jdk8u_jdk by JetBrains.
the class ReplayCachePrecise method main.
public static void main(String[] args) throws Exception {
AuthTimeWithHash a1 = new AuthTimeWithHash(client, server, time(0), 0, "1111111111111111");
AuthTimeWithHash a2 = new AuthTimeWithHash(client, server, time(0), 0, "2222222222222222");
KerberosTime now = new KerberosTime(time(0) * 1000L);
// When all new styles, must exact match
ReplayCache cache = ReplayCache.getInstance("dfl:./c1");
cache.checkAndStore(now, a1);
cache.checkAndStore(now, a2);
// When only old style in cache, partial match
cache = ReplayCache.getInstance("dfl:./c2");
cache.checkAndStore(now, a1);
// A small surgery to remove the new style from the cache file
SeekableByteChannel ch = Files.newByteChannel(Paths.get("c2"), StandardOpenOption.WRITE, StandardOpenOption.READ);
ch.position(6);
ch.write(ByteBuffer.wrap(a1.encode(false)));
ch.truncate(ch.position());
ch.close();
try {
cache.checkAndStore(now, a2);
throw new Exception();
} catch (KrbException ke) {
// Correct
System.out.println(ke);
}
}
use of java.nio.channels.SeekableByteChannel in project jdk8u_jdk by JetBrains.
the class ReplayCacheTestProc method csize.
// return size of dfl file, excluding the null hash ones
private static int csize(int p) throws Exception {
try (SeekableByteChannel chan = Files.newByteChannel(Paths.get(dfl(p)), StandardOpenOption.READ)) {
chan.position(6);
int cc = 0;
while (true) {
try {
if (AuthTime.readFrom(chan) != null)
cc++;
} catch (BufferUnderflowException e) {
break;
}
}
return cc;
} catch (IOException ioe) {
return 0;
}
}
Aggregations