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


Java ByteBuffer.hasRemaining方法代码示例

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


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

示例1: write

import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
public void write(byte[] b, int off, int len) throws IOException {
  ByteBuffer buf = ByteBuffer.wrap(b, off, len);
  while (buf.hasRemaining()) {
    try {
      if (write(buf) < 0) {
        throw new IOException("The stream is closed");
      }
    } catch (IOException e) {
      /* Unlike read, write can not inform user of partial writes.
       * So will close this if there was a partial write.
       */
      if (buf.capacity() > buf.remaining()) {
        writer.close();
      }
      throw e;
    }
  }
}
 
开发者ID:naver,项目名称:hadoop,代码行数:20,代码来源:SocketOutputStream.java

示例2: consume

import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
public void consume(ByteBuffer buf) throws IOException {
    if (!buf.hasRemaining()) {
        return;
    }

    if (buf.hasArray()) {
        consume(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining());
        buf.position(buf.limit());
        return;
    }

    ensureAvailable(buf.remaining());
    byte[] tmp = new byte[Math.min(buf.remaining(), MAX_READ_CHUNK_SIZE)];
    while (buf.hasRemaining()) {
        int chunkSize = Math.min(buf.remaining(), tmp.length);
        buf.get(tmp, 0, chunkSize);
        System.arraycopy(tmp, 0, mArray, mSize, chunkSize);
        mSize += chunkSize;
    }
}
 
开发者ID:F8LEFT,项目名称:FApkSigner,代码行数:22,代码来源:ByteArrayDataSink.java

示例3: ConstraintFailureException

import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
 * Constructor for deserializing an exception returned from the EE.
 * @param exceptionBuffer
 */
public ConstraintFailureException(ByteBuffer exceptionBuffer) {
    super(exceptionBuffer);
    type = ConstraintType.get(exceptionBuffer.getInt());
    try {
        tableName = FastDeserializer.readString(exceptionBuffer);
    }
    catch (IOException e) {
        // implies that the EE created an invalid constraint
        // failure, which would be a corruption/defect.
        throw new ServerFaultException("Unexpected error when deserializing exception from EE", e);
    }
    if (exceptionBuffer.hasRemaining()) {
        int tableSize = exceptionBuffer.getInt();
        buffer = ByteBuffer.allocate(tableSize);
        //Copy the exception details.
        exceptionBuffer.get(buffer.array());
    } else {
        buffer = ByteBuffer.allocate(0);
    }
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:25,代码来源:ConstraintFailureException.java

示例4: main

import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    try (RandomAccessFile aFile = new RandomAccessFile("data/data.txt", "rw")) {
        FileChannel inChannel = aFile.getChannel();

        ByteBuffer buf = ByteBuffer.allocate(1024);

        int bytesRead = inChannel.read(buf);
        while (bytesRead != -1) {

            System.out.println("Read " + bytesRead);
            buf.flip();

            while (buf.hasRemaining()) {
                System.out.print((char) buf.get());
            }
            System.out.print("\n");
            buf.clear();
            bytesRead = inChannel.read(buf);
        }
    }
}
 
开发者ID:vitaly-chibrikov,项目名称:otus_java_2017_06,代码行数:22,代码来源:NioMain.java

示例5: copyArray

import java.nio.ByteBuffer; //导入方法依赖的package包/类
public void copyArray() {
    ByteBuffer buffer = ByteBuffer.allocate(4096);
    while (buffer.hasRemaining()) {
        buffer.put((byte) 1);
    }
    byte[] b = new byte[1024];
    int count = 10000000;
    System.currentTimeMillis();

    long t1 = System.currentTimeMillis();
    for (int i = 0; i < count; i++) {
        buffer.position(0);
        buffer.get(b, 0, b.length);
    }
    long t2 = System.currentTimeMillis();
    System.out.println("take time:" + (t2 - t1) + " ms.(testCopyArray)");
}
 
开发者ID:huang-up,项目名称:mycat-src-1.6.1-RELEASE,代码行数:18,代码来源:BufferPerformanceMain.java

示例6: decodeBufferLoop

import java.nio.ByteBuffer; //导入方法依赖的package包/类
private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
    int mark = src.position();
    try {
        while (src.hasRemaining()) {
            int b = src.get();

            char c = decode(b);
            if (c == '\uFFFD')
                return CoderResult.unmappableForLength(1);
            if (!dst.hasRemaining())
                return CoderResult.OVERFLOW;
            mark++;
            dst.put(c);
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:SingleByteDecoder.java

示例7: write

import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
public int write(String name, ByteBuffer src) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), false, true);

    int fd = -1;
    try {
        try {
            fd = file.openForAttributeAccess(followLinks);

            // open/create attribute file
            int afd = openat(fd, nameAsBytes(file,name),
                             (O_CREAT|O_WRONLY|O_TRUNC|O_XATTR),
                             UnixFileModeAttribute.ALL_PERMISSIONS);

            // wrap with channel
            FileChannel fc = UnixChannelFactory.newFileChannel(afd, file.toString(), false, true);

            // write value (nothing we can do if I/O error occurs)
            try {
                int rem = src.remaining();
                while (src.hasRemaining()) {
                    fc.write(src);
                }
                return rem;
            } finally {
                fc.close();
            }
        } catch (UnixException x) {
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Unable to write extended attribute '" + name +
                "': " + x.getMessage());
        }
    } finally {
        close(fd);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:38,代码来源:SolarisUserDefinedFileAttributeView.java

示例8: send

import java.nio.ByteBuffer; //导入方法依赖的package包/类
public void send(Object obj) {
    if (httpServerExchange.getRequestMethod()
            .equals(HEAD)) {
        httpServerExchange.endExchange();
        return;
    }
    httpServerExchange.getResponseHeaders()
            .put(Headers.CONTENT_TYPE, JSON_CONTENT_TYPE);

    ObjectMapper objectMapper = LOCAL_MAPPER.get();
    if (objectMapper == null) {
        LOGGER.info("Creating new parser.........");
        objectMapper = JsonFactory.createUseJSONDates();
        LOCAL_MAPPER.set(objectMapper);
    }

    String jsonStr = objectMapper
            .toJson(obj);
    ByteBuffer byteBuffer = ByteBuffer.wrap(jsonStr.getBytes(StandardCharsets.UTF_8));
    StreamSinkChannel responseChannel = httpServerExchange.getResponseChannel();
    int written;
    try {
        do {
            written = responseChannel.write(byteBuffer);
        } while (byteBuffer.hasRemaining() && written > 0);
    } catch (IOException e) {
        LOGGER.error("Can not write response: " + e.getMessage(), e);
    }
}
 
开发者ID:icha024,项目名称:spur,代码行数:30,代码来源:Res.java

示例9: processRemaining

import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
protected void processRemaining(ByteBuffer bb) {
  length += bb.remaining();
  int k1 = 0;
  for (int i = 0; bb.hasRemaining(); i += 8) {
    k1 ^= toInt(bb.get()) << i;
  }
  h1 ^= Murmur3_32HashFunction.mixK1(k1);
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:10,代码来源:Murmur3_32HashFunction.java

示例10: blockingRead

import java.nio.ByteBuffer; //导入方法依赖的package包/类
private void blockingRead(UpgradeProcessor<?> processor, ByteBuffer bb) throws IOException {
	int last = 0;
	while (bb.hasRemaining()) {
		last = processor.read();
		if (last == -1) {
			throw new IOException(sm.getString("frame.eos"));
		}
		bb.put((byte) (last ^ mask[bb.position() % 4]));
	}
	bb.flip();
}
 
开发者ID:how2j,项目名称:lazycat,代码行数:12,代码来源:WsFrame.java

示例11: onBytesRead

import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
 * Consumes a portion of the response.
 *
 * @param byteBuffer The ByteBuffer to append. Must be a direct buffer, and
 *            no references to it may be retained after the method ends, as
 *            it wraps code managed on the native heap.
 */
@CalledByNative
private void onBytesRead(ByteBuffer buffer) {
    try {
        if (mContentLengthOverLimit) {
            return;
        }

        int size = buffer.remaining();
        mSize += size;
        if (mSkippingToOffset) {
            if (mSize <= mOffset) {
                return;
            } else {
                mSkippingToOffset = false;
                buffer.position((int) (mOffset - (mSize - size)));
            }
        }

        boolean contentLengthOverLimit =
                (mContentLengthLimit != 0 && mSize > mContentLengthLimit);
        if (contentLengthOverLimit) {
            buffer.limit(size - (int) (mSize - mContentLengthLimit));
        }

        while (buffer.hasRemaining()) {
            mSink.write(buffer);
        }
        if (contentLengthOverLimit) {
            onContentLengthOverLimit();
        }
    } catch (Exception e) {
        onCalledByNativeException(e);
    }
}
 
开发者ID:lizhangqu,项目名称:chromium-net-for-android,代码行数:42,代码来源:ChromiumUrlRequest.java

示例12: read

import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
public int read(ByteBuffer dst, long position) throws IOException {
    if (!dst.hasRemaining()) {
        return 0;
    }
    S3Object object = s3.getObject(new GetObjectRequest(bucket, key)
            .withRange(position, position + dst.remaining()));
    return ByteBufferUtils.readFromInputStream(object.getObjectContent(), dst, true);
}
 
开发者ID:mentegy,项目名称:s3-channels,代码行数:10,代码来源:S3RangedReadObjectChannel.java

示例13: append

import java.nio.ByteBuffer; //导入方法依赖的package包/类
public boolean append(BrokerMessage.MessageContent.Builder messageBuilder) {
    try {
        if (fileSize == 0) {
            messageBuilder.setOffset(startOffset);
        } else {
            messageBuilder.setOffset(endOffset);
        }
        messageBuilder.setCreateTime(System.currentTimeMillis());
        BrokerMessage.MessageContent message = messageBuilder.build();
        byte[] messageBytes = message.toByteArray();
        int totalSize = Segment.MESSAGE_HEADER_LENGTH + messageBytes.length;
        ByteBuffer byteBuffer = ByteBuffer.allocate(totalSize);
        byteBuffer.putLong(BrokerUtils.getCRC32(messageBytes));
        byteBuffer.putInt(messageBytes.length);
        byteBuffer.put(messageBytes);
        byteBuffer.flip();
        channel.position(channel.size());
        int writeSize = 0;
        while (byteBuffer.hasRemaining()) {
            writeSize += channel.write(byteBuffer);
        }
        channel.force(true);
        if (writeSize != totalSize) {
            LOG.warn("append message failed");
            return false;
        }
        if (fileSize == 0) {
            endOffset = startOffset + writeSize;
        } else {
            endOffset += writeSize;
        }
        fileSize += writeSize;
    } catch (IOException ex) {
        LOG.warn("append message exception:", ex);
        return false;
    }
    return true;
}
 
开发者ID:wenweihu86,项目名称:distmq,代码行数:39,代码来源:Segment.java

示例14: fill

import java.nio.ByteBuffer; //导入方法依赖的package包/类
private static ByteBuffer fill(ByteBuffer b) {
    int i = 0;
    while (b.hasRemaining()) {
        b.put((byte) (++i & 0xff));
    }
    return b;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:8,代码来源:DataProviders.java

示例15: writeTo

import java.nio.ByteBuffer; //导入方法依赖的package包/类
public void writeTo(OutputStream out) throws IOException {
    byte[] buffer = new byte[8192];
    ByteBuffer data = this.data.duplicate(); // positioned ByteBuffers aren't thread safe
    data.clear();
    while (data.hasRemaining()) {
        int count = Math.min(buffer.length, data.remaining());
        data.get(buffer, 0, count);
        out.write(buffer, 0, count);
    }
}
 
开发者ID:alibaba,项目名称:atlas,代码行数:11,代码来源:Dex.java


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