当前位置: 首页>>代码示例>>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;未经允许,请勿转载。