本文整理汇总了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();
}
示例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;
}
}
示例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));
}
}
示例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);
}
}
示例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;
}
}
示例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;
}
示例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);
}
}
示例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);
}
}
示例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;
}
示例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);
}
}
示例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);
}
示例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());
}
}
示例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;
}
示例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;
}
示例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;
}