本文整理汇总了Java中java.nio.channels.SeekableByteChannel.write方法的典型用法代码示例。如果您正苦于以下问题:Java SeekableByteChannel.write方法的具体用法?Java SeekableByteChannel.write怎么用?Java SeekableByteChannel.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.SeekableByteChannel
的用法示例。
在下文中一共展示了SeekableByteChannel.write方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: xorFiles
import java.nio.channels.SeekableByteChannel; //导入方法依赖的package包/类
public static void xorFiles(SeekableByteChannel inputA, SeekableByteChannel inputB,
SeekableByteChannel outputChannel, long limit) throws IOException {
ByteBuffer aBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
ByteBuffer bBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
ByteBuffer outBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
do {
if (inputA.position() == inputA.size()) inputA.position(0);
if (inputB.position() == inputB.size()) inputB.position(0);
inputA.read(aBuffer);
inputB.read(bBuffer);
aBuffer.flip();
bBuffer.flip();
int cap = min(aBuffer.remaining(), bBuffer.remaining(), outBuffer.remaining());
for (int i = 0; i < cap; i += 8) {
long a = aBuffer.getLong();
long b = bBuffer.getLong();
outBuffer.putLong(a ^ b);
}
aBuffer.compact();
bBuffer.compact();
outBuffer.flip();
long bytesRequired = limit - outputChannel.size();
if (outBuffer.limit() > bytesRequired) outBuffer.limit((int) bytesRequired);
outputChannel.write(outBuffer);
outBuffer.compact();
} while (outputChannel.size() < limit);
}
示例2: createNoClose
import java.nio.channels.SeekableByteChannel; //导入方法依赖的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;
}
示例3: main
import java.nio.channels.SeekableByteChannel; //导入方法依赖的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);
}
}
示例4: main
import java.nio.channels.SeekableByteChannel; //导入方法依赖的package包/类
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);
}
}
示例5: fileCreatedAfterStartIsReadAfterPermissionsFixed
import java.nio.channels.SeekableByteChannel; //导入方法依赖的package包/类
@Test
public void fileCreatedAfterStartIsReadAfterPermissionsFixed() throws Exception {
final File file = temporaryFolder.newFile();
final Path path = file.toPath();
// make sure the file doesn't exist prior to this test
Files.deleteIfExists(path);
final FileInput mockInput = mockFileInput();
final CollectingBuffer buffer = new CollectingBuffer();
final MessageBuilder messageBuilder = new MessageBuilder().input("input-id").outputs(new HashSet<String>()).source("test");
final FileReaderService readerService = new FileReaderService(
new SinglePathSet(path.toString()),
Charsets.UTF_8,
FileInput.InitialReadPosition.START,
mockInput,
messageBuilder,
new NewlineChunkSplitter(),
buffer,
1024,
250L,
fileObserver);
readerService.startAsync();
readerService.awaitRunning();
// create new unreadable file and write a line to it
final SeekableByteChannel channel = Files.newByteChannel(file.toPath(),
Sets.newHashSet(StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND),
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("---------")));
channel.write(ByteBuffer.wrap("hellotest\n".getBytes()));
// Check that there has been no message read from the file.
final Message msgNull = buffer.getMessageQueue().poll(500, TimeUnit.MILLISECONDS);
assertNull("There should be no message read from the file!", msgNull);
// Make file readable.
Files.setPosixFilePermissions(file.toPath(), Sets.newHashSet(PosixFilePermission.OWNER_READ));
// the reader service should detect the file modifcation event, create a chunkreader for it and eventually
// a message should appear in the buffer
// let's wait for that
final Message msg = buffer.getMessageQueue().poll(10, TimeUnit.SECONDS);
assertNotNull("file reader should have created a message", msg);
assertEquals("message content matches", "hellotest", msg.getMessage());
assertEquals("no more messages have been added to the buffer", 0, buffer.getMessageQueue().size());
readerService.stopAsync();
readerService.awaitTerminated();
}