本文整理汇总了Java中java.nio.channels.GatheringByteChannel类的典型用法代码示例。如果您正苦于以下问题:Java GatheringByteChannel类的具体用法?Java GatheringByteChannel怎么用?Java GatheringByteChannel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
GatheringByteChannel类属于java.nio.channels包,在下文中一共展示了GatheringByteChannel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: divideProcessing
import java.nio.channels.GatheringByteChannel; //导入依赖的package包/类
protected void divideProcessing (int bufferSize, StringBuffer bf, ByteBuffer bb, GatheringByteChannel outChannel, String charset) throws IOException {
int bfsCnt = bf.length()/bufferSize+1;
for(int i=0;i<bfsCnt;i++) {
String sub = null;
if(i<bfsCnt-1) {
sub = bf.substring(i*bufferSize, (i+1)*bufferSize);
} else {
sub = bf.substring(i*bufferSize, i*bufferSize+(bf.length()%bufferSize));
}
if(sub != null){
bb.put(sub.getBytes(charset));
bb.flip();
outChannel.write(bb);
bb.clear();
}
}
bf.setLength(0);
}
示例2: writeTo
import java.nio.channels.GatheringByteChannel; //导入依赖的package包/类
@Override
public void writeTo(GatheringByteChannel channel) throws IOException {
// nothing to do
if (length == 0) {
return;
}
int currentLength = length;
int currentOffset = offset;
BytesRef ref = new BytesRef();
while (currentLength > 0) {
// try to align to the underlying pages while writing, so no new arrays will be created.
int fragmentSize = Math.min(currentLength, PAGE_SIZE - (currentOffset % PAGE_SIZE));
boolean newArray = bytearray.get(currentOffset, fragmentSize, ref);
assert !newArray : "PagedBytesReference failed to align with underlying bytearray. offset [" + currentOffset + "], size [" + fragmentSize + "]";
Channels.writeToChannel(ref.bytes, ref.offset, ref.length, channel);
currentLength -= ref.length;
currentOffset += ref.length;
}
assert currentLength == 0;
}
示例3: writeTo
import java.nio.channels.GatheringByteChannel; //导入依赖的package包/类
@Override
public long writeTo(GatheringByteChannel channel) throws IOException {
if (completed())
throw new KafkaException("This operation cannot be invoked on a complete request.");
int totalWrittenPerCall = 0;
boolean sendComplete;
do {
long written = current.writeTo(channel);
totalWrittenPerCall += written;
sendComplete = current.completed();
if (sendComplete)
nextSendOrDone();
} while (!completed() && sendComplete);
totalWritten += totalWrittenPerCall;
if (completed() && totalWritten != size)
log.error("mismatch in sending bytes over socket; expected: " + size + " actual: " + totalWritten);
log.trace("Bytes written as part of multi-send call: {}, total bytes written so far: {}, expected bytes to write: {}",
totalWrittenPerCall, totalWritten, size);
return totalWrittenPerCall;
}
示例4: writeTo
import java.nio.channels.GatheringByteChannel; //导入依赖的package包/类
@Override
public long writeTo(GatheringByteChannel destChannel, long offset, int length) throws IOException {
long newSize = Math.min(channel.size(), end) - start;
int oldSize = sizeInBytes();
if (newSize < oldSize)
throw new KafkaException(String.format(
"Size of FileRecords %s has been truncated during write: old size %d, new size %d",
file.getAbsolutePath(), oldSize, newSize));
long position = start + offset;
int count = Math.min(length, oldSize);
final long bytesTransferred;
if (destChannel instanceof TransportLayer) {
TransportLayer tl = (TransportLayer) destChannel;
bytesTransferred = tl.transferFrom(channel, position, count);
} else {
bytesTransferred = channel.transferTo(position, count, destChannel);
}
return bytesTransferred;
}
示例5: writeTo
import java.nio.channels.GatheringByteChannel; //导入依赖的package包/类
@Override
public long writeTo(GatheringByteChannel channel) throws IOException {
long written = 0;
if (remaining > 0) {
written = records.writeTo(channel, size() - remaining, remaining);
if (written < 0)
throw new EOFException("Wrote negative bytes to channel. This shouldn't happen.");
remaining -= written;
}
pending = TransportLayers.hasPendingWrites(channel);
if (remaining <= 0 && pending)
channel.write(EMPTY_BYTE_BUFFER);
return written;
}
示例6: writeTo
import java.nio.channels.GatheringByteChannel; //导入依赖的package包/类
@Override
public long writeTo(GatheringByteChannel channel) throws IOException {
if (completed())
throw new KafkaException("This operation cannot be completed on a complete request.");
int totalWrittenPerCall = 0;
boolean sendComplete = false;
do {
long written = current.writeTo(channel);
totalWritten += written;
totalWrittenPerCall += written;
sendComplete = current.completed();
if (sendComplete)
nextSendOrDone();
} while (!completed() && sendComplete);
if (log.isTraceEnabled())
log.trace("Bytes written as part of multisend call : " + totalWrittenPerCall + "Total bytes written so far : " + totalWritten + "Expected bytes to write : " + size);
return totalWrittenPerCall;
}
示例7: transfer
import java.nio.channels.GatheringByteChannel; //导入依赖的package包/类
protected long transfer(ByteBuf source, WritableByteChannel target, MultipartState sourceFullyWrittenState) throws IOException {
int transferred = 0;
if (target instanceof GatheringByteChannel) {
transferred = source.readBytes((GatheringByteChannel) target, (int) source.readableBytes());
} else {
for (ByteBuffer byteBuffer : source.nioBuffers()) {
int len = byteBuffer.remaining();
int written = target.write(byteBuffer);
transferred += written;
if (written != len) {
// couldn't write full buffer, exit loop
break;
}
}
// assume this is a basic single ByteBuf
source.readerIndex(source.readerIndex() + transferred);
}
if (source.isReadable()) {
slowTarget = true;
} else {
state = sourceFullyWrittenState;
}
return transferred;
}
示例8: getBytes
import java.nio.channels.GatheringByteChannel; //导入依赖的package包/类
private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException {
checkIndex(index, length);
if (length == 0) {
return 0;
}
ByteBuffer tmpBuf;
if (internal) {
tmpBuf = internalNioBuffer();
} else {
tmpBuf = memory.duplicate();
}
index = idx(index);
tmpBuf.clear().position(index).limit(index + length);
return out.write(tmpBuf);
}
示例9: gather
import java.nio.channels.GatheringByteChannel; //导入依赖的package包/类
public static void gather() {
ByteBuffer header = ByteBuffer.allocate(14);
ByteBuffer body = ByteBuffer.allocate(100);
header.put(headers.getBytes());
body.put(bodys.getBytes());
GatheringByteChannel channel = getChannel();
try {
header.flip();
body.flip();
channel.write(new ByteBuffer[] { header, body });
} catch (IOException e) {
e.printStackTrace();
}
}
示例10: sendBuffer
import java.nio.channels.GatheringByteChannel; //导入依赖的package包/类
private void sendBuffer (ByteBuffer data,
GatheringByteChannel channel, String contentType)
throws Exception
{
ByteBuffer [] buffers = { staticHdr, dynHdr, data };
staticHdr.rewind();
cbtemp.clear();
cbtemp.put ("Content-Length: " + data.limit());
cbtemp.put (LINE_SEP);
cbtemp.put ("Content-Type: ");
cbtemp.put (contentType);
cbtemp.put (LINE_SEP);
cbtemp.put (LINE_SEP);
cbtemp.flip();
buffers [1] = utf8.encode (cbtemp);
while (channel.write (buffers) != 0) {
// nothing
}
}
示例11: main
import java.nio.channels.GatheringByteChannel; //导入依赖的package包/类
public static void main (String [] argv)
throws Exception
{
int reps = 10;
if (argv.length > 0) {
reps = Integer.parseInt (argv [0]);
}
FileOutputStream fos = new FileOutputStream (DEMOGRAPHIC);
GatheringByteChannel gatherChannel = fos.getChannel();
// generate some brilliant marcom, er, repurposed content
ByteBuffer [] bs = utterBS (reps);
// deliver the message to the waiting market
while (gatherChannel.write (bs) > 0) {
// empty body
// loop until write() returns zero
}
System.out.println ("Mindshare paradigms synergized to "
+ DEMOGRAPHIC);
fos.close();
}
示例12: writeTo
import java.nio.channels.GatheringByteChannel; //导入依赖的package包/类
/**
* Write some of this set to the given channel.
*
* @param destChannel The channel to write to.
* @param writePosition The position in the message set to begin writing from.
* @param size The maximum number of bytes to write
* @return The number of bytes actually written.
*/
@Override
public int writeTo(final GatheringByteChannel destChannel, final long writePosition, final int size) throws IOException {
// Ensure that the underlying size has not changed.
int newSize = Math.min((int) channel.size(), end) - start;
if (newSize < _size.get()) {
throw new SamsaException(String.format(
"Size of FileMessageSet %s has been truncated during write: old size %d, new size %d",
file.getAbsolutePath(), _size.get(), newSize));
}
int bytesTransferred = (int) channel.transferTo(start + writePosition, Math.min(size, sizeInBytes()), destChannel);
if (LOG.isTraceEnabled()) {
LOG.trace("FileMessageSet {} : bytes transferred : {} bytes requested for transfer : {}",
file.getAbsolutePath(), bytesTransferred, Math.min(size, sizeInBytes()));
}
return bytesTransferred;
}
示例13: newSink
import java.nio.channels.GatheringByteChannel; //导入依赖的package包/类
private static Sink newSink(Object out) {
if (out instanceof OutputStream) {
return new OioSink((OutputStream) out);
}
if (out instanceof GatheringByteChannel) {
return new NioSink((GatheringByteChannel) out);
}
throw new FastdfsException("unknown sink output type " + out.getClass().getName());
}
示例14: getBox
import java.nio.channels.GatheringByteChannel; //导入依赖的package包/类
public void getBox(WritableByteChannel writableByteChannel) throws IOException {
ByteBuffer bb = ByteBuffer.allocate(16);
long size = getSize();
if (isSmallBox(size)) {
IsoTypeWriter.writeUInt32(bb, size);
} else {
IsoTypeWriter.writeUInt32(bb, 1);
}
bb.put(IsoFile.fourCCtoBytes("mdat"));
if (isSmallBox(size)) {
bb.put(new byte[8]);
} else {
IsoTypeWriter.writeUInt64(bb, size);
}
bb.rewind();
writableByteChannel.write(bb);
if (writableByteChannel instanceof GatheringByteChannel) {
List<ByteBuffer> nuSamples = unifyAdjacentBuffers(samples);
for (int i = 0; i < Math.ceil((double) nuSamples.size() / STEPSIZE); i++) {
List<ByteBuffer> sublist = nuSamples.subList(
i * STEPSIZE, // start
(i + 1) * STEPSIZE < nuSamples.size() ? (i + 1) * STEPSIZE : nuSamples.size()); // end
ByteBuffer sampleArray[] = sublist.toArray(new ByteBuffer[sublist.size()]);
do {
((GatheringByteChannel) writableByteChannel).write(sampleArray);
} while (sampleArray[sampleArray.length - 1].remaining() > 0);
}
//System.err.println(bytesWritten);
} else {
for (ByteBuffer sample : samples) {
sample.rewind();
writableByteChannel.write(sample);
}
}
}
示例15: writeToChannel
import java.nio.channels.GatheringByteChannel; //导入依赖的package包/类
/**
* Copies bytes from source {@link org.jboss.netty.buffer.ChannelBuffer} to a {@link java.nio.channels.GatheringByteChannel}
*
* @param source ChannelBuffer to copy from
* @param sourceIndex index in <i>source</i> to start copying from
* @param length how many bytes to copy
* @param channel target GatheringByteChannel
*/
public static void writeToChannel(ChannelBuffer source, int sourceIndex, int length, GatheringByteChannel channel) throws IOException {
while (length > 0) {
int written = source.getBytes(sourceIndex, channel, length);
sourceIndex += written;
length -= written;
}
assert length == 0;
}