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

The following examples show how to use java.nio.channels.SeekableByteChannel#write() . 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: TestUtilities.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Copies the full content of the given test resource in a temporary file and returns the channel for that file.
 * The file is opened with {@link StandardOpenOption#DELETE_ON_CLOSE}, together with read and write options.
 *
 * @param  caller    defines the root from which to search for the {@code resource}.
 * @param  resource  path (relative to the {@code caller}) of the test file to copy.
 * @return a channel opened on a copy of the content of the given test resource.
 * @throws IOException if an error occurred while copying the data.
 *
 * @since 0.8
 */
public static SeekableByteChannel createTemporaryFile(final Class<?> caller, final String resource) throws IOException {
    final SeekableByteChannel channel;
    try (ReadableByteChannel in = Channels.newChannel(caller.getResourceAsStream(resource))) {
        final int s = resource.lastIndexOf('.');
        final Path file = Files.createTempFile("SIS", (s >= 0) ? resource.substring(s) : null);
        channel = Files.newByteChannel(file, StandardOpenOption.DELETE_ON_CLOSE,
                            StandardOpenOption.READ, StandardOpenOption.WRITE);
        final ByteBuffer buffer = ByteBuffer.allocate(4000);
        while (in.read(buffer) >= 0) {
            buffer.flip();
            channel.write(buffer);
            buffer.clear();
        }
    }
    return channel.position(0);
}
 
Example 2
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 3
Source File: DflCache.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static SeekableByteChannel createNoClose(Path p)
        throws IOException {
    SeekableByteChannel newChan = Files.newByteChannel(
            p, StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING,
                StandardOpenOption.WRITE);
    ByteBuffer buffer = ByteBuffer.allocate(6);
    buffer.putShort((short)KRB5_RV_VNO);
    buffer.order(ByteOrder.nativeOrder());
    buffer.putInt(KerberosTime.getDefaultSkew());
    buffer.flip();
    newChan.write(buffer);
    return newChan;
}
 
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: DflCache.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static SeekableByteChannel createNoClose(Path p)
        throws IOException {
    SeekableByteChannel newChan = Files.newByteChannel(
            p, StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING,
                StandardOpenOption.WRITE);
    ByteBuffer buffer = ByteBuffer.allocate(6);
    buffer.putShort((short)KRB5_RV_VNO);
    buffer.order(ByteOrder.nativeOrder());
    buffer.putInt(KerberosTime.getDefaultSkew());
    buffer.flip();
    newChan.write(buffer);
    return newChan;
}
 
Example 6
Source File: DflCache.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static SeekableByteChannel createNoClose(Path p)
        throws IOException {
    SeekableByteChannel newChan = Files.newByteChannel(
            p, StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING,
                StandardOpenOption.WRITE);
    ByteBuffer buffer = ByteBuffer.allocate(6);
    buffer.putShort((short)KRB5_RV_VNO);
    buffer.order(ByteOrder.nativeOrder());
    buffer.putInt(KerberosTime.getDefaultSkew());
    buffer.flip();
    newChan.write(buffer);
    return newChan;
}
 
Example 7
Source File: DflCache.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static SeekableByteChannel createNoClose(Path p)
        throws IOException {
    SeekableByteChannel newChan = Files.newByteChannel(
            p, StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING,
                StandardOpenOption.WRITE);
    ByteBuffer buffer = ByteBuffer.allocate(6);
    buffer.putShort((short)KRB5_RV_VNO);
    buffer.order(ByteOrder.nativeOrder());
    buffer.putInt(KerberosTime.getDefaultSkew());
    buffer.flip();
    newChan.write(buffer);
    return newChan;
}
 
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: DflCache.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static SeekableByteChannel createNoClose(Path p)
        throws IOException {
    SeekableByteChannel newChan = Files.newByteChannel(
            p, StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING,
                StandardOpenOption.WRITE);
    ByteBuffer buffer = ByteBuffer.allocate(6);
    buffer.putShort((short)KRB5_RV_VNO);
    buffer.order(ByteOrder.nativeOrder());
    buffer.putInt(KerberosTime.getDefaultSkew());
    buffer.flip();
    newChan.write(buffer);
    return newChan;
}
 
Example 10
Source File: ReplayCachePrecise.java    From openjdk-jdk9 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,
                "HASH", "1111111111111111");
        AuthTimeWithHash a2 = new AuthTimeWithHash(client, server, time(0), 0,
                "HASH", "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 11
Source File: DflCache.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static SeekableByteChannel createNoClose(Path p)
        throws IOException {
    SeekableByteChannel newChan = Files.newByteChannel(
            p, StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING,
                StandardOpenOption.WRITE);
    ByteBuffer buffer = ByteBuffer.allocate(6);
    buffer.putShort((short)KRB5_RV_VNO);
    buffer.order(ByteOrder.nativeOrder());
    buffer.putInt(KerberosTime.getDefaultSkew());
    buffer.flip();
    newChan.write(buffer);
    return newChan;
}
 
Example 12
Source File: DflCache.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static SeekableByteChannel createNoClose(Path p)
        throws IOException {
    SeekableByteChannel newChan = Files.newByteChannel(
            p, StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING,
                StandardOpenOption.WRITE);
    ByteBuffer buffer = ByteBuffer.allocate(6);
    buffer.putShort((short)KRB5_RV_VNO);
    buffer.order(ByteOrder.nativeOrder());
    buffer.putInt(KerberosTime.getDefaultSkew());
    buffer.flip();
    newChan.write(buffer);
    return newChan;
}
 
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 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: 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 16
Source File: DflCache.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static SeekableByteChannel createNoClose(Path p)
        throws IOException {
    SeekableByteChannel newChan = Files.newByteChannel(
            p, StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING,
                StandardOpenOption.WRITE);
    ByteBuffer buffer = ByteBuffer.allocate(6);
    buffer.putShort((short)KRB5_RV_VNO);
    buffer.order(ByteOrder.nativeOrder());
    buffer.putInt(KerberosTime.getDefaultSkew());
    buffer.flip();
    newChan.write(buffer);
    return newChan;
}
 
Example 17
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 18
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 19
Source File: FilesNewByteChannelTest.java    From ParallelGit with Apache License 2.0 4 votes vote down vote up
private static void writeChannel(SeekableByteChannel channel, byte[] bytes) throws IOException {
  ByteBuffer buffer = ByteBuffer.wrap(bytes);
  channel.write(buffer);
}
 
Example 20
Source File: FilesNewByteChannelTest.java    From ParallelGit with Apache License 2.0 4 votes vote down vote up
private static void writeChannel(SeekableByteChannel channel, byte[] bytes) throws IOException {
  ByteBuffer buffer = ByteBuffer.wrap(bytes);
  channel.write(buffer);
}