本文整理匯總了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();
}