/** * Populates the header with information received via socket */ public void readHeader() throws IOException { if (this.socket != null) { final ByteBuffer cb = getCommBuffer(); synchronized (cb) { fetchHeader(); final int type = cb.getInt(); final int numParts = cb.getInt(); final int txid = cb.getInt(); cb.clear(); if (!MessageType.validate(type)) { throw new IOException( String.format("Invalid message type %s while reading header", Integer.valueOf(type))); } // Set the header and payload fields only after receiving all the // socket data, providing better message consistency in the face // of exceptional conditions (e.g. IO problems, timeouts etc.) this.messageType = type; this.numberOfParts = numParts; // Already set in setPayloadFields via setNumberOfParts this.transactionId = txid; } } else { throw new IOException("Dead Connection"); } }