/** * constructs HooMEResponse from input stream * * @param in deserialization input source * @throws java.io.IOException if I/O problem * @throws BadAttributeValueException if bad data value */ public HooMEResponse(MessageInput in) throws java.io.IOException, BadAttributeValueException { super(in); this.resultList = new ArrayList<Result>(); int payloadLength = HooMEUtilities.getUnsignedInt(in.getScanner().readShort()); long expecting = 0; byte rawMatches = in.getScanner().readByte(); int mul = 8; // cobined size in octets of file id and size for every result short rawPort = in.getScanner().readShort(); byte[] addressArray = new byte[Consts.FOUR_BITS]; for (int i = 0; i < Consts.FOUR_BITS; i++) { addressArray[i] = in.getScanner().readByte(); } this.setResponseHost( new InetSocketAddress( InetAddress.getByAddress(addressArray), HooMEUtilities.getUnsignedInt(rawPort))); long matches = HooMEUtilities.getUnsignedLongInt(rawMatches); expecting = (mul * matches); for (long i = 0; i < matches; i++) { Result aResult = new Result(in); // check to see if we have exceeded the specified payload length and throw exception if ((aResult.getFileName().length() + expecting) > payloadLength || (aResult.getFileName().length() + expecting) > Consts.MAX_UNSIGNED_SHORT) { throw new BadAttributeValueException(Consts.ILL_ARG); } else { expecting += aResult.getFileName().length(); this.addResult(aResult); } } }
/** * checks to see if we have not exceeded the allowable number of bytes in a payload * * @param result the result that is being added * @return true if not bad attribute */ public boolean validateSize(Result result) { int existingResultSize = 8; // size of a result object with an empty string int count = 28; // combined size of all fields currently in the hoome response class count += existingResultSize + result .getFileName() .length(); // add the length result to be added to the current count for (Result aResult : this.resultList) { count += existingResultSize + aResult.getFileName().length(); if (count > Consts.MAX_UNSIGNED_SHORT) { return false; } } return true; }
/** overides encode in HooMEMessage */ @Override public void encode(MessageOutput out) throws java.io.IOException { super.encode(out); int payLoadLength = 0; for (Result aResult : this.resultList) { payLoadLength += aResult.getFileName().length() + 1; } // add header for result payLoadLength += (Consts.FOUR_BITS + Consts.FOUR_BITS) * this.resultList.size(); // add the header for response payLoadLength += 1 + Consts.TWO_BITS + Consts.FOUR_BITS; out.getDataOutputStream().writeShort((short) payLoadLength); out.getDataOutputStream().writeByte((byte) this.resultList.size()); out.getDataOutputStream().writeShort((short) this.responseHost.getPort()); out.getDataOutputStream().write(this.responseHost.getAddress().getAddress()); for (Result aResult : this.resultList) { aResult.encode(out); } }