Java Code Examples for java.nio.file.Files.newByteChannel()

The following are Jave code examples for showing how to use newByteChannel() of the java.nio.file.Files class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Matcher   File: InputFile.java   View Source Code Vote up 6 votes
private static byte[] hash(Path path) throws IOException {
	TlData tlData = InputFile.tlData.get();

	MessageDigest digest = tlData.digest;
	ByteBuffer buffer = tlData.buffer;
	buffer.clear();

	try (SeekableByteChannel channel = Files.newByteChannel(path)) {
		while (channel.read(buffer) != -1) {
			buffer.flip();
			digest.update(buffer);
			buffer.clear();
		}
	}

	return digest.digest();
}
 
Example 2
Project: jdk8u-jdk   File: ReplayCacheTestProc.java   View Source Code Vote up 6 votes
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;
    }
}
 
Example 3
Project: elasticsearch_my   File: FileSystemUtilsTests.java   View Source Code Vote up 6 votes
@Before
public void copySourceFilesToTarget() throws IOException, URISyntaxException {
    src = createTempDir();
    dst = createTempDir();
    Files.createDirectories(src);
    Files.createDirectories(dst);
    txtFile = src.resolve("text-file.txt");

    try (ByteChannel byteChannel = Files.newByteChannel(txtFile, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
        expectedBytes = new byte[3];
        expectedBytes[0] = randomByte();
        expectedBytes[1] = randomByte();
        expectedBytes[2] = randomByte();
        byteChannel.write(ByteBuffer.wrap(expectedBytes));
    }
}
 
Example 4
Project: cornerstone   File: IOUtils.java   View Source Code Vote up 6 votes
public static byte[] partitionRead(final Path path,int partionSize,int partitionIndex) throws IOException {

        try (SeekableByteChannel sbc = Files.newByteChannel(path);
             InputStream in = Channels.newInputStream(sbc)) {
            long startIndex = (partitionIndex-1)*partionSize;
            if (startIndex < 0) {
                startIndex = 0;
            }
            in.skip(startIndex);
            if (partionSize >  MAX_BUFFER_SIZE)
                throw new OutOfMemoryError("Required array size too large");

            return read(in,  partionSize);

        }
    }
 
Example 5
Project: openjdk-jdk10   File: ReplayCacheTestProc.java   View Source Code Vote up 6 votes
private static int csize(int p) throws Exception {
    try (SeekableByteChannel chan = Files.newByteChannel(
            Paths.get(cwd, 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;
    }
}
 
Example 6
Project: scala-playground   File: FilePersister.java   View Source Code Vote up 6 votes
public static List<BinaryTrade> readFromFile(Path p) throws IOException {
  System.out.println("Reading binary data to file ... ");
  List<BinaryTrade> trades = new ArrayList<>();
  long start = System.nanoTime();

  try (SeekableByteChannel channel = Files.newByteChannel(p, READ)) {
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(batchSize);
    int nRead;
    while ((nRead = channel.read(byteBuffer)) != -1) {
      if (nRead == 0) continue;
      byteBuffer.position(0);
      byteBuffer.limit(nRead);
      while (byteBuffer.hasRemaining()) {
        byte[] bytes = new byte[40];
        byteBuffer.get(bytes, 0, 40);
        trades.add(new BinaryTrade(bytes));
      }
      byteBuffer.clear();
    }
  }

  System.out.printf("\tTime to read %,.1f million elements: %,.2f seconds%n%n", trades.size() / 1e6, (System.nanoTime() - start) / 1e9);
  return trades;
}
 
Example 7
Project: jdk8u-jdk   File: ReplayCachePrecise.java   View Source Code Vote up 5 votes
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 8
Project: cornerstone   File: IOUtils.java   View Source Code Vote up 5 votes
public static byte[] reverseRead(final Path path,Charset charset,long size) throws IOException {
    try (SeekableByteChannel sbc = Files.newByteChannel(path);
         InputStream in = Channels.newInputStream(sbc)) {
        long startIndex = sbc.size() - size;
        if (startIndex < 0) {
            startIndex = 0;
        }
        in.skip(startIndex);
        if (size > (long) MAX_BUFFER_SIZE)
            throw new OutOfMemoryError("Required array size too large");

        return read(in, (int) size);

    }
}
 
Example 9
Project: Mass   File: IOUtil.java   View Source Code Vote up 5 votes
/**
 * 
 * @param resource
 * @param bufferSize
 * @return
 * @throws IOException
 */
public static ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException {
    ByteBuffer buffer;

    Path path = Paths.get(resource);
    if (Files.isReadable(path)) {
        try (SeekableByteChannel fc = Files.newByteChannel(path)) {
            buffer = createByteBuffer((int)fc.size() + 1);
            while (fc.read(buffer) != -1) {
                ;
            }
        }
    } else {
        try (
                InputStream source = IOUtil.class.getClassLoader().getResourceAsStream(resource);
                ReadableByteChannel rbc = Channels.newChannel(source)
        ) {
            buffer = createByteBuffer(bufferSize);

            while (true) {
                int bytes = rbc.read(buffer);
                if (bytes == -1) {
                    break;
                }
                if (buffer.remaining() == 0) {
                    buffer = resizeBuffer(buffer, buffer.capacity() * 2);
                }
            }
        }
    }

    buffer.flip();
    return buffer;
}
 
Example 10
Project: openjdk-jdk10   File: DeleteOnClose.java   View Source Code Vote up 5 votes
public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        Path file = Files.createTempFile("blah", "tmp");
        ProcessTools.executeTestJava(DeleteOnClose.class.getName(),
                                     file.toAbsolutePath().toString())
                    .shouldHaveExitValue(0);
        runTest(file);
    } else {
        // open file but do not close it. Its existance will be checked by
        // the caller.
        Files.newByteChannel(Paths.get(args[0]), READ, WRITE, DELETE_ON_CLOSE);
    }
}
 
Example 11
Project: httrack2warc   File: NdxCache.java   View Source Code Vote up 5 votes
@Override
public InputStream openStream() throws IOException {
    parseDatHeader();
    SeekableByteChannel channel = Files.newByteChannel(datFile);
    channel.position(Math.abs(position) + headerLen);
    return new CountingStream(Channels.newInputStream(channel), dataLen);
}
 
Example 12
Project: guava-mock   File: MoreFiles.java   View Source Code Vote up 5 votes
@Override
public byte[] read() throws IOException {
  try (SeekableByteChannel channel = Files.newByteChannel(path, options)) {
    return com.google.common.io.Files.readFile(
        Channels.newInputStream(channel), channel.size());
  }
}
 
Example 13
Project: Lwjgl3-Game-Engine-Programming-Series   File: ImageLoader.java   View Source Code Vote up 5 votes
public static ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException {
    ByteBuffer buffer;

    Path path = Paths.get(resource);
    if (Files.isReadable(path)) {
        try (SeekableByteChannel fc = Files.newByteChannel(path)) {
            buffer = BufferUtils.createByteBuffer((int)fc.size() + 1);
            while (fc.read(buffer) != -1) {
                ;
            }
        }
    } else {
        try (
                InputStream source = Thread.class.getClassLoader().getResourceAsStream(resource);
                ReadableByteChannel rbc = Channels.newChannel(source)
            ) {
                buffer = BufferUtils.createByteBuffer(bufferSize);

                while (true) {
                    int bytes = rbc.read(buffer);
                    if (bytes == -1) {
                        break;
                    }
                    if (buffer.remaining() == 0) {
                        buffer = resizeBuffer(buffer, buffer.capacity() * 2);
                    }
                }
            }
        }

        buffer.flip();
        return buffer;
}
 
Example 14
Project: SteamAudio-Java   File: Utils.java   View Source Code Vote up 5 votes
public static ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException {
    ByteBuffer buffer;

    Path path = Paths.get(resource);
    if (Files.isReadable(path)) {
        try (SeekableByteChannel fc = Files.newByteChannel(path)) {
            buffer = BufferUtils.createByteBuffer((int) fc.size() + 1);
            while (fc.read(buffer) != -1) ;
        }
    } else {
        try (InputStream source = Utils.class.getResourceAsStream(resource);
             ReadableByteChannel rbc = Channels.newChannel(source)) {
            buffer = createByteBuffer(bufferSize);

            while (true) {
                int bytes = rbc.read(buffer);
                if (bytes == -1) {
                    break;
                }
                if (buffer.remaining() == 0) {
                    buffer = resizeBuffer(buffer, buffer.capacity() * 2);
                }
            }
        }
    }

    buffer.flip();
    return buffer;
}
 
Example 15
Project: googles-monorepo-demo   File: MoreFiles.java   View Source Code Vote up 5 votes
@Override
public byte[] read() throws IOException {
  try (SeekableByteChannel channel = Files.newByteChannel(path, options)) {
    return com.google.common.io.Files.readFile(
        Channels.newInputStream(channel), channel.size());
  }
}
 
Example 16
Project: openjdk-jdk10   File: DflCache.java   View Source Code Vote up 5 votes
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
Project: jdk8u-jdk   File: DflCache.java   View Source Code Vote up 4 votes
private int loadAndCheck(Path p, AuthTimeWithHash time,
        KerberosTime currTime)
        throws IOException, KrbApErrException {
    int missed = 0;
    if (Files.isSymbolicLink(p)) {
        throw new IOException("Symlink not accepted");
    }
    try {
        Set<PosixFilePermission> perms =
                Files.getPosixFilePermissions(p);
        if (uid != -1 &&
                (Integer)Files.getAttribute(p, "unix:uid") != uid) {
            throw new IOException("Not mine");
        }
        if (perms.contains(PosixFilePermission.GROUP_READ) ||
                perms.contains(PosixFilePermission.GROUP_WRITE) ||
                perms.contains(PosixFilePermission.GROUP_EXECUTE) ||
                perms.contains(PosixFilePermission.OTHERS_READ) ||
                perms.contains(PosixFilePermission.OTHERS_WRITE) ||
                perms.contains(PosixFilePermission.OTHERS_EXECUTE)) {
            throw new IOException("Accessible by someone else");
        }
    } catch (UnsupportedOperationException uoe) {
        // No POSIX permissions? Ignore it.
    }
    chan = Files.newByteChannel(p, StandardOpenOption.WRITE,
            StandardOpenOption.READ);

    long timeLimit = currTime.getSeconds() - readHeader(chan);

    long pos = 0;
    boolean seeNewButNotSame = false;
    while (true) {
        try {
            pos = chan.position();
            AuthTime a = AuthTime.readFrom(chan);
            if (a instanceof AuthTimeWithHash) {
                if (time.equals(a)) {
                    // Exact match, must be a replay
                    throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
                } else if (time.isSameIgnoresHash(a)) {
                    // Two different authenticators in the same second.
                    // Remember it
                    seeNewButNotSame = true;
                }
            } else {
                if (time.isSameIgnoresHash(a)) {
                    // Two authenticators in the same second. Considered
                    // same if we haven't seen a new style version of it
                    if (!seeNewButNotSame) {
                        throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
                    }
                }
            }
            if (a.ctime < timeLimit) {
                missed++;
            } else {
                missed--;
            }
        } catch (BufferUnderflowException e) {
            // Half-written file?
            chan.position(pos);
            break;
        }
    }
    return missed;
}
 
Example 18
Project: openjdk-jdk10   File: DeleteOnClose.java   View Source Code Vote up 4 votes
public static void runTest(Path path) throws Exception {
    // check temporary file has been deleted after jvm termination
    if (Files.exists(path)) {
        throw new RuntimeException("Temporary file was not deleted");
    }

    // check temporary file has been deleted after closing it
    Path file = Files.createTempFile("blep", "tmp");
    Files.newByteChannel(file, READ, WRITE, DELETE_ON_CLOSE).close();
    if (Files.exists(file))
        throw new RuntimeException("Temporary file was not deleted");

    Path dir = Files.createTempDirectory("blah");
    try {
        // check that DELETE_ON_CLOSE fails when file is a sym link
        if (TestUtil.supportsLinks(dir)) {
            file = dir.resolve("foo");
            Files.createFile(file);
            Path link = dir.resolve("link");
            Files.createSymbolicLink(link, file);
            try {
                Files.newByteChannel(link, READ, WRITE, DELETE_ON_CLOSE);
                throw new RuntimeException("IOException expected");
            } catch (IOException ignore) { }
        }

        // check that DELETE_ON_CLOSE works with files created via open
        // directories
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
            if (stream instanceof SecureDirectoryStream) {
                SecureDirectoryStream<Path> secure = (SecureDirectoryStream<Path>)stream;
                file = Paths.get("foo");

                Set<OpenOption> opts = new HashSet<>();
                opts.add(WRITE);
                opts.add(DELETE_ON_CLOSE);
                secure.newByteChannel(file, opts).close();

                if (Files.exists(dir.resolve(file)))
                    throw new RuntimeException("File not deleted");
            }
        }
    } finally {
        TestUtil.removeAll(dir);
    }
}
 
Example 19
Project: openjdk-jdk10   File: DflCache.java   View Source Code Vote up 4 votes
private int loadAndCheck(Path p, AuthTimeWithHash time,
        KerberosTime currTime)
        throws IOException, KrbApErrException {
    int missed = 0;
    if (Files.isSymbolicLink(p)) {
        throw new IOException("Symlink not accepted");
    }
    try {
        Set<PosixFilePermission> perms =
                Files.getPosixFilePermissions(p);
        if (uid != -1 &&
                (Integer)Files.getAttribute(p, "unix:uid") != uid) {
            throw new IOException("Not mine");
        }
        if (perms.contains(PosixFilePermission.GROUP_READ) ||
                perms.contains(PosixFilePermission.GROUP_WRITE) ||
                perms.contains(PosixFilePermission.GROUP_EXECUTE) ||
                perms.contains(PosixFilePermission.OTHERS_READ) ||
                perms.contains(PosixFilePermission.OTHERS_WRITE) ||
                perms.contains(PosixFilePermission.OTHERS_EXECUTE)) {
            throw new IOException("Accessible by someone else");
        }
    } catch (UnsupportedOperationException uoe) {
        // No POSIX permissions? Ignore it.
    }
    chan = Files.newByteChannel(p, StandardOpenOption.WRITE,
            StandardOpenOption.READ);

    long timeLimit = currTime.getSeconds() - readHeader(chan);

    long pos = 0;
    boolean seeNewButNotSame = false;
    while (true) {
        try {
            pos = chan.position();
            AuthTime a = AuthTime.readFrom(chan);
            if (a instanceof AuthTimeWithHash) {
                if (time.equals(a)) {
                    // Exact match, must be a replay
                    throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
                } else if (time.sameTimeDiffHash((AuthTimeWithHash)a)) {
                    // Two different authenticators in the same second.
                    // Remember it
                    seeNewButNotSame = true;
                }
            } else {
                if (time.isSameIgnoresHash(a)) {
                    // Two authenticators in the same second. Considered
                    // same if we haven't seen a new style version of it
                    if (!seeNewButNotSame) {
                        throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
                    }
                }
            }
            if (a.ctime < timeLimit) {
                missed++;
            } else {
                missed--;
            }
        } catch (BufferUnderflowException e) {
            // Half-written file?
            chan.position(pos);
            break;
        }
    }
    return missed;
}
 
Example 20
Project: openjdk-jdk10   File: ZipFSTester.java   View Source Code Vote up 4 votes
static void channel(FileSystem fs, Path path)
    throws Exception
{
    System.out.println("test ByteChannel...");
    Set<OpenOption> read = new HashSet<>();
    read.add(READ);
    int n = 0;
    ByteBuffer bb = null;
    ByteBuffer bb2 = null;
    int N = 120;

    try (SeekableByteChannel sbc = Files.newByteChannel(path)) {
        System.out.printf("   sbc[0]: pos=%d, size=%d%n", sbc.position(), sbc.size());
        if (sbc.position() != 0) {
            throw new RuntimeException("CHECK FAILED!");
        }

        bb = ByteBuffer.allocate((int)sbc.size());
        n = sbc.read(bb);
        System.out.printf("   sbc[1]: read=%d, pos=%d, size=%d%n",
                          n, sbc.position(), sbc.size());
        if (sbc.position() != sbc.size()) {
            throw new RuntimeException("CHECK FAILED!");
        }
        bb2 = ByteBuffer.allocate((int)sbc.size());
    }

    // sbc.position(pos) is not supported in current version
    // try the FileChannel
    try (SeekableByteChannel sbc = fs.provider().newFileChannel(path, read)) {
        sbc.position(N);
        System.out.printf("   sbc[2]: pos=%d, size=%d%n",
                          sbc.position(), sbc.size());
        if (sbc.position() != N) {
            throw new RuntimeException("CHECK FAILED!");
        }
        bb2.limit(100);
        n = sbc.read(bb2);
        System.out.printf("   sbc[3]: read=%d, pos=%d, size=%d%n",
                          n, sbc.position(), sbc.size());
        if (n < 0 || sbc.position() != (N + n)) {
            throw new RuntimeException("CHECK FAILED!");
        }
        System.out.printf("   sbc[4]: bb[%d]=%d, bb1[0]=%d%n",
                          N, bb.get(N) & 0xff, bb2.get(0) & 0xff);
    }
}