當前位置: 首頁>>代碼示例>>Java>>正文


Java Files.newByteChannel方法代碼示例

本文整理匯總了Java中java.nio.file.Files.newByteChannel方法的典型用法代碼示例。如果您正苦於以下問題:Java Files.newByteChannel方法的具體用法?Java Files.newByteChannel怎麽用?Java Files.newByteChannel使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.nio.file.Files的用法示例。


在下文中一共展示了Files.newByteChannel方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: hash

import java.nio.file.Files; //導入方法依賴的package包/類
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();
}
 
開發者ID:sfPlayer1,項目名稱:Matcher,代碼行數:18,代碼來源:InputFile.java

示例2: csize

import java.nio.file.Files; //導入方法依賴的package包/類
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;
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:18,代碼來源:ReplayCacheTestProc.java

示例3: copySourceFilesToTarget

import java.nio.file.Files; //導入方法依賴的package包/類
@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));
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:17,代碼來源:FileSystemUtilsTests.java

示例4: partitionRead

import java.nio.file.Files; //導入方法依賴的package包/類
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);

        }
    }
 
開發者ID:ctripcorp,項目名稱:cornerstone,代碼行數:17,代碼來源:IOUtils.java

示例5: csize

import java.nio.file.Files; //導入方法依賴的package包/類
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;
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:18,代碼來源:ReplayCacheTestProc.java

示例6: readFromFile

import java.nio.file.Files; //導入方法依賴的package包/類
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;
}
 
開發者ID:kogupta,項目名稱:scala-playground,代碼行數:25,代碼來源:FilePersister.java

示例7: main

import java.nio.file.Files; //導入方法依賴的package包/類
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);
        }
    }
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:33,代碼來源:ReplayCachePrecise.java

示例8: reverseRead

import java.nio.file.Files; //導入方法依賴的package包/類
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);

    }
}
 
開發者ID:ctripcorp,項目名稱:cornerstone,代碼行數:16,代碼來源:IOUtils.java

示例9: ioResourceToByteBuffer

import java.nio.file.Files; //導入方法依賴的package包/類
/**
 * 
 * @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;
}
 
開發者ID:brokenprogrammer,項目名稱:Mass,代碼行數:41,代碼來源:IOUtil.java

示例10: main

import java.nio.file.Files; //導入方法依賴的package包/類
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);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:DeleteOnClose.java

示例11: openStream

import java.nio.file.Files; //導入方法依賴的package包/類
@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);
}
 
開發者ID:nla,項目名稱:httrack2warc,代碼行數:8,代碼來源:NdxCache.java

示例12: read

import java.nio.file.Files; //導入方法依賴的package包/類
@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());
  }
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:8,代碼來源:MoreFiles.java

示例13: ioResourceToByteBuffer

import java.nio.file.Files; //導入方法依賴的package包/類
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;
}
 
開發者ID:oreonengine,項目名稱:Lwjgl3-Game-Engine-Programming-Series,代碼行數:34,代碼來源:ImageLoader.java

示例14: ioResourceToByteBuffer

import java.nio.file.Files; //導入方法依賴的package包/類
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;
}
 
開發者ID:justjanne,項目名稱:SteamAudio-Java,代碼行數:30,代碼來源:Utils.java

示例15: createNoClose

import java.nio.file.Files; //導入方法依賴的package包/類
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;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:15,代碼來源:DflCache.java


注:本文中的java.nio.file.Files.newByteChannel方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。