List of usage examples for io.netty.buffer ByteBuf writableBytes
public abstract int writableBytes();
From source file:de.unipassau.isl.evs.ssh.core.network.handler.SignerTest.java
License:Open Source License
public void testModify() { ByteBuf buf = channel.alloc().buffer(); buf.capacity(1000);//from w w w . ja v a 2s. c o m while (buf.writableBytes() > 0) { buf.writeByte(buf.writableBytes()); } try { channel.writeOutbound(buf.duplicate().retain()); for (ByteBuf msg; (msg = channel.readOutbound()) != null;) { msg.setByte(500, ~msg.getByte(500)); channel.writeInbound(msg); } channel.checkException(); if (false) throw new SignatureException(); fail("Missing exception"); } catch (SignatureException e) { } if (channel.isOpen()) { fail("Channel not closed"); } }
From source file:divconq.ctp.stream.GzipStream.java
License:Open Source License
protected void deflate(ByteBuf out) { int numBytes = 0; do {/*from w w w. j av a 2s . c o m*/ byte[] o = out.array(); numBytes = this.deflater.deflate(o, out.arrayOffset() + out.writerIndex(), out.writableBytes(), Deflater.SYNC_FLUSH); out.writerIndex(out.writerIndex() + numBytes); } while (numBytes > 0); }
From source file:divconq.ctp.stream.UngzipStream.java
License:Open Source License
protected void inflate(ByteBuf in) { switch (this.gzipState) { case HEADER_START: if (in.readableBytes() < 10) return; // read magic numbers int magic0 = in.readByte(); int magic1 = in.readByte(); if (magic0 != 31) { OperationContext.get().getTaskRun().kill("Input is not in the GZIP format"); return; }/* w ww . j a va 2 s . c o m*/ this.crc.update(magic0); this.crc.update(magic1); int method = in.readUnsignedByte(); if (method != Deflater.DEFLATED) { OperationContext.get().getTaskRun() .kill("Unsupported compression method " + method + " in the GZIP header"); return; } this.crc.update(method); this.flags = in.readUnsignedByte(); this.crc.update(this.flags); if ((this.flags & FRESERVED) != 0) { OperationContext.get().getTaskRun().kill("Reserved flags are set in the GZIP header"); return; } // mtime (int) this.crc.update(in.readByte()); this.crc.update(in.readByte()); this.crc.update(in.readByte()); this.crc.update(in.readByte()); this.crc.update(in.readUnsignedByte()); // extra flags this.crc.update(in.readUnsignedByte()); // operating system this.gzipState = GzipState.FLG_READ; case FLG_READ: if ((this.flags & FEXTRA) != 0) { if (in.readableBytes() < 2) return; int xlen1 = in.readUnsignedByte(); int xlen2 = in.readUnsignedByte(); this.crc.update(xlen1); this.crc.update(xlen2); this.xlen |= xlen1 << 8 | xlen2; } this.gzipState = GzipState.XLEN_READ; case XLEN_READ: if (this.xlen != -1) { if (in.readableBytes() < xlen) return; byte[] xtra = new byte[xlen]; in.readBytes(xtra); this.crc.update(xtra); } this.gzipState = GzipState.SKIP_FNAME; case SKIP_FNAME: if ((this.flags & FNAME) != 0) { boolean gotend = false; while (in.isReadable()) { int b = in.readUnsignedByte(); this.crc.update(b); if (b == 0x00) { gotend = true; break; } } if (!gotend) return; } this.gzipState = GzipState.SKIP_COMMENT; case SKIP_COMMENT: if ((this.flags & FCOMMENT) != 0) { boolean gotend = false; while (in.isReadable()) { int b = in.readUnsignedByte(); this.crc.update(b); if (b == 0x00) { gotend = true; break; } } if (!gotend) return; } this.gzipState = GzipState.PROCESS_FHCRC; case PROCESS_FHCRC: if ((this.flags & FHCRC) != 0) { if (in.readableBytes() < 4) return; long crcValue = 0; for (int i = 0; i < 4; ++i) crcValue |= (long) in.readUnsignedByte() << i * 8; long readCrc = crc.getValue(); if (crcValue != readCrc) { OperationContext.get().getTaskRun() .kill("CRC value missmatch. Expected: " + crcValue + ", Got: " + readCrc); return; } } this.crc.reset(); this.gzipState = GzipState.PRROCESS_CONTENT; case PRROCESS_CONTENT: int readableBytes = in.readableBytes(); if (readableBytes < 1) return; if (in.hasArray()) { this.inflater.setInput(in.array(), in.arrayOffset() + in.readerIndex(), readableBytes); } else { byte[] array = new byte[readableBytes]; in.getBytes(in.readerIndex(), array); this.inflater.setInput(array); } int maxOutputLength = this.inflater.getRemaining() << 1; ByteBuf decompressed = Hub.instance.getBufferAllocator().heapBuffer(maxOutputLength); boolean readFooter = false; byte[] outArray = decompressed.array(); try { while (!this.inflater.needsInput()) { int writerIndex = decompressed.writerIndex(); int outIndex = decompressed.arrayOffset() + writerIndex; int length = decompressed.writableBytes(); if (length == 0) { // completely filled the buffer allocate a new one and start to fill it this.outlist.add(decompressed); decompressed = Hub.instance.getBufferAllocator().heapBuffer(maxOutputLength); outArray = decompressed.array(); continue; } int outputLength = this.inflater.inflate(outArray, outIndex, length); if (outputLength > 0) { decompressed.writerIndex(writerIndex + outputLength); this.crc.update(outArray, outIndex, outputLength); } else { if (this.inflater.needsDictionary()) { if (this.dictionary == null) { OperationContext.get().getTaskRun().kill( "decompression failure, unable to set dictionary as non was specified"); return; } this.inflater.setDictionary(this.dictionary); } } if (this.inflater.finished()) { readFooter = true; break; } } in.skipBytes(readableBytes - this.inflater.getRemaining()); } catch (DataFormatException x) { OperationContext.get().getTaskRun().kill("decompression failure: " + x); return; } finally { if (decompressed.isReadable()) { this.outlist.add(decompressed); } else { decompressed.release(); } } if (!readFooter) break; this.gzipState = GzipState.PROCESS_FOOTER; case PROCESS_FOOTER: if (in.readableBytes() < 8) return; long crcValue = 0; for (int i = 0; i < 4; ++i) crcValue |= (long) in.readUnsignedByte() << i * 8; long readCrc = this.crc.getValue(); if (crcValue != readCrc) { OperationContext.get().getTaskRun() .kill("CRC value missmatch. Expected: " + crcValue + ", Got: " + readCrc); return; } // read ISIZE and verify int dataLength = 0; for (int i = 0; i < 4; ++i) dataLength |= in.readUnsignedByte() << i * 8; int readLength = this.inflater.getTotalOut(); if (dataLength != readLength) { OperationContext.get().getTaskRun() .kill("Number of bytes mismatch. Expected: " + dataLength + ", Got: " + readLength); return; } this.gzipState = GzipState.DONE; case DONE: break; } }
From source file:divconq.net.ssl.SslHandler.java
License:Apache License
private SSLEngineResult wrap(SSLEngine engine, ByteBuf in, ByteBuf out) throws SSLException { ByteBuffer in0 = in.nioBuffer(); if (!in0.isDirect()) { ByteBuffer newIn0 = ByteBuffer.allocateDirect(in0.remaining()); newIn0.put(in0).flip();//from w w w .j a v a2s. c om in0 = newIn0; } for (;;) { ByteBuffer out0 = out.nioBuffer(out.writerIndex(), out.writableBytes()); SSLEngineResult result = engine.wrap(in0, out0); in.skipBytes(result.bytesConsumed()); out.writerIndex(out.writerIndex() + result.bytesProduced()); switch (result.getStatus()) { case BUFFER_OVERFLOW: out.ensureWritable(maxPacketBufferSize); break; default: return result; } } }
From source file:divconq.net.ssl.SslHandler.java
License:Apache License
private static SSLEngineResult unwrap(SSLEngine engine, ByteBuffer in, ByteBuf out) throws SSLException { int overflows = 0; for (;;) {/*from ww w.j a v a 2s . c o m*/ ByteBuffer out0 = out.nioBuffer(out.writerIndex(), out.writableBytes()); SSLEngineResult result = engine.unwrap(in, out0); out.writerIndex(out.writerIndex() + result.bytesProduced()); switch (result.getStatus()) { case BUFFER_OVERFLOW: int max = engine.getSession().getApplicationBufferSize(); switch (overflows++) { case 0: out.ensureWritable(Math.min(max, in.remaining())); break; default: out.ensureWritable(max); } break; default: return result; } } }
From source file:dorkbox.network.pipeline.ByteBufOutput.java
License:Apache License
/** Writes the length and string, or null. Short strings are checked and if ASCII they are written more efficiently, else they * are written as UTF8. If a string is known to be ASCII, {@link #writeAscii(String)} may be used. The string can be read using * {@link ByteBufInput#readString()} or {@link ByteBufInput#readStringBuilder()}. * @param value May be null. *//*from ww w .j a v a 2s . c om*/ @Override public void writeString(String value) throws KryoException { if (value == null) { writeByte(0x80); // 0 means null, bit 8 means UTF8. return; } int charCount = value.length(); if (charCount == 0) { writeByte(1 | 0x80); // 1 means empty string, bit 8 means UTF8. return; } // Detect ASCII. boolean ascii = false; if (charCount > 1 && charCount < 64) { // only snoop 64 chars in ascii = true; for (int i = 0; i < charCount; i++) { int c = value.charAt(i); if (c > 127) { ascii = false; break; } } } ByteBuf buffer = byteBuf; if (buffer.writableBytes() < charCount) { buffer.capacity(buffer.capacity() + charCount + 1); } if (!ascii) { writeUtf8Length(charCount + 1); } int charIndex = 0; // Try to write 8 bit chars. for (; charIndex < charCount; charIndex++) { int c = value.charAt(charIndex); if (c > 127) { break; // whoops! detect ascii. have to continue with a slower method! } buffer.writeByte((byte) c); } if (charIndex < charCount) { writeString_slow(value, charCount, charIndex); } else if (ascii) { // specify it's ASCII int i = buffer.writerIndex() - 1; buffer.setByte(i, buffer.getByte(i) | 0x80); // Bit 8 means end of ASCII. } }
From source file:dorkbox.network.pipeline.ByteBufOutput.java
License:Apache License
/** Writes the length and CharSequence as UTF8, or null. The string can be read using {@link ByteBufInput#readString()} or * {@link ByteBufInput#readStringBuilder()}. * @param value May be null. *///from w w w . j a v a 2 s . co m @Override public void writeString(CharSequence value) throws KryoException { if (value == null) { writeByte(0x80); // 0 means null, bit 8 means UTF8. return; } int charCount = value.length(); if (charCount == 0) { writeByte(1 | 0x80); // 1 means empty string, bit 8 means UTF8. return; } writeUtf8Length(charCount + 1); ByteBuf buffer = byteBuf; if (buffer.writableBytes() < charCount) { buffer.capacity(buffer.capacity() + charCount + 1); } int charIndex = 0; // Try to write 8 bit chars. for (; charIndex < charCount; charIndex++) { int c = value.charAt(charIndex); if (c > 127) { break; // whoops! have to continue with a slower method! } buffer.writeByte((byte) c); } if (charIndex < charCount) { writeString_slow(value, charCount, charIndex); } }
From source file:dorkbox.network.pipeline.ByteBufOutput.java
License:Apache License
/** Writes a string that is known to contain only ASCII characters. Non-ASCII strings passed to this method will be corrupted. * Each byte is a 7 bit character with the remaining byte denoting if another character is available. This is slightly more * efficient than {@link #writeString(String)}. The string can be read using {@link ByteBufInput#readString()} or * {@link ByteBufInput#readStringBuilder()}. * @param value May be null. *//*from w ww.j av a2 s . com*/ @Override public void writeAscii(String value) throws KryoException { if (value == null) { writeByte(0x80); // 0 means null, bit 8 means UTF8. return; } int charCount = value.length(); if (charCount == 0) { writeByte(1 | 0x80); // 1 means empty string, bit 8 means UTF8. return; } ByteBuf buffer = byteBuf; if (buffer.writableBytes() < charCount) { buffer.capacity(buffer.capacity() + charCount + 1); } int charIndex = 0; // Try to write 8 bit chars. for (; charIndex < charCount; charIndex++) { int c = value.charAt(charIndex); buffer.writeByte((byte) c); } // specify it's ASCII int i = buffer.writerIndex() - 1; buffer.setByte(i, buffer.getByte(i) | 0x80); // Bit 8 means end of ASCII. }
From source file:io.gatling.http.client.body.multipart.impl.FilePartImpl.java
License:Apache License
@Override protected void copyContentInto(ByteBuf target) throws IOException { // can return -1 if file is empty or FileChannel was closed int transferred = target.writeBytes(getChannel(), target.writableBytes()); if (transferred > 0) { position += transferred;/*w ww. ja va 2s. co m*/ } if (position == file.length() || transferred < 0) { state = PartImplState.POST_CONTENT; if (channel.isOpen()) { channel.close(); } } }
From source file:io.gatling.http.client.body.multipart.impl.PartImpl.java
License:Apache License
void copyInto(ByteBuf source, ByteBuf target, PartImplState sourceFullyWrittenState) { int sourceRemaining = source.readableBytes(); int targetRemaining = target.writableBytes(); if (sourceRemaining <= targetRemaining) { target.writeBytes(source);/* w w w . j av a 2s . com*/ state = sourceFullyWrittenState; } else { target.writeBytes(source, targetRemaining); } }