Exemple #1
0
  /* (non-Javadoc) @see java.util.Queue#poll() */
  @Override
  public byte[] poll() {
    byte[] data = null;
    final int lim = rcvbuffer.length;
    if (rcvbuffoff == lim) {
      /* get more data */
      final InputStream in = rcvin;
      int off = 0;
      while (off < lim) {
        try {
          off += in.read(rcvbuffer, off, lim - off);
        } catch (IOException e) {
          e.printStackTrace();
          throw new RuntimeException("Error reading from inputstream", e);
        }
      }
      rcvbuffoff = 0;
    }
    //		final int buffidx = DataCodec.readInt(rcvbuffer, rcvbuffoff);
    final int buffidx = DataCodec.readShort(rcvbuffer, rcvbuffoff);
    //		rcvbuffoff +=DataCodec.INTEGER_BYTES;
    rcvbuffoff += DataCodec.SHORT_BYTES;

    if (buffidx != rbuffidx) {
      rbuffers[rbuffidx].clear();
      rbuffidx = buffidx;
    }
    //		final int dlen = DataCodec.readInt(rcvbuffer, rcvbuffoff);
    final int dlen = DataCodec.readShort(rcvbuffer, rcvbuffoff);
    //		rcvbuffoff +=DataCodec.INTEGER_BYTES;
    rcvbuffoff += DataCodec.SHORT_BYTES;
    data = new byte[dlen];
    rbuffers[buffidx].get(data);
    return data;
  }
Exemple #2
0
  /* (non-Javadoc) @see java.util.Queue#offer(java.lang.Object) */
  @Override
  public final boolean offer(final byte[] b) {
    //		int curridx = wbuffidx;
    short curridx = wbuffidx;
    ByteBuffer currbuff = wbuffers[wbuffidx];
    final int currcap = currbuff.remaining();
    if (b.length > currcap) {
      currbuff.clear();
      // REVU: not handling sender overtaking receiver
      curridx = (short) ((curridx + 1) % DATA_BUFF_CNT);
      currbuff = wbuffers[curridx];
      wbuffidx = curridx;
    }
    //		currbuff.put(b); // REVU: flush first?

    if (sndbuffoff + MSG_SIZE > sndbuffer.length) {
      /* flush */
      try {
        this.sndout.write(sndbuffer);
        this.sndout.flush();
        sndbuffoff = 0;
      } catch (IOException e1) {
        e1.printStackTrace();
      }
    }

    currbuff.put(b); // REVU^:Or write first?

    //		DataCodec.writeInt(wbuffidx, sndbuffer, sndbuffoff); sndbuffoff += DataCodec.INTEGER_BYTES;
    //		DataCodec.writeInt(b.length, sndbuffer, sndbuffoff); sndbuffoff += DataCodec.INTEGER_BYTES;
    DataCodec.writeShort(wbuffidx, sndbuffer, sndbuffoff);
    sndbuffoff += DataCodec.SHORT_BYTES;
    DataCodec.writeShort((short) b.length, sndbuffer, sndbuffoff);
    sndbuffoff += DataCodec.SHORT_BYTES;
    return true;
  }