/*

Write a String as a Network Int n, followed by n Bytes
Alternative to 16 bit read/writeUTF.
Encoding standard is... ?
6900
public static void writeString(DataOutput out, String s) throws IOException {
        if (s != null) {
            byte[] buffer = s.getBytes("UTF-8");
            int len = buffer.length;
            out.writeInt(len);
            out.write(buffer, 0, len);
        } else {
            out.writeInt(-1);
        }
    }
611273
final void writeInt(int value)
    {
        byte writeBuffer[] = new byte[4];
        writeBuffer[0] = (byte) (value >>> 24);
        writeBuffer[1] = (byte) (value >>> 16);
        writeBuffer[2] = (byte) (value >>> 8);
        writeBuffer[3] = (byte) (value >>> 0);
        write(writeBuffer, 0, 4);
    }
1152220
public static void writeUTF8(OutputStream out, String str) throws IOException {
        byte[] s = str.getBytes(UTF8);
        out.write(s);
    }
1011259
public static void writeBytes(OutputStream os, byte[] buf) throws IOException {
        if (buf.length < OP_PUSHDATA1) {
            os.write(buf.length);
            os.write(buf);
        } else if (buf.length < 256) {
            os.write(OP_PUSHDATA1);
            os.write(buf.length);
            os.write(buf);
        } else if (buf.length < 65536) {
            os.write(OP_PUSHDATA2);
            Utils.uint16ToByteStreamLE(buf.length, os);
            os.write(buf);
        } else {
            throw new RuntimeException("Unimplemented");
        }
    }
1287957
public static int networkByteOrderToInt(byte[] buf, int start, int count) {
        if (count > 4) {
            throw new IllegalArgumentException(
                    "Cannot handle more than 4 bytes");
        }

        int result = 0;

        for (int i = 0; i < count; i++) {
            result <<= 8;
            result |= (buf[start + i] & 0xff);
        }

        return result;
    }
268433
public static void writeInt(byte[] bytes, int value, int offset) {
        bytes[offset] = (byte) (0xFF & (value >> 24));
        bytes[offset + 1] = (byte) (0xFF & (value >> 16));
        bytes[offset + 2] = (byte) (0xFF & (value >> 8));
        bytes[offset + 3] = (byte) (0xFF & value);
    }
193864
function uint16BEtoNumber(bytes) {
  var n = 0;
  for (var i = 0; i < 1; i++) {
    n |= bytes[i];
    n <<= 8;
  }
  n |= bytes[1];
  return n;
}
378435
public static byte[] writeInt(int v, byte[] b, int offset) {
        b[offset] = (byte) v;
        b[offset + 1] = (byte) (v >> 8);
        b[offset + 2] = (byte) (v >> 16);
        b[offset + 3] = (byte) (v >> 24);

        return b;
    }
471137
public void writeBytes(byte[] buffer)
        throws IOException
    {
        if (buffer == null)
            os.write('N');
        else
            writeBytes(buffer, 0, buffer.length);
    }
436664
@Override
  public int write(OutputStreamWithBuffer os, char []cbuf, int off, int len)
    throws IOException
  {
    byte []buffer = os.buffer();
    int length = os.offset();
    int capacity = buffer.length;
    int tail = off + len;
    int head = off;

    while (off < tail) {
      while (capacity - length <= 4) {
        buffer = os.nextBuffer(length);
        length = os.offset();
      }
      
      char ch = cbuf[off++];

      if (ch < 0x80) {
        buffer[length++] = (byte) ch;
      }
      else if (ch < 0x800) {
        buffer[length++] = (byte) (0xc0 + (ch >> 6));
        buffer[length++] = (byte) (0x80 + (ch & 0x3f));
      }
      else if (ch < 0xd800 || 0xdfff < ch) {
        // server/0815
        buffer[length++] = (byte) (0xe0 + (ch >> 12));
        buffer[length++] = (byte) (0x80 + ((ch >> 6) & 0x3f));
        buffer[length++] = (byte) (0x80 + (ch & 0x3f));
      }
      else if (off == tail) {
        off--;
        break;
      }
      else {
        char ch2 = cbuf[off++];
        
        int v = 0x10000 + (ch & 0x3ff) * 0x400 + (ch2 & 0x3ff);
          
        buffer[length++] = (byte) (0xf0 + (v >> 18));
        buffer[length++] = (byte) (0x80 + ((v >> 12) & 0x3f));
        buffer[length++] = (byte) (0x80 + ((v >> 6) & 0x3f));
        buffer[length++] = (byte) (0x80 + (v & 0x3f));
      }
    }

    os.offset(length);
    
    return off - head;
  }
1034968
private void writeInt(int value, byte[] data, int offset) {
        int index = offset;
        data[index++] = (byte) (value & 0xff);
        data[index++] = (byte) ((value >> 8) & 0xff);
        data[index++] = (byte) ((value >> 16) & 0xff);
        data[index++] = (byte) ((value >> 24) & 0xff);
    }
1020062
public static int writeInt(byte[] target, int offset, int value) {
        target[offset] = (byte) (value >>> 24);
        target[offset + 1] = (byte) (value >>> 16);
        target[offset + 2] = (byte) (value >>> 8);
        target[offset + 3] = (byte) value;
        return Integer.BYTES;
    }
964304
private int writeString(String str) {
        int length = 0;
        try {
            byte[] buf = str.getBytes("ISO-8859-1");
            System.arraycopy(buf, 0, output, currentPos, buf.length);
            length = buf.length;
            currentPos += length;
        } catch (UnsupportedEncodingException e) {
            // This should never happen!
        }

        return length;
    }
524085
def WriteUInt256(self, value):
        
        if type(value) is UInt256:
            value.Serialize(self)
        else:
            raise Exception("Cannot write value that is not UInt256")
715909
long write(OutputStream out) throws IOException {
    // decode macbinary if this was sent
    if (contentType.equals("application/x-macbinary")) {
      out = new MacBinaryDecoderOutputStream(out);
    }
    long size=0;
    int read;
    byte[] buf = new byte[8 * 1024];
    while((read = partInput.read(buf)) != -1) {
      out.write(buf, 0, read);
      size += read;
    }
    return size;
  }
191983
function writeVarInt (buffer, number, offset) {
  varuint.encode(number, buffer, offset);
  return varuint.encode.bytes;
}
524840
def write_uint8(self, value, little_endian=True):
        
        if little_endian:
            endian = "<"
        else:
            endian = ">"
        return self.pack('%sB' % endian, value)