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


Java ByteBuffer.compact方法代码示例

本文整理汇总了Java中java.nio.ByteBuffer.compact方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuffer.compact方法的具体用法?Java ByteBuffer.compact怎么用?Java ByteBuffer.compact使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.ByteBuffer的用法示例。


在下文中一共展示了ByteBuffer.compact方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {

        RandomAccessFile file = new RandomAccessFile(FILE_PATH, "rw");

        FileChannel channel = file.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(48);

        int read = channel.read(byteBuffer);
        while (read != -1) {
            byteBuffer.flip();

            while (byteBuffer.hasRemaining()) {
                System.out.print((char)byteBuffer.get());
            }

            byteBuffer.compact();
            read = channel.read(byteBuffer);
        }
        file.close();
    }
 
开发者ID:daishicheng,项目名称:outcomes,代码行数:22,代码来源:Main.java

示例2: fastCopy

import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
     * copy
     *
     * @param src
     * @param dest
     * @throws IOException
     */
    public static void fastCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException {
        final ByteBuffer buffer = ByteBuffer.allocateDirect(8 * 1024);
        int count = 0;

        while ((count = src.read(buffer)) != -1) {
//            LogUtil.d("luaviewp-fastCopy", count, buffer.capacity(), buffer.remaining(), buffer.array().length);
            // prepare the buffer to be drained
            buffer.flip();
            // write to the channel, may block
            dest.write(buffer);
            // If partial transfer, shift remainder down
            // If buffer is empty, same as doing clear()
            buffer.compact();
        }
        // EOF will leave buffer in fill state
        buffer.flip();
        // make sure the buffer is fully drained.
        while (buffer.hasRemaining()) {
            dest.write(buffer);
        }
    }
 
开发者ID:alibaba,项目名称:LuaViewPlayground,代码行数:29,代码来源:ChannelTools.java

示例3: channelCopy

import java.nio.ByteBuffer; //导入方法依赖的package包/类
private static void channelCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException
{
    final ByteBuffer buffer = ByteBuffer.allocateDirect(2 * 1024);
    while (src.read(buffer) != -1)
    {
        // prepare the buffer to be drained
        buffer.flip();
        // write to the channel, may block
         dest.write(buffer);

        // If partial transfer, shift remainder down
        // If buffer is empty, same as doing clear()
        buffer.compact();
    }

    // EOF will leave buffer in fill state
    buffer.flip();

    // make sure the buffer is fully drained.
    while (buffer.hasRemaining())
    {
        dest.write(buffer);
    }
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:25,代码来源:HttpClientTransmitterImpl.java

示例4: deleteTagChunk

import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
 * Delete Tag Chunk
 * <p/>
 * Can be used when chunk is not the last chunk
 * <p/>
 * Continually copy a 4mb chunk, write the chunk and repeat until the rest of the file after the tag
 * is rewritten
 *
 * @param fc
 * @param endOfExistingChunk
 * @param lengthTagChunk
 * @throws IOException
 */
private void deleteTagChunk(final FileChannel fc, int endOfExistingChunk, final int lengthTagChunk) throws IOException {
    //Position for reading after the tag
    fc.position(endOfExistingChunk);

    final ByteBuffer buffer = ByteBuffer.allocate((int) TagOptionSingleton.getInstance().getWriteChunkSize());
    while (fc.read(buffer) >= 0 || buffer.position() != 0) {
        buffer.flip();
        final long readPosition = fc.position();
        fc.position(readPosition - lengthTagChunk - buffer.limit());
        fc.write(buffer);
        fc.position(readPosition);
        buffer.compact();
    }
    //Truncate the file after the last chunk
    final long newLength = fc.size() - lengthTagChunk;
    logger.config(loggingName + " Setting new length to:" + newLength);
    fc.truncate(newLength);
}
 
开发者ID:GlennioTech,项目名称:MetadataEditor,代码行数:32,代码来源:WavTagWriter.java

示例5: shift

import java.nio.ByteBuffer; //导入方法依赖的package包/类
ByteBufferReference shift(ByteBufferReference ref1, ByteBufferReference ref2) {
    ByteBuffer buf1 = ref1.get();
    if (buf1.capacity() < engine.getSession().getPacketBufferSize()) {
        ByteBufferReference newRef = getNetBuffer();
        ByteBuffer newBuf = newRef.get();
        newBuf.put(buf1);
        buf1 = newBuf;
        ref1.clear();
        ref1 = newRef;
    } else {
        buf1.compact();
    }
    ByteBuffer buf2 = ref2.get();
    Utils.copy(buf2, buf1, Math.min(buf1.remaining(), buf2.remaining()));
    buf1.flip();
    return ref1;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:AsyncSSLDelegate.java

示例6: combine

import java.nio.ByteBuffer; //导入方法依赖的package包/类
ByteBufferReference combine(ByteBufferReference ref1, ByteBufferReference ref2) {
    ByteBuffer buf1 = ref1.get();
    ByteBuffer buf2 = ref2.get();
    int avail1 = buf1.capacity() - buf1.remaining();
    if (buf2.remaining() < avail1) {
        buf1.compact();
        buf1.put(buf2);
        buf1.flip();
        ref2.clear();
        return ref1;
    }
    int newsize = buf1.remaining() + buf2.remaining();
    ByteBuffer newbuf = ByteBuffer.allocate(newsize); // getting rid of buffer pools
    newbuf.put(buf1);
    newbuf.put(buf2);
    newbuf.flip();
    ref1.clear();
    ref2.clear();
    return ByteBufferReference.of(newbuf);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:AsyncSSLDelegate.java

示例7: xorFiles

import java.nio.ByteBuffer; //导入方法依赖的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

示例8: compactReadBuffer

import java.nio.ByteBuffer; //导入方法依赖的package包/类
private ByteBuffer compactReadBuffer(ByteBuffer buffer, int offset) {
	if(buffer == null) {
		return null;
	}
	buffer.limit(buffer.position());
	buffer.position(offset);
	buffer = buffer.compact();
	readBufferOffset = 0;
	return buffer;
}
 
开发者ID:huang-up,项目名称:mycat-src-1.6.1-RELEASE,代码行数:11,代码来源:AbstractConnection.java

示例9: write0

import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
 * return true ,means no more data
 *
 * @return
 */
private boolean write0() {
    if (!writing.compareAndSet(false, true)) {
        return false;
    }
    ByteBuffer theBuffer = con.writeBuffer;
    if (theBuffer == null || !theBuffer.hasRemaining()) { // writeFinished,if buffer not NULL,recycle
        if (theBuffer != null) {
            con.recycle(theBuffer);
            con.writeBuffer = null;

        }
        // poll again
        ByteBuffer buffer = con.writeQueue.poll();
        // more data
        if (buffer != null) {
            if (buffer.limit() == 0) {
                con.recycle(buffer);
                con.writeBuffer = null;
                con.close("quit cmd");
                writing.set(false);
                return true;
            } else {
                con.writeBuffer = buffer;
                asyncWrite(buffer);
                return false;
            }
        } else {
            // no buffer
            writing.set(false);
            return true;
        }
    } else {
        theBuffer.compact();
        asyncWrite(theBuffer);
        return false;
    }

}
 
开发者ID:actiontech,项目名称:dble,代码行数:44,代码来源:AIOSocketWR.java

示例10: decode

import java.nio.ByteBuffer; //导入方法依赖的package包/类
public final void decode(SocketAddress address, byte[] array, int offset, int length) {
    ByteBuffer buffer = mAddressByteBufferMap.get(address);
    if (buffer == null) {
        buffer = ByteBuffer.allocate(CACHE_BUFFER_LENGTH);
        mAddressByteBufferMap.put(address, buffer);
        buffer.flip();
        LoggerFactory.getLogger().info("创建 " + address.toString() + " 数据缓冲区ByteBuffer");
    }
    LoggerFactory.getLogger().info(String.format("%s 上次未解码 position=%d limit=%d", address.toString(), buffer.position(), buffer.limit()));
    if (buffer.limit() + length > buffer.capacity()) {
        LoggerFactory.getLogger().warn(address.toString() + " -> decode缓存区已满,读取的数据被丢弃!!!!!");
        return;
    }
    buffer.compact();
    buffer.put(array, offset, length);
    buffer.flip();
    buffer.mark();
    LoggerFactory.getLogger().info(String.format("%s 合并未解码 position=%d limit=%d", address.toString(), buffer.position(), buffer.limit()));
    LoggerFactory.getLogger().info(address.toString() + " 开始解码数据");
    while (buffer.hasRemaining() && (decode(address, buffer) != null)) {
        LoggerFactory.getLogger().info(address.toString() + " 成功解码一条数据");
        buffer.compact();
        buffer.flip();
        buffer.mark();
    }
    LoggerFactory.getLogger().info(address.toString() + " 退出解码");
    buffer.reset();
    LoggerFactory.getLogger().info(String.format("%s 未解码数据 position=%d limit=%d", address.toString(), buffer.position(), buffer.limit()));
}
 
开发者ID:spacetimeme,项目名称:DreamSocket,代码行数:30,代码来源:MessageDecode.java

示例11: ungetRequestBytes

import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
 * Pushes back the given data. This should only be used by transfer coding handlers that have read past
 * the end of the request when handling pipelined requests
 *
 * @param unget The buffer to push back
 */
public void ungetRequestBytes(final Pooled<ByteBuffer> unget) {
    if (getExtraBytes() == null) {
        setExtraBytes(unget);
    } else {
        Pooled<ByteBuffer> eb = getExtraBytes();
        ByteBuffer buf = eb.getResource();
        final ByteBuffer ugBuffer = unget.getResource();

        if (ugBuffer.limit() - ugBuffer.remaining() > buf.remaining()) {
            //stuff the existing data after the data we are ungetting
            ugBuffer.compact();
            ugBuffer.put(buf);
            ugBuffer.flip();
            eb.free();
            setExtraBytes(unget);
        } else {
            //TODO: this is horrible, but should not happen often
            final byte[] data = new byte[ugBuffer.remaining() + buf.remaining()];
            int first = ugBuffer.remaining();
            ugBuffer.get(data, 0, ugBuffer.remaining());
            buf.get(data, first, buf.remaining());
            eb.free();
            unget.free();
            final ByteBuffer newBuffer = ByteBuffer.wrap(data);
            setExtraBytes(new ImmediatePooled<>(newBuffer));
        }
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:35,代码来源:HttpServerConnection.java

示例12: readBlocking

import java.nio.ByteBuffer; //导入方法依赖的package包/类
public void readBlocking(StreamSourceFrameChannel channel) throws IOException {
    Pooled<ByteBuffer> pooled = channel.getWebSocketChannel().getBufferPool().allocate();
    final ByteBuffer buffer = pooled.getResource();
    try {
        for (; ; ) {
            int res = channel.read(buffer);
            if (res == -1) {
                buffer.flip();
                data.write(buffer);
                this.complete = true;
                return;
            } else if (res == 0) {
                channel.awaitReadable();
            }
            checkMaxSize(channel, res);
            if (!buffer.hasRemaining()) {
                buffer.flip();
                data.write(buffer);
                buffer.compact();
                if (!bufferFullMessage) {
                    //if we are not reading the full message we return
                    return;
                }
            }
        }
    } finally {
        pooled.free();
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:30,代码来源:BufferedTextMessage.java

示例13: handleWrite

import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static void handleWrite(SelectionKey key) throws IOException {
	ByteBuffer buf = (ByteBuffer) key.attachment();
	buf.flip();
	SocketChannel sc = (SocketChannel) key.channel();
	while (buf.hasRemaining()) {
		sc.write(buf);
	}
	buf.compact();
}
 
开发者ID:spafka,项目名称:spark_deep,代码行数:10,代码来源:NioServer.java

示例14: copy

import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static void copy(ReadableByteChannel src, WritableByteChannel dest) throws IOException {
	final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);

	while (src.read(buffer) != -1) {
		buffer.flip();
		dest.write(buffer);
		buffer.compact();
	}
	buffer.flip();
	dest.write(buffer);
}
 
开发者ID:xtf-cz,项目名称:xtf,代码行数:12,代码来源:IOUtils.java

示例15: isStreamEqual

import java.nio.ByteBuffer; //导入方法依赖的package包/类
private static boolean isStreamEqual(InputStream i1, InputStream i2)
        throws IOException {

    ReadableByteChannel ch1 = Channels.newChannel(i1);
    ReadableByteChannel ch2 = Channels.newChannel(i2);

    ByteBuffer buf1 = ByteBuffer.allocateDirect(1024);
    ByteBuffer buf2 = ByteBuffer.allocateDirect(1024);

    try {
        while (true) {

            int n1 = ch1.read(buf1);
            int n2 = ch2.read(buf2);

            if (n1 == -1 || n2 == -1) return n1 == n2;

            buf1.flip();
            buf2.flip();

            for (int i = 0; i < Math.min(n1, n2); i++)
                if (buf1.get() != buf2.get())
                    return false;

            buf1.compact();
            buf2.compact();
        }

    } finally {
        if (i1 != null) i1.close();
        if (i2 != null) i2.close();
    }
}
 
开发者ID:eclipse,项目名称:gemoc-studio,代码行数:34,代码来源:IFileUtils.java


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