本文整理汇总了Java中java.nio.channels.WritableByteChannel.write方法的典型用法代码示例。如果您正苦于以下问题:Java WritableByteChannel.write方法的具体用法?Java WritableByteChannel.write怎么用?Java WritableByteChannel.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.WritableByteChannel
的用法示例。
在下文中一共展示了WritableByteChannel.write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: fastCopy
import java.nio.channels.WritableByteChannel; //导入方法依赖的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);
}
}
示例2: getBox
import java.nio.channels.WritableByteChannel; //导入方法依赖的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);
}
示例3: transferTo
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
@Override
public long transferTo(WritableByteChannel target, long position) throws IOException {
if (this.byteBufferHeader.hasRemaining()) {
transfered += target.write(this.byteBufferHeader);
return transfered;
} else {
List<ByteBuffer> messageBufferList = this.getMessageResult.getMessageBufferList();
for (ByteBuffer bb : messageBufferList) {
if (bb.hasRemaining()) {
transfered += target.write(bb);
return transfered;
}
}
}
return 0;
}
示例4: write
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void write(WritableByteChannel channel) throws IOException {
logger.config(getLoggingFilename() + ":Writing tag to channel");
byte[] bodyByteBuffer = writeFramesToBuffer().toByteArray();
logger.config(getLoggingFilename() + ":bodybytebuffer:sizebeforeunsynchronisation:" + bodyByteBuffer.length);
// Unsynchronize if option enabled and unsync required
unsynchronization = TagOptionSingleton.getInstance().isUnsyncTags() && ID3Unsynchronization.requiresUnsynchronization(bodyByteBuffer);
if (isUnsynchronization()) {
bodyByteBuffer = ID3Unsynchronization.unsynchronize(bodyByteBuffer);
logger.config(getLoggingFilename() + ":bodybytebuffer:sizeafterunsynchronisation:" + bodyByteBuffer.length);
}
ByteBuffer headerBuffer = writeHeaderToBuffer(0, bodyByteBuffer.length);
channel.write(headerBuffer);
channel.write(ByteBuffer.wrap(bodyByteBuffer));
}
示例5: transferTo
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
@Override
public long transferTo(WritableByteChannel target, long position) throws IOException {
if (this.byteBufferHeader.hasRemaining()) {
transferred += target.write(this.byteBufferHeader);
return transferred;
} else {
List<ByteBuffer> messageBufferList = this.queryMessageResult.getMessageBufferList();
for (ByteBuffer bb : messageBufferList) {
if (bb.hasRemaining()) {
transferred += target.write(bb);
return transferred;
}
}
}
return 0;
}
示例6: transferTo
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
@Override
public long transferTo(WritableByteChannel target, long position) throws IOException {
if (this.byteBufferHeader.hasRemaining()) {
transfered += target.write(this.byteBufferHeader);
return transfered;
}
else {
List<ByteBuffer> messageBufferList = this.getMessageResult.getMessageBufferList();
for (ByteBuffer bb : messageBufferList) {
if (bb.hasRemaining()) {
transfered += target.write(bb);
return transfered;
}
}
}
return 0;
}
示例7: writeNioBuffer
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
private int writeNioBuffer(
WritableByteChannel writeCh,
ByteBuffer buf) throws IOException {
int originalLimit = buf.limit();
int ret = 0;
try {
int ioSize = Math.min(buf.remaining(), NIO_BUFFER_LIMIT);
buf.limit(buf.position() + ioSize);
ret = writeCh.write(buf);
} finally {
buf.limit(originalLimit);
}
return ret;
}
示例8: send
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
@Override
public int send(WritableByteChannel channel, int sendCurrentLength) throws IOException {
synchronized (writeBuffer) {
int navail=(int)(nWritten-nRead);
int l=Math.min(navail, readBuffer.capacity()-readBuffer.position());
l=Math.min(sendCurrentLength, l);
readBuffer.limit(readBuffer.position()+l);
int n=channel.write(readBuffer);
if(n>0)
{
nRead+=n;
if(navail==readBuffer.capacity())
{
// If buffer was full before but no longer then notify possibly waiting writers.
writeBuffer.notifyAll();
}
}
if(readBuffer.position()==readBuffer.capacity())
{
readBuffer.position(0);
}
return n;
}
}
示例9: getBox
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
public void getBox(WritableByteChannel os) throws IOException {
for (Box replacer : replacers) {
replacer.getBox(os);
}
ByteBuffer header = ByteBuffer.allocate(8);
IsoTypeWriter.writeUInt32(header, 8 + data.limit());
header.put(TYPE.getBytes());
header.rewind();
os.write(header);
data.rewind();
os.write(data);
}
示例10: writeFully
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
public static void writeFully(final WritableByteChannel channel, final ByteBuffer buf)
throws IOException {
do {
int written = channel.write(buf);
if (written < 0) {
throw new EOFException();
}
} while (buf.hasRemaining());
}
示例11: channelIO
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
/**
* Helper for {@link #channelRead(ReadableByteChannel, ByteBuffer)}
* and {@link #channelWrite(WritableByteChannel, ByteBuffer)}. Only
* one of readCh or writeCh should be non-null.
*
* @see #channelRead(ReadableByteChannel, ByteBuffer)
* @see #channelWrite(WritableByteChannel, ByteBuffer)
*/
private static int channelIO(ReadableByteChannel readCh,
WritableByteChannel writeCh,
ByteBuffer buf) throws IOException {
int originalLimit = buf.limit();
int initialRemaining = buf.remaining();
int ret = 0;
while (buf.remaining() > 0) {
try {
int ioSize = Math.min(buf.remaining(), NIO_BUFFER_LIMIT);
buf.limit(buf.position() + ioSize);
ret = (readCh == null) ? writeCh.write(buf) : readCh.read(buf);
if (ret < ioSize) {
break;
}
} finally {
buf.limit(originalLimit);
}
}
int nBytes = initialRemaining - buf.remaining();
return (nBytes > 0) ? nBytes : ret;
}
示例12: writePadding
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
/**
* Write paddings byte to the channel
*
* @param channel
* @param padding
* @throws IOException
*/
protected void writePadding(WritableByteChannel channel, int padding) throws IOException
{
if (padding > 0)
{
channel.write(ByteBuffer.wrap(new byte[padding]));
}
}
示例13: transferTo
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
@Override
public long transferTo(WritableByteChannel target, long position) throws IOException {
if (this.byteBufferHeader.hasRemaining()) {
transferred += target.write(this.byteBufferHeader);
return transferred;
} else if (this.selectMappedBufferResult.getByteBuffer().hasRemaining()) {
transferred += target.write(this.selectMappedBufferResult.getByteBuffer());
return transferred;
}
return 0;
}
示例14: transferToArbitraryChannel
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
private long transferToArbitraryChannel(long position, int icount,
WritableByteChannel target)
throws IOException
{
// Untrusted target: Use a newly-erased buffer
int c = Math.min(icount, TRANSFER_SIZE);
ByteBuffer bb = ByteBuffer.allocate(c);
long tw = 0; // Total bytes written
long pos = position;
try {
while (tw < icount) {
bb.limit(Math.min((int)(icount - tw), TRANSFER_SIZE));
int nr = read(bb, pos);
if (nr <= 0)
break;
bb.flip();
// ## Bug: Will block writing target if this channel
// ## is asynchronously closed
int nw = target.write(bb);
tw += nw;
if (nw != nr)
break;
pos += nw;
bb.clear();
}
return tw;
} catch (IOException x) {
if (tw > 0)
return tw;
throw x;
}
}
示例15: transferTo
import java.nio.channels.WritableByteChannel; //导入方法依赖的package包/类
@Override
public long transferTo(WritableByteChannel target, long position) throws IOException {
if (this.byteBufferHeader.hasRemaining()) {
transfered += target.write(this.byteBufferHeader);
return transfered;
}
else if (this.selectMapedBufferResult.getByteBuffer().hasRemaining()) {
transfered += target.write(this.selectMapedBufferResult.getByteBuffer());
return transfered;
}
return 0;
}