Java Code Examples for java.nio.channels.SeekableByteChannel#close()

The following examples show how to use java.nio.channels.SeekableByteChannel#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Rusila.java    From neembuu-uploader with GNU General Public License v3.0 6 votes vote down vote up
private static String getVStr(Rus r,String n, int maxDataSize)throws IOException{
    String v;
    if(r instanceof ClassRus){
        return ((ClassRus)r).v(n);
    }else {
        SeekableByteChannel dp = r.p(n, StandardOpenOption.READ,StandardOpenOption.CREATE);
        if(maxDataSize < 0) {
            v = getVStrF(dp);
        }else{
            ByteBuffer bb=ByteBuffer.allocate(Math.min((int)dp.size(),maxDataSize));
            dp.read(bb);
            v = new String(bb.array(),Charset.forName("UTF-8"));
        }
        try{dp.close();}catch(Exception a){a.printStackTrace();}
    }
    return v;
}
 
Example 2
Source File: Rusila.java    From neembuu-uploader with GNU General Public License v3.0 6 votes vote down vote up
public static void set(Rus r,String n,Object v)throws Exception{
    SeekableByteChannel dp = r.p(
                    n, 
                    StandardOpenOption.WRITE,StandardOpenOption.CREATE,
                    StandardOpenOption.TRUNCATE_EXISTING);
    ByteBuffer bb;
    if(v instanceof Integer  || v instanceof Long || v instanceof Double || v instanceof Float 
            || v instanceof Boolean || v instanceof Character){
        // explicitly specifying charset to avoid localization 
        bb=ByteBuffer.wrap(v.toString().getBytes(Charset.forName("UTF-8")));
    }else if(v instanceof String){
        bb=ByteBuffer.wrap(((String)v).getBytes(Charset.forName("UTF-8")));
    }else {
        bb=SeekableByteChannel_wrap.toByteBuffer(v);
    }
    dp.write(bb);
    try{dp.close();}catch(Exception a){a.printStackTrace();}
}
 
Example 3
Source File: Rusila.java    From neembuu-uploader with GNU General Public License v3.0 5 votes vote down vote up
public static void set(Rus r,String n,V v)throws Exception{
    SeekableByteChannel dp = r.p(
                    n, 
                    StandardOpenOption.WRITE,StandardOpenOption.CREATE,
                    StandardOpenOption.TRUNCATE_EXISTING);
    dp.write(ByteBuffer.wrap(v.raw()));
    try{dp.close();}catch(Exception a){a.printStackTrace();}
}
 
Example 4
Source File: ReplayCachePrecise.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
        }
    }
 
Example 5
Source File: ReplayCachePrecise.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
        }
    }
 
Example 6
Source File: Dat.java    From THULAC-Java with MIT License 5 votes vote down vote up
/**
 * Read a {@link Dat} from a given file.
 *
 * @param filename
 * 		The name of the {@link Dat} file.
 *
 * @throws IOException
 * 		If an I/O error occurred while reading the file.
 */
public Dat(String filename) throws IOException {
	SeekableByteChannel channel = Files.newByteChannel(Paths.get(filename));
	// DWORD base + DWORD check -> 8 bytes per record
	this.datSize = (int) (channel.size() >> 3);
	this.dat = new int[this.datSize << 1];
	// strange though, dat files are stored little endian
	ByteBuffer bb = ByteBuffer.allocateDirect(64 * 1024)
			.order(ByteOrder.LITTLE_ENDIAN);
	bb.clear();
	if (!BufferUtils.readInts(channel, bb, this.dat))
		throw new IOException("File does not contain enough data!");
	channel.close();
}
 
Example 7
Source File: GcsUtilTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testGCSChannelCloseIdempotent() throws IOException {
  GoogleCloudStorageReadOptions readOptions =
      GoogleCloudStorageReadOptions.builder().setFastFailOnNotFound(false).build();
  SeekableByteChannel channel =
      new GoogleCloudStorageReadChannel(
          null, "dummybucket", "dummyobject", null, new ClientRequestHelper<>(), readOptions);
  channel.close();
  channel.close();
}
 
Example 8
Source File: ReplayCachePrecise.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
        }
    }
 
Example 9
Source File: ReplayCachePrecise.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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);
        }
    }
 
Example 10
Source File: TestLeakFS.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Test leaks via Files.newByteChannel */
public void testLeakByteChannel() throws IOException {
  Path dir = wrap(createTempDir());
  
  OutputStream file = Files.newOutputStream(dir.resolve("stillopen"));
  file.write(5);
  file.close();
  SeekableByteChannel leak = Files.newByteChannel(dir.resolve("stillopen"));

  Exception e = expectThrows(Exception.class, () -> dir.getFileSystem().close());
  assertTrue(e.getMessage().contains("file handle leaks"));
  leak.close();
}
 
Example 11
Source File: ReplayCachePrecise.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
        }
    }
 
Example 12
Source File: ReplayCachePrecise.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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);
        }
    }
 
Example 13
Source File: ReplayCachePrecise.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
        }
    }
 
Example 14
Source File: ReplayCachePrecise.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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);
        }
    }
 
Example 15
Source File: GcsNioIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(groups = {"bucket"})
public void testCloseWhilePrefetching() throws Exception {
    final String large = getGCPTestInputPath() + largeFilePath;
    SeekableByteChannel chan = BucketUtils.addPrefetcher(10, Files.newByteChannel(Paths.get(URI.create(large))));
    // read just 1 byte, get the prefetching going
    ByteBuffer one = ByteBuffer.allocate(1);
    chan.read(one);
    // closing must not throw an exception, even if the prefetching
    // thread is active.
    chan.close();
}
 
Example 16
Source File: ReplayCachePrecise.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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);
        }
    }
 
Example 17
Source File: JournaledChannelPoolTest.java    From emissary with Apache License 2.0 5 votes vote down vote up
/**
 * Test of freeChannel method, of class JournaledChannelPool.
 */
@Test
public void testGetAndFree() throws Exception {
    final SeekableByteChannel out = this.instance.getFree();
    final int created = this.instance.getCreatedCount();
    assertTrue("Should have recorded creation of at least 1 object", created > 0);
    final int free = this.instance.getFreeSize();
    // out is a wrapper, closing it will delegate to freeChannel
    out.close();
    assertTrue("Free should be one more than " + free, (free + 1) == this.instance.getFreeSize());
}
 
Example 18
Source File: ReplayCachePrecise.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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);
        }
    }
 
Example 19
Source File: ReplayCachePrecise.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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);
        }
    }
 
Example 20
Source File: FLVWriter.java    From red5-io with Apache License 2.0 4 votes vote down vote up
private Map<String, ?> getMetaData(Path path, int maxTags) throws IOException {
    Map<String, ?> meta = null;
    // attempt to read the metadata
    SeekableByteChannel channel = Files.newByteChannel(path, StandardOpenOption.READ);
    long size = channel.size();
    log.debug("Channel open: {} size: {} position: {}", channel.isOpen(), size, channel.position());
    if (size > 0L) {
        // skip flv signature 4b, flags 1b, data offset 4b (9b), prev tag size (4b)
        channel.position(appendOffset);
        // flv tag header size 11b
        ByteBuffer dst = ByteBuffer.allocate(11);
        do {
            int read = channel.read(dst);
            if (read > 0) {
                dst.flip();
                byte tagType = (byte) (dst.get() & 31); // 1
                int bodySize = IOUtils.readUnsignedMediumInt(dst); // 3
                int timestamp = IOUtils.readExtendedMediumInt(dst); // 4
                int streamId = IOUtils.readUnsignedMediumInt(dst); // 3
                log.debug("Data type: {} timestamp: {} stream id: {} body size: {}", new Object[] { tagType, timestamp, streamId, bodySize });
                if (tagType == ITag.TYPE_METADATA) {
                    ByteBuffer buf = ByteBuffer.allocate(bodySize);
                    read = channel.read(buf);
                    if (read > 0) {
                        buf.flip();
                        // construct the meta
                        IoBuffer ioBuf = IoBuffer.wrap(buf);
                        Input input = new Input(ioBuf);
                        String metaType = Deserializer.deserialize(input, String.class);
                        log.debug("Metadata type: {}", metaType);
                        meta = Deserializer.deserialize(input, Map.class);
                        input = null;
                        ioBuf.clear();
                        ioBuf.free();
                        if (meta.containsKey("duration")) {
                            appendOffset = channel.position() + 4L;
                            break;
                        }
                    }
                    buf.compact();
                }
                // advance beyond prev tag size
                channel.position(channel.position() + 4L);
                //int prevTagSize = dst.getInt(); // 4
                //log.debug("Previous tag size: {} {}", prevTagSize, (bodySize - 11));
                dst.compact();
            }
        } while (--maxTags > 0); // read up-to "max" tags looking for duration
        channel.close();
    }
    return meta;
}