List of usage examples for io.netty.buffer ByteBuf readBytes
public abstract ByteBuf readBytes(ByteBuffer dst);
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; }//from w ww .j a va2 s . com 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.lang.chars.Utf8Decoder.java
License:Open Source License
public static CharSequence decode(ByteBuf buffer) { byte[] dest = new byte[buffer.readableBytes()]; buffer.readBytes(dest); return Utf8Decoder.decode(dest); }
From source file:divconq.lang.chars.Utf8Decoder.java
License:Open Source License
public static CharSequence decode(ByteBuf buffer, int max) { byte[] dest = new byte[Math.min(buffer.readableBytes(), max)]; buffer.readBytes(dest); return Utf8Decoder.decode(dest); }
From source file:divconq.net.ByteToMessageDecoder.java
License:Apache License
@Override public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception { ByteBuf buf = internalBuffer(); int readable = buf.readableBytes(); if (buf.isReadable()) { ByteBuf bytes = buf.readBytes(readable); buf.release();/*from www. j a v a2 s .c om*/ ctx.fireChannelRead(bytes); } else { buf.release(); } this.cumulation = null; ctx.fireChannelReadComplete(); this.handlerRemoved0(ctx); }
From source file:divconq.net.ssl.JdkSslServerContext.java
License:Apache License
public JdkSslServerContext(File certChainFile, File keyFile, String keyPassword, Iterable<String> ciphers, Iterable<String> nextProtocols, long sessionCacheSize, long sessionTimeout) throws SSLException { super(ciphers); if (certChainFile == null) { throw new NullPointerException("certChainFile"); }/*from w w w .ja va 2s . co m*/ if (keyFile == null) { throw new NullPointerException("keyFile"); } if (keyPassword == null) { keyPassword = ""; } if (nextProtocols != null && nextProtocols.iterator().hasNext()) { throw new SSLException("NPN/ALPN unsupported: " + nextProtocols); } else { this.nextProtocols = Collections.emptyList(); } String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm"); if (algorithm == null) { algorithm = "SunX509"; } try { KeyStore ks = KeyStore.getInstance("JKS"); ks.load(null, null); CertificateFactory cf = CertificateFactory.getInstance("X.509"); KeyFactory rsaKF = KeyFactory.getInstance("RSA"); KeyFactory dsaKF = KeyFactory.getInstance("DSA"); ByteBuf encodedKeyBuf = PemReader.readPrivateKey(keyFile); byte[] encodedKey = new byte[encodedKeyBuf.readableBytes()]; encodedKeyBuf.readBytes(encodedKey).release(); char[] keyPasswordChars = keyPassword.toCharArray(); PKCS8EncodedKeySpec encodedKeySpec = generateKeySpec(keyPasswordChars, encodedKey); PrivateKey key; try { key = rsaKF.generatePrivate(encodedKeySpec); } catch (InvalidKeySpecException ignore) { key = dsaKF.generatePrivate(encodedKeySpec); } List<Certificate> certChain = new ArrayList<Certificate>(); ByteBuf[] certs = PemReader.readCertificates(certChainFile); try { for (ByteBuf buf : certs) { certChain.add(cf.generateCertificate(new ByteBufInputStream(buf))); } } finally { for (ByteBuf buf : certs) { buf.release(); } } ks.setKeyEntry("key", key, keyPasswordChars, certChain.toArray(new Certificate[certChain.size()])); // Set up key manager factory to use our key store KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm); kmf.init(ks, keyPasswordChars); // Initialize the SSLContext to work with our key managers. ctx = SSLContext.getInstance(PROTOCOL); ctx.init(kmf.getKeyManagers(), null, null); SSLSessionContext sessCtx = ctx.getServerSessionContext(); if (sessionCacheSize > 0) { sessCtx.setSessionCacheSize((int) Math.min(sessionCacheSize, Integer.MAX_VALUE)); } if (sessionTimeout > 0) { sessCtx.setSessionTimeout((int) Math.min(sessionTimeout, Integer.MAX_VALUE)); } } catch (Exception e) { throw new SSLException("failed to initialize the server-side SSL context", e); } }
From source file:dorkbox.util.storage.DefaultStorageSerializationManager.java
License:Apache License
@Override public Object read(final ByteBuf buffer, final int length) throws IOException { final Input input = new Input(); buffer.readBytes(input.getBuffer()); final Object o = readFullClassAndObject(input); buffer.skipBytes(input.position());//from ww w. ja v a 2s . c o m return o; }
From source file:eu.jangos.realm.network.encoder.RealmPacketEncoder.java
License:Open Source License
private void reverseHeader(ChannelHandlerContext ctx, ByteBuf out) { // Retaining the actual index. int index = out.writerIndex(); out.resetReaderIndex();// w w w.j ava 2 s.c om // Reading the header. byte[] header = out.readBytes(4).array(); // Resetting the reader index to send it. out.resetReaderIndex(); // Swapping the header. byte temp = header[2]; header[2] = header[3]; header[3] = temp; // Encrypting the header (if applicable). header = ctx.channel().attr(CRYPT).get().encrypt(header); // Reset the writer index. out.resetWriterIndex(); // Write the header. out.writeBytes(header); // Reset the writer index to the default one. out.writerIndex(index); }
From source file:eu.jangos.realm.network.packet.client.auth.CMSG_AUTH_SESSION.java
License:Apache License
@Override public void decode(ByteBuf buf) throws Exception { if (buf.readableBytes() < this.size) { throw new Exception(); }/* ww w. j a v a 2 s . c o m*/ buf.readShort(); this.build = buf.readInt(); this.unknown = buf.readInt(); StringBuilder b = new StringBuilder(); byte c; while ((c = buf.readByte()) != 0) { b.append((char) c); } this.account = b.toString(); this.seed = buf.readBytes(4).array(); this.digest = buf.readBytes(20).array(); int sizeAddons = buf.readInt(); byte[] compressedAddons = buf.readBytes(buf.readableBytes()).array(); Inflater decompressor = new Inflater(); decompressor.setInput(compressedAddons); ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedAddons.length); final byte[] buffer = new byte[1024]; while (!decompressor.finished()) { try { final int count = decompressor.inflate(buffer); bos.write(buffer, 0, count); } catch (final DataFormatException e) { } } try { bos.close(); } catch (final IOException e) { } final byte[] decompressedAddons = bos.toByteArray(); if (sizeAddons != decompressedAddons.length) { System.out.println("Something went wrong"); return; } ByteBuf addonBuffer = Unpooled.wrappedBuffer(decompressedAddons).order(ByteOrder.LITTLE_ENDIAN); this.listAddon = new ArrayList<>(); while (addonBuffer.isReadable()) { String name; byte unk6; int crc, unk7; b = new StringBuilder(); while ((c = addonBuffer.readByte()) != 0) { b.append((char) c); } name = b.toString(); crc = addonBuffer.readInt(); unk7 = addonBuffer.readInt(); unk6 = addonBuffer.readByte(); AddonInfo info = new AddonInfo(name, unk6, crc, unk7); this.listAddon.add(info); } }
From source file:eu.matejkormuth.rpgdavid.starving.remote.netty.handlers.ShortHeaderFrameDecoder.java
License:Open Source License
@Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf buf, final List<Object> out) throws Exception { out.add(buf.readBytes(buf.readShort())); }
From source file:eu.matejkormuth.rpgdavid.starving.remote.netty.packets.CommandPacket.java
License:Open Source License
@Override public void readFrom(ByteBuf fromBuffer) { short length = fromBuffer.readShort(); byte[] array = new byte[length]; fromBuffer.readBytes(array); this.command = new String(array, PROTOCOL_ENCODING); }