Java OutputStream Write Int writeInt(int data, int length, OutputStream out)

Here you can find the source of writeInt(int data, int length, OutputStream out)

Description

write Int

License

Apache License

Declaration

public static void writeInt(int data, int length, OutputStream out) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

public class Main {
    public static void writeInt(int data, int length, OutputStream out) throws IOException {
        out.write(getBytes(data, new byte[length]), 0, length);
    }/*from w  ww  .  ja v  a2s. com*/

    public static void write(byte[] data, OutputStream out) throws IOException {
        write(data, data.length, out);
    }

    public static void write(byte[] data, int length, OutputStream out) throws IOException {
        out.write(data != null ? data : new byte[length], 0, length);
    }

    public static byte[] getBytes(byte[] buffer, int offset) {
        byte[] b = new byte[buffer.length - offset];
        System.arraycopy(buffer, offset, b, 0, buffer.length - offset);
        return b;
    }

    public static byte[] getBytes(byte[] buffer, int offset, int length) {
        byte[] b = new byte[length];
        System.arraycopy(buffer, offset, b, 0, length);
        return b;
    }

    public static byte[] getBytes(int value, byte[] dest) {
        int lastIndex = dest.length - 1;
        for (int i = lastIndex; i >= 0; i--) {
            dest[i] = (byte) (value & 0xff);
            value = value >> 8;
        }
        return dest;
    }

    public static byte[] getBytes(long value, byte[] dest) {
        int lastIndex = dest.length - 1;
        for (int i = lastIndex; i >= 0; i--) {
            dest[i] = (byte) (value & 0xff);
            value = value >> 8;
        }
        return dest;
    }

    public static byte[] getBytes(String s) {
        return s.getBytes();
    }

    public static byte[] getBytes(String s, String encoding) throws UnsupportedEncodingException {
        if (encoding != null) {
            return s.getBytes(encoding);
        }

        return s.getBytes();
    }
}

Related

  1. writeInt(final int v, final OutputStream out)
  2. writeInt(final int value, final OutputStream os)
  3. writeInt(final OutputStream out, final int v)
  4. writeInt(final OutputStream out, final int value)
  5. writeInt(final OutputStream outputStream, final int integer)
  6. writeInt(int v, OutputStream stream)
  7. writeInt(int v, OutputStream stream)
  8. writeInt(OutputStream buffer, int val)
  9. writeInt(OutputStream buffer, int value)