当前位置: 首页>>代码示例>>Java>>正文


Java SeekableByteChannel.read方法代码示例

本文整理汇总了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);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:27,代码来源:AuthTime.java

示例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);
}
 
开发者ID:zeobviouslyfakeacc,项目名称:ModLoaderInstaller,代码行数:31,代码来源:FileUtils.java

示例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);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:11,代码来源:DflCache.java

示例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);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:33,代码来源:AuthTime.java

示例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);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:33,代码来源:AuthTime.java


注:本文中的java.nio.channels.SeekableByteChannel.read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。