本文整理汇总了Java中java.nio.channels.GatheringByteChannel.write方法的典型用法代码示例。如果您正苦于以下问题:Java GatheringByteChannel.write方法的具体用法?Java GatheringByteChannel.write怎么用?Java GatheringByteChannel.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.GatheringByteChannel
的用法示例。
在下文中一共展示了GatheringByteChannel.write方法的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 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;
}
示例3: 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);
}
示例4: 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();
}
}
示例5: 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
}
}
示例6: 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();
}
示例7: writeTo
import java.nio.channels.GatheringByteChannel; //导入方法依赖的package包/类
@Override
public long writeTo(GatheringByteChannel channel) throws IOException {
long written = channel.write(buffers);
if (written < 0)
throw new EOFException("Wrote negative bytes to channel. This shouldn't happen.");
remaining -= written;
pending = TransportLayers.hasPendingWrites(channel);
return written;
}
示例8: writeTo
import java.nio.channels.GatheringByteChannel; //导入方法依赖的package包/类
@Override
public long writeTo(GatheringByteChannel channel, long position, int length) throws IOException {
if (position > Integer.MAX_VALUE)
throw new IllegalArgumentException("position should not be greater than Integer.MAX_VALUE: " + position);
if (position + length > buffer.limit())
throw new IllegalArgumentException("position+length should not be greater than buffer.limit(), position: "
+ position + ", length: " + length + ", buffer.limit(): " + buffer.limit());
int pos = (int) position;
ByteBuffer dup = buffer.duplicate();
dup.position(pos);
dup.limit(pos + length);
return channel.write(dup);
}
示例9: writeFullyTo
import java.nio.channels.GatheringByteChannel; //导入方法依赖的package包/类
/**
* Write all records to the given channel (including partial records).
* @param channel The channel to write to
* @return The number of bytes written
* @throws IOException For any IO errors writing to the channel
*/
public int writeFullyTo(GatheringByteChannel channel) throws IOException {
buffer.mark();
int written = 0;
while (written < sizeInBytes())
written += channel.write(buffer);
buffer.reset();
return written;
}
示例10: getBytes
import java.nio.channels.GatheringByteChannel; //导入方法依赖的package包/类
public int getBytes(int index, GatheringByteChannel out, int length)
throws IOException {
if (useGathering()) {
return (int) out.write(toByteBuffers(index, length));
}
// XXX Gathering write is not supported because of a known issue.
// See http://bugs.sun.com/view_bug.do?bug_id=6210541
// This issue appeared in 2004 and is still unresolved!?
return out.write(toByteBuffer(index, length));
}
示例11: writeTo
import java.nio.channels.GatheringByteChannel; //导入方法依赖的package包/类
@Override
public long writeTo(GatheringByteChannel channel) throws IOException {
long written = channel.write(buffers);
if (written < 0)
throw new EOFException("Wrote negative bytes to channel. This shouldn't happen.");
remaining -= written;
// This is temporary workaround. As Send , Receive interfaces are being used by BlockingChannel.
// Once BlockingChannel is removed we can make Send, Receive to work with transportLayer rather than
// GatheringByteChannel or ScatteringByteChannel.
if (channel instanceof TransportLayer)
pending = ((TransportLayer) channel).hasPendingWrites();
return written;
}
示例12: getBytes
import java.nio.channels.GatheringByteChannel; //导入方法依赖的package包/类
@Override
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
// adapted from UnpooledDirectByteBuf:
checkIndex(index, length);
if (length == 0) {
return 0;
}
ByteBuffer tmpBuf = memorySegment.wrap(index, length);
return out.write(tmpBuf);
}
示例13: gatherWrite
import java.nio.channels.GatheringByteChannel; //导入方法依赖的package包/类
public static void gatherWrite(ByteBuffer[] byteBuffers,String file) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
GatheringByteChannel gatherChannel = fos.getChannel();
while(gatherChannel.write(byteBuffers)>0){
}
System.out.println("全部写入文件:"+file);
gatherChannel.close();
fos.close();
}
示例14: drainTo
import java.nio.channels.GatheringByteChannel; //导入方法依赖的package包/类
/**
* Drain pending buffers one at a time into the socket
* @param channel
* @return
* @throws IOException
*/
@Override
int drainTo (final GatheringByteChannel channel) throws IOException {
int bytesWritten = 0;
long rc = 0;
do {
/*
* Nothing to write
*/
if (m_currentWriteBuffer == null && m_queuedBuffers.isEmpty()) {
break;
}
ByteBuffer buffer = null;
if (m_currentWriteBuffer == null) {
m_currentWriteBuffer = m_queuedBuffers.poll();
buffer = m_currentWriteBuffer.b();
buffer.flip();
} else {
buffer = m_currentWriteBuffer.b();
}
rc = channel.write(buffer);
//Discard the buffer back to a pool if no data remains
if (!buffer.hasRemaining()) {
m_currentWriteBuffer.discard();
m_currentWriteBuffer = null;
m_messagesWritten++;
}
bytesWritten += rc;
} while (rc > 0);
m_bytesWritten += bytesWritten;
return bytesWritten;
}
示例15: getBytes
import java.nio.channels.GatheringByteChannel; //导入方法依赖的package包/类
private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException {
ensureAccessible();
if (length == 0) {
return 0;
}
ByteBuffer tmpBuf;
if (internal) {
tmpBuf = internalNioBuffer();
} else {
tmpBuf = buffer.duplicate();
}
tmpBuf.clear().position(index).limit(index + length);
return out.write(tmpBuf);
}