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