code: public static byte[] intToBytesWithLen(int x, int len) {
          if(len <= 4) {
              return trailingBytes(intToBytes(x), len);
          } else {
              ByteBuffer bb = ByteBuffer.allocate(len);
              bb.position(len-4);
              bb.putInt(x);
              assert bb.remaining() == 0;
              return bb.array();
          }
      }
docs: Write a big-endian integer into the least significant bytes of a byte array.