Skip to content

Thanks for code. Maybe you interested in proposed improvements: #10

@igor--

Description

@igor--
  • no need HappyDataInputStream extends DataInputStream,
    if you use DataInputStream readFully method: stream.readFully(...) to read from input stream exactly some number
    of bytes (not less as it can be if you use read() )

  • you use double arithmetic in the code (seems it's java script way But in Java we can work with bits fields.
    So instead:
    frame[1] = (byte) (masked | 127);
    frame[2] = (byte) (((int) Math.floor(length / Math.pow(2, 56))) & BYTE);
    frame[3] = (byte) (((int) Math.floor(length / Math.pow(2, 48))) & BYTE);
    frame[4] = (byte) (((int) Math.floor(length / Math.pow(2, 40))) & BYTE);
    frame[5] = (byte) (((int) Math.floor(length / Math.pow(2, 32))) & BYTE);
    frame[6] = (byte) (((int) Math.floor(length / Math.pow(2, 24))) & BYTE);
    frame[7] = (byte) (((int) Math.floor(length / Math.pow(2, 16))) & BYTE);
    frame[8] = (byte) (((int) Math.floor(length / Math.pow(2, 8))) & BYTE);
    frame[9] = (byte) (length & BYTE);

    can be used:
    frame[1] = (byte) (masked | 127);
    //frame[2] = (byte) (0);
    //frame[3] = (byte) (0);
    //frame[4] = (byte) (0);
    //frame[5] = (byte) (0);
    frame[6] = (byte) ((length >> 24) & 0xFF);
    frame[7] = (byte) ((length >> 16) & 0xFF);
    frame[8] = (byte) ((length >> 8) & 0xFF);
    frame[9] = (byte) (length & 0xFF);

    • variable 'length' has int type, so contains only 4 bytes, so
      frame[2]..frame[5] will be always 0.
      (your code ported from java script and wrote as length can contains 8 bytes)

    • you can use DataInputStream standard function to read 2 and 8 bytes integer, likes:
      length = stream.readUnsignedShort(); // read 2 bytes length

      long length8 = stream.readLong(); // read 8 bytes length
      if( length8 > Integer.MAX_VALUE )
      throw new IOException("too big frame length");
      length = (int)length8;

instead of your method: byteArrayToLong()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions