static byte[] extractByteArray(ByteBuffer buf) { int len = buf.getInt(); if (len > 0) { byte[] a = new byte[len]; buf.get(a, 0, len); return a; } else return new byte[0]; }
private void recv(SocketChannel sc, ClientInfo ci) throws IOException { ci.channel.read(ci.inBuf); ByteBuffer tmpBuf = ci.inBuf.duplicate(); tmpBuf.flip(); int bytesProcessed = 0; boolean doneLoop = false; while (!doneLoop) { byte b; try { b = tmpBuf.get(); } catch (BufferUnderflowException bue) { // Processed all data in buffer ci.inBuf.clear(); doneLoop = true; break; } switch (b) { case TypeServerConstants.WELCOME: bytesProcessed++; break; case TypeServerConstants.GET_STRING_REQUEST: bytesProcessed++; if (ci.outputPending) { // Client is backed up. We can't append to // the byte buffer because it's in the wrong // state. We could allocate another buffer // here and change our send method to know // about multiple buffers, but we'll just // assume that the client is dead break; } ci.outBuf.put(TypeServerConstants.GET_STRING_RESPONSE); ByteBuffer strBuf = encoder.encode(testString); ci.outBuf.putShort((short) strBuf.remaining()); ci.outBuf.put(strBuf); ci.outBuf.flip(); send(sc, ci); break; case TypeServerConstants.GET_STRING_RESPONSE: int startPos = tmpBuf.position(); try { int nBytes = tmpBuf.getInt(); byte[] buf = new byte[nBytes]; tmpBuf.get(buf); bytesProcessed += buf.length + 5; String s = new String(buf); // Send the string to the GUI break; } catch (BufferUnderflowException bue) { // Processed all available data ci.inBuf.position(ci.inBuf.position() + bytesProcessed); doneLoop = true; } break; } } }
static byte[] extractByteString(ByteBuffer buf) { int len = buf.getInt(); byte[] str = null; if (len > 0) { str = new byte[len]; buf.get(str, 0, len); buf.get(); // trailing null } else str = new byte[0]; return str; }
static String extractString(ByteBuffer buf) throws java.io.UnsupportedEncodingException { int len = buf.getInt(); byte[] str = null; if (len > 0) { str = new byte[len - 1]; ; buf.get(str, 0, len - 1); buf.get(); // trailing null } else str = new byte[0]; return new String(str, "UTF-8"); }
private synchronized DBMessage go(DBMessage msg, ByteDecoder decoder) throws IOException { if (_sock == null) _open(); { ByteBuffer out = msg.prepare(); while (out.remaining() > 0) _sock.write(out); } if (_pool != null) _pool._everWorked = true; if (decoder == null) return null; ByteBuffer response = decoder._buf; if (response.position() != 0) throw new IllegalArgumentException(); int read = 0; while (read < DBMessage.HEADER_LENGTH) read += _read(response); int len = response.getInt(0); if (len <= DBMessage.HEADER_LENGTH) throw new IllegalArgumentException("db sent invalid length: " + len); if (len > response.capacity()) throw new IllegalArgumentException( "db message size is too big (" + len + ") " + "max is (" + response.capacity() + ")"); response.limit(len); while (read < len) read += _read(response); if (read != len) throw new RuntimeException("something is wrong"); response.flip(); return new DBMessage(response); }
/** * Returns the offset from the file start where the latest block should be written (end of prev * block). */ private int getRingCursor(ByteBuffer buffer) { int c = buffer.getInt(4); checkState(c >= FILE_PROLOGUE_BYTES, "Integer overflow"); return c; }
void read(ByteBuffer buf) throws IOException { int magic = buf.getInt(); if (magic != MAGIC) { throw new IOException( "Incorrect magic number 0x" + Integer.toHexString(magic) + " (expected " + MAGIC + ")"); } size = buf.getInt(); flags = buf.getInt(); height = buf.getInt(); width = buf.getInt(); pitchOrLinearSize = buf.getInt(); backBufferCountOrDepth = buf.getInt(); mipMapCountOrAux = buf.getInt(); alphaBitDepth = buf.getInt(); reserved1 = buf.getInt(); surface = buf.getInt(); colorSpaceLowValue = buf.getInt(); colorSpaceHighValue = buf.getInt(); destBltColorSpaceLowValue = buf.getInt(); destBltColorSpaceHighValue = buf.getInt(); srcOverlayColorSpaceLowValue = buf.getInt(); srcOverlayColorSpaceHighValue = buf.getInt(); srcBltColorSpaceLowValue = buf.getInt(); srcBltColorSpaceHighValue = buf.getInt(); pfSize = buf.getInt(); pfFlags = buf.getInt(); pfFourCC = buf.getInt(); pfRGBBitCount = buf.getInt(); pfRBitMask = buf.getInt(); pfGBitMask = buf.getInt(); pfBBitMask = buf.getInt(); pfABitMask = buf.getInt(); ddsCaps1 = buf.getInt(); ddsCaps2 = buf.getInt(); ddsCapsReserved1 = buf.getInt(); ddsCapsReserved2 = buf.getInt(); textureStage = buf.getInt(); }