Exemplo n.º 1
0
  @Override
  public ByteBuffer retrieveNextMessage(Connection c) throws IOException {
    final NIOReadStream inputStream = c.readStream();

    /*
     * Note that access to the read stream is not synchronized. In this application
     * the VoltPort will invoke this input handler to interact with the read stream guaranteeing
     * thread safety. That said the Connection interface does allow other parts of the application
     * access to the read stream.
     */
    ByteBuffer result = null;

    if (m_nextLength == 0 && inputStream.dataAvailable() > (Integer.SIZE / 8)) {
      m_nextLength = inputStream.getInt();
      if (m_nextLength < 1) {
        throw new IOException(
            "Next message length is " + m_nextLength + " which is less than 1 and is nonsense");
      }
      if (m_nextLength > 52428800) {
        throw new IOException(
            "Next message length is "
                + m_nextLength
                + " which is greater then the hard coded "
                + "max of 52428800. Break up the work into smaller chunks (2 megabytes is reasonable) "
                + "and send as multiple messages or stored procedure invocations");
      }
      assert m_nextLength > 0;
    }
    if (m_nextLength > 0 && inputStream.dataAvailable() >= m_nextLength) {
      result = ByteBuffer.allocate(m_nextLength);
      inputStream.getBytes(result.array());
      m_nextLength = 0;
      m_sequenceId++;
    }
    return result;
  }