org.glassfish.grizzly.Buffer.compact()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(11.7k)|赞(0)|评价(0)|浏览(102)

本文整理了Java中org.glassfish.grizzly.Buffer.compact()方法的一些代码示例,展示了Buffer.compact()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Buffer.compact()方法的具体详情如下:
包路径:org.glassfish.grizzly.Buffer
类名称:Buffer
方法名:compact

Buffer.compact介绍

[英]Compacts this buffer  (optional operation).

The bytes between the buffer's current position and its limit, if any, are copied to the beginning of the buffer. That is, the byte at index p = position() is copied to index zero, the byte at index p + 1 is copied to index one, and so forth until the byte at index limit() - 1 is copied to index n = limit() - 1 - p. The buffer's position is then set to n+1 and its limit is set to its capacity. The mark, if defined, is discarded.

The buffer's position is set to the number of bytes copied, rather than to zero, so that an invocation of this method can be followed immediately by an invocation of another relative put method.

Invoke this method after writing data from a buffer in case the write was incomplete. The following loop, for example, copies bytes from one channel to another via the buffer buf:

buf.clear();          // Prepare buffer for use 
for (;;) { 
if (in.read(buf) < 0 && !buf.hasRemaining()) 
break;        // No more bytes to transfer 
buf.flip(); 
out.write(buf); 
buf.compact();    // In case of partial write 
}

[中]压缩此缓冲区(可选操作)。
缓冲区当前位置与其限制(如果有)之间的字节复制到缓冲区的开头。也就是说,索引p=position()处的字节复制到索引0,索引p+1处的字节复制到索引1,依此类推,直到索引limit()处的字节-1复制到索引n=limit()-1-p。然后将缓冲区的位置设置为n+1,并将其限制设置为其容量。标记(如果已定义)将被丢弃。
缓冲区的位置设置为复制的字节数,而不是零,因此调用此方法后可以立即调用另一个相对put方法。
在从缓冲区写入数据后调用此方法,以防写入不完整。例如,以下循环通过缓冲区buf将字节从一个通道复制到另一个通道:

buf.clear();          // Prepare buffer for use 
for (;;) { 
if (in.read(buf) < 0 && !buf.hasRemaining()) 
break;        // No more bytes to transfer 
buf.flip(); 
out.write(buf); 
buf.compact();    // In case of partial write 
}

代码示例

代码示例来源:origin: javaee/grizzly

transformer.release(attributeStorage);
} else if (status == Status.INCOMPLETE) {
  buffer.compact();
  if (!buffer.isComposite()) {
    buffer = CompositeBuffer.newBuffer(

代码示例来源:origin: javaee/grizzly

transformer.release(attributeStorage);
} else if (status == Status.INCOMPLETE) {
  buffer.compact();
  if (!buffer.isComposite()) {
    buffer = CompositeBuffer.newBuffer(

代码示例来源:origin: org.glassfish.grizzly/grizzly-websockets-server

transformer.release(attributeStorage);
} else if (status == Status.INCOMPLETE) {
  buffer.compact();
  if (!buffer.isComposite()) {
    buffer = CompositeBuffer.newBuffer(

代码示例来源:origin: org.glassfish.grizzly/grizzly-core

transformer.release(attributeStorage);
} else if (status == Status.INCOMPLETE) {
  buffer.compact();
  if (!buffer.isComposite()) {
    buffer = CompositeBuffer.newBuffer(

代码示例来源:origin: javaee/grizzly

transformer.release(attributeStorage);
} else if (status == Status.INCOMPLETE) {
  buffer.compact();
  if (!buffer.isComposite()) {
    buffer = CompositeBuffer.newBuffer(

代码示例来源:origin: org.mule.glassfish.grizzly/grizzly-framework

transformer.release(attributeStorage);
} else if (status == Status.INCOMPLETE) {
  buffer.compact();
  if (!buffer.isComposite()) {
    buffer = CompositeBuffer.newBuffer(

代码示例来源:origin: org.glassfish.grizzly/grizzly-http-server-core

transformer.release(attributeStorage);
} else if (status == Status.INCOMPLETE) {
  buffer.compact();
  if (!buffer.isComposite()) {
    buffer = CompositeBuffer.newBuffer(

代码示例来源:origin: javaee/grizzly

transformer.release(attributeStorage);
} else if (status == Status.INCOMPLETE) {
  buffer.compact();
  if (!buffer.isComposite()) {
    buffer = CompositeBuffer.newBuffer(

代码示例来源:origin: javaee/grizzly

@Override
public BuffersBuffer compact() {
  checkDispose();
  if (buffersSize == 0) {
    return this;
  } else if (buffersSize == 1) {
    final Buffer buffer = buffers[0];
    Buffers.setPositionLimit(buffer, buffer.position() + position,
        buffer.position() + limit);
    buffer.compact();
  } else {
    checkIndex(position);
    final int posBufferIndex = lastSegmentIndex;
    activeBuffer.position(toActiveBufferPos(position));
    checkIndex(limit - 1);
    final int limitBufferIndex = lastSegmentIndex;
    activeBuffer.limit(toActiveBufferPos(limit));
    for(int i = posBufferIndex; i <= limitBufferIndex; i++) {
      final Buffer b1 = buffers[i - posBufferIndex];
      buffers[i - posBufferIndex] = buffers[i];
      buffers[i] = b1;
    }
  }
  
  setPosLim(0, position);
  refreshBuffers();
  
  resetLastLocation();
  return this;
}

代码示例来源:origin: javaee/grizzly

@Override
public BuffersBuffer compact() {
  checkDispose();
  if (buffersSize == 0) {
    return this;
  } else if (buffersSize == 1) {
    final Buffer buffer = buffers[0];
    Buffers.setPositionLimit(buffer, buffer.position() + position,
        buffer.position() + limit);
    buffer.compact();
  } else {
    checkIndex(position);
    final int posBufferIndex = lastSegmentIndex;
    activeBuffer.position(toActiveBufferPos(position));
    checkIndex(limit - 1);
    final int limitBufferIndex = lastSegmentIndex;
    activeBuffer.limit(toActiveBufferPos(limit));
    for(int i = posBufferIndex; i <= limitBufferIndex; i++) {
      final Buffer b1 = buffers[i - posBufferIndex];
      buffers[i - posBufferIndex] = buffers[i];
      buffers[i] = b1;
    }
  }
  
  setPosLim(0, position);
  refreshBuffers();
  
  resetLastLocation();
  return this;
}

代码示例来源:origin: javaee/grizzly

@Override
public BuffersBuffer compact() {
  checkDispose();
  if (buffersSize == 0) {
    return this;
  } else if (buffersSize == 1) {
    final Buffer buffer = buffers[0];
    Buffers.setPositionLimit(buffer, buffer.position() + position,
        buffer.position() + limit);
    buffer.compact();
  } else {
    checkIndex(position);
    final int posBufferIndex = lastSegmentIndex;
    activeBuffer.position(toActiveBufferPos(position));
    checkIndex(limit - 1);
    final int limitBufferIndex = lastSegmentIndex;
    activeBuffer.limit(toActiveBufferPos(limit));
    for(int i = posBufferIndex; i <= limitBufferIndex; i++) {
      final Buffer b1 = buffers[i - posBufferIndex];
      buffers[i - posBufferIndex] = buffers[i];
      buffers[i] = b1;
    }
  }
  
  setPosLim(0, position);
  refreshBuffers();
  
  resetLastLocation();
  return this;
}

代码示例来源:origin: org.mule.glassfish.grizzly/grizzly-framework

@Override
public BuffersBuffer compact() {
  checkDispose();
  if (buffersSize == 0) {
    return this;
  } else if (buffersSize == 1) {
    final Buffer buffer = buffers[0];
    Buffers.setPositionLimit(buffer, buffer.position() + position,
        buffer.position() + limit);
    buffer.compact();
  } else {
    checkIndex(position);
    final int posBufferIndex = lastSegmentIndex;
    activeBuffer.position(toActiveBufferPos(position));
    checkIndex(limit - 1);
    final int limitBufferIndex = lastSegmentIndex;
    activeBuffer.limit(toActiveBufferPos(limit));
    for(int i = posBufferIndex; i <= limitBufferIndex; i++) {
      final Buffer b1 = buffers[i - posBufferIndex];
      buffers[i - posBufferIndex] = buffers[i];
      buffers[i] = b1;
    }
  }
  
  setPosLim(0, position);
  refreshBuffers();
  
  resetLastLocation();
  return this;
}

代码示例来源:origin: org.glassfish.grizzly/grizzly-websockets-server

@Override
public BuffersBuffer compact() {
  checkDispose();
  if (buffersSize == 0) {
    return this;
  } else if (buffersSize == 1) {
    final Buffer buffer = buffers[0];
    Buffers.setPositionLimit(buffer, buffer.position() + position,
        buffer.position() + limit);
    buffer.compact();
  } else {
    checkIndex(position);
    final int posBufferIndex = lastSegmentIndex;
    activeBuffer.position(toActiveBufferPos(position));
    checkIndex(limit - 1);
    final int limitBufferIndex = lastSegmentIndex;
    activeBuffer.limit(toActiveBufferPos(limit));
    for(int i = posBufferIndex; i <= limitBufferIndex; i++) {
      final Buffer b1 = buffers[i - posBufferIndex];
      buffers[i - posBufferIndex] = buffers[i];
      buffers[i] = b1;
    }
  }
  
  setPosLim(0, position);
  refreshBuffers();
  
  resetLastLocation();
  return this;
}

代码示例来源:origin: javaee/grizzly

@Override
public BuffersBuffer compact() {
  checkDispose();
  if (buffersSize == 0) {
    return this;
  } else if (buffersSize == 1) {
    final Buffer buffer = buffers[0];
    Buffers.setPositionLimit(buffer, buffer.position() + position,
        buffer.position() + limit);
    buffer.compact();
  } else {
    checkIndex(position);
    final int posBufferIndex = lastSegmentIndex;
    activeBuffer.position(toActiveBufferPos(position));
    checkIndex(limit - 1);
    final int limitBufferIndex = lastSegmentIndex;
    activeBuffer.limit(toActiveBufferPos(limit));
    for(int i = posBufferIndex; i <= limitBufferIndex; i++) {
      final Buffer b1 = buffers[i - posBufferIndex];
      buffers[i - posBufferIndex] = buffers[i];
      buffers[i] = b1;
    }
  }
  
  setPosLim(0, position);
  refreshBuffers();
  
  resetLastLocation();
  return this;
}

代码示例来源:origin: javaee/grizzly

@Override
public BuffersBuffer compact() {
  checkDispose();
  if (buffersSize == 0) {
    return this;
  } else if (buffersSize == 1) {
    final Buffer buffer = buffers[0];
    Buffers.setPositionLimit(buffer, buffer.position() + position,
        buffer.position() + limit);
    buffer.compact();
  } else {
    checkIndex(position);
    final int posBufferIndex = lastSegmentIndex;
    activeBuffer.position(toActiveBufferPos(position));
    checkIndex(limit - 1);
    final int limitBufferIndex = lastSegmentIndex;
    activeBuffer.limit(toActiveBufferPos(limit));
    for(int i = posBufferIndex; i <= limitBufferIndex; i++) {
      final Buffer b1 = buffers[i - posBufferIndex];
      buffers[i - posBufferIndex] = buffers[i];
      buffers[i] = b1;
    }
  }
  
  setPosLim(0, position);
  refreshBuffers();
  
  resetLastLocation();
  return this;
}

代码示例来源:origin: javaee/grizzly

@Override
public BuffersBuffer compact() {
  checkDispose();
  if (buffersSize == 0) {
    return this;
  } else if (buffersSize == 1) {
    final Buffer buffer = buffers[0];
    Buffers.setPositionLimit(buffer, buffer.position() + position,
        buffer.position() + limit);
    buffer.compact();
  } else {
    checkIndex(position);
    final int posBufferIndex = lastSegmentIndex;
    activeBuffer.position(toActiveBufferPos(position));
    checkIndex(limit - 1);
    final int limitBufferIndex = lastSegmentIndex;
    activeBuffer.limit(toActiveBufferPos(limit));
    for(int i = posBufferIndex; i <= limitBufferIndex; i++) {
      final Buffer b1 = buffers[i - posBufferIndex];
      buffers[i - posBufferIndex] = buffers[i];
      buffers[i] = b1;
    }
  }
  
  setPosLim(0, position);
  refreshBuffers();
  
  resetLastLocation();
  return this;
}

代码示例来源:origin: org.glassfish.grizzly/grizzly-http-server-core

@Override
public BuffersBuffer compact() {
  checkDispose();
  if (buffersSize == 0) {
    return this;
  } else if (buffersSize == 1) {
    final Buffer buffer = buffers[0];
    Buffers.setPositionLimit(buffer, buffer.position() + position,
        buffer.position() + limit);
    buffer.compact();
  } else {
    checkIndex(position);
    final int posBufferIndex = lastSegmentIndex;
    activeBuffer.position(toActiveBufferPos(position));
    checkIndex(limit - 1);
    final int limitBufferIndex = lastSegmentIndex;
    activeBuffer.limit(toActiveBufferPos(limit));
    for(int i = posBufferIndex; i <= limitBufferIndex; i++) {
      final Buffer b1 = buffers[i - posBufferIndex];
      buffers[i - posBufferIndex] = buffers[i];
      buffers[i] = b1;
    }
  }
  
  setPosLim(0, position);
  refreshBuffers();
  
  resetLastLocation();
  return this;
}

代码示例来源:origin: javaee/grizzly

@Override
public BuffersBuffer compact() {
  checkDispose();
  if (buffersSize == 0) {
    return this;
  } else if (buffersSize == 1) {
    final Buffer buffer = buffers[0];
    Buffers.setPositionLimit(buffer, buffer.position() + position,
        buffer.position() + limit);
    buffer.compact();
  } else {
    checkIndex(position);
    final int posBufferIndex = lastSegmentIndex;
    activeBuffer.position(toActiveBufferPos(position));
    checkIndex(limit - 1);
    final int limitBufferIndex = lastSegmentIndex;
    activeBuffer.limit(toActiveBufferPos(limit));
    for(int i = posBufferIndex; i <= limitBufferIndex; i++) {
      final Buffer b1 = buffers[i - posBufferIndex];
      buffers[i - posBufferIndex] = buffers[i];
      buffers[i] = b1;
    }
  }
  
  setPosLim(0, position);
  refreshBuffers();
  
  resetLastLocation();
  return this;
}

代码示例来源:origin: javaee/grizzly

@Override
public BuffersBuffer compact() {
  checkDispose();
  if (buffersSize == 0) {
    return this;
  } else if (buffersSize == 1) {
    final Buffer buffer = buffers[0];
    Buffers.setPositionLimit(buffer, buffer.position() + position,
        buffer.position() + limit);
    buffer.compact();
  } else {
    checkIndex(position);
    final int posBufferIndex = lastSegmentIndex;
    activeBuffer.position(toActiveBufferPos(position));
    checkIndex(limit - 1);
    final int limitBufferIndex = lastSegmentIndex;
    activeBuffer.limit(toActiveBufferPos(limit));
    for(int i = posBufferIndex; i <= limitBufferIndex; i++) {
      final Buffer b1 = buffers[i - posBufferIndex];
      buffers[i - posBufferIndex] = buffers[i];
      buffers[i] = b1;
    }
  }
  
  setPosLim(0, position);
  refreshBuffers();
  
  resetLastLocation();
  return this;
}

代码示例来源:origin: org.glassfish.grizzly/grizzly-core

@Override
public BuffersBuffer compact() {
  checkDispose();
  if (buffersSize == 0) {
    return this;
  } else if (buffersSize == 1) {
    final Buffer buffer = buffers[0];
    Buffers.setPositionLimit(buffer, buffer.position() + position,
        buffer.position() + limit);
    buffer.compact();
  } else {
    checkIndex(position);
    final int posBufferIndex = lastSegmentIndex;
    activeBuffer.position(toActiveBufferPos(position));
    checkIndex(limit - 1);
    final int limitBufferIndex = lastSegmentIndex;
    activeBuffer.limit(toActiveBufferPos(limit));
    for(int i = posBufferIndex; i <= limitBufferIndex; i++) {
      final Buffer b1 = buffers[i - posBufferIndex];
      buffers[i - posBufferIndex] = buffers[i];
      buffers[i] = b1;
    }
  }
  
  setPosLim(0, position);
  refreshBuffers();
  
  resetLastLocation();
  return this;
}

相关文章