本文整理汇总了Java中java.nio.channels.SeekableByteChannel.read方法的典型用法代码示例。如果您正苦于以下问题:Java SeekableByteChannel.read方法的具体用法?Java SeekableByteChannel.read怎么用?Java SeekableByteChannel.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.SeekableByteChannel
的用法示例。
在下文中一共展示了SeekableByteChannel.read方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readStringWithLength
import java.nio.channels.SeekableByteChannel; //导入方法依赖的package包/类
/**
* Reads an LC style string from a channel, which is a int32 length
* plus a UTF-8 encoded string possibly ends with \0.
* @throws IOException if there is a format error
* @throws BufferUnderflowException if goes beyond the end
*/
private static String readStringWithLength(SeekableByteChannel chan)
throws IOException {
ByteBuffer bb = ByteBuffer.allocate(4);
bb.order(ByteOrder.nativeOrder());
chan.read(bb);
bb.flip();
int len = bb.getInt();
if (len > 1024) {
// Memory attack? The string should be fairly short.
throw new IOException("Invalid string length");
}
bb = ByteBuffer.allocate(len);
if (chan.read(bb) != len) {
throw new IOException("Not enough string");
}
byte[] data = bb.array();
return (data[len-1] == 0)?
new String(data, 0, len-1, StandardCharsets.UTF_8):
new String(data, StandardCharsets.UTF_8);
}
示例2: 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);
}
示例3: readHeader
import java.nio.channels.SeekableByteChannel; //导入方法依赖的package包/类
private static int readHeader(SeekableByteChannel chan)
throws IOException {
ByteBuffer bb = ByteBuffer.allocate(6);
chan.read(bb);
if (bb.getShort(0) != KRB5_RV_VNO) {
throw new IOException("Not correct rcache version");
}
bb.order(ByteOrder.nativeOrder());
return bb.getInt(2);
}
示例4: readFrom
import java.nio.channels.SeekableByteChannel; //导入方法依赖的package包/类
/**
* Reads an AuthTime or AuthTimeWithHash object from a channel.
* @throws IOException if there is a format error
* @throws BufferUnderflowException if goes beyond the end
*/
public static AuthTime readFrom(SeekableByteChannel chan)
throws IOException {
String client = readStringWithLength(chan);
String server = readStringWithLength(chan);
ByteBuffer bb = ByteBuffer.allocate(8);
chan.read(bb);
bb.order(ByteOrder.nativeOrder());
int cusec = bb.getInt(0);
int ctime = bb.getInt(4);
if (client.isEmpty()) {
StringTokenizer st = new StringTokenizer(server, " :");
if (st.countTokens() != 6) {
throw new IOException("Incorrect rcache style");
}
st.nextToken();
String hash = st.nextToken();
st.nextToken();
client = st.nextToken();
st.nextToken();
server = st.nextToken();
return new AuthTimeWithHash(
client, server, ctime, cusec, hash);
} else {
return new AuthTime(
client, server, ctime, cusec);
}
}
示例5: readFrom
import java.nio.channels.SeekableByteChannel; //导入方法依赖的package包/类
/**
* Reads an AuthTime or AuthTimeWithHash object from a channel.
* @throws IOException if there is a format error
* @throws BufferUnderflowException if goes beyond the end
*/
public static AuthTime readFrom(SeekableByteChannel chan)
throws IOException {
String client = readStringWithLength(chan);
String server = readStringWithLength(chan);
ByteBuffer bb = ByteBuffer.allocate(8);
chan.read(bb);
bb.order(ByteOrder.nativeOrder());
int cusec = bb.getInt(0);
int ctime = bb.getInt(4);
if (client.isEmpty()) {
StringTokenizer st = new StringTokenizer(server, " :");
if (st.countTokens() != 6) {
throw new IOException("Incorrect rcache style");
}
String hashAlg = st.nextToken();
String hash = st.nextToken();
st.nextToken();
client = st.nextToken();
st.nextToken();
server = st.nextToken();
return new AuthTimeWithHash(
client, server, ctime, cusec, hashAlg, hash);
} else {
return new AuthTime(
client, server, ctime, cusec);
}
}