Example usage for java.nio ByteOrder BIG_ENDIAN

List of usage examples for java.nio ByteOrder BIG_ENDIAN

Introduction

In this page you can find the example usage for java.nio ByteOrder BIG_ENDIAN.

Prototype

ByteOrder BIG_ENDIAN

To view the source code for java.nio ByteOrder BIG_ENDIAN.

Click Source Link

Document

This constant represents big endian.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    System.out.println(ByteOrder.BIG_ENDIAN.toString());
}

From source file:Main.java

public static void main(String args[]) {
    ByteOrder b = ByteOrder.nativeOrder();

    if (b.equals(ByteOrder.BIG_ENDIAN)) {
        System.out.println("Big endian");
    } else {//from w w w .ja v a 2 s . c  o m

        System.out.println("Little  endian");
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer byteBuffer = ByteBuffer.allocate(7).order(ByteOrder.BIG_ENDIAN);
    CharBuffer charBuffer = byteBuffer.asCharBuffer();

    byteBuffer.put(0, (byte) 0);
    byteBuffer.put(1, (byte) 'H');
    byteBuffer.put(2, (byte) 0);
    byteBuffer.put(3, (byte) 'i');
    byteBuffer.put(4, (byte) 0);
    byteBuffer.put(5, (byte) '!');
    byteBuffer.put(6, (byte) 0);

    println(byteBuffer);//from  ww  w .  j  a v  a  2s .  c om
    println(charBuffer);

    // now slice it differently
    byteBuffer.position(4);
    charBuffer = byteBuffer.asCharBuffer();

    println(byteBuffer);
    println(charBuffer);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(10);
    int capacity = bbuf.capacity(); // 10
    System.out.println(capacity);

    bbuf.order(ByteOrder.BIG_ENDIAN);

    bbuf.put("java2s.com".getBytes());

    System.out.println(Arrays.toString(bbuf.array()));
}

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[12]);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));
    bb.rewind();//from w w  w.j  a  v a2s . com
    bb.order(ByteOrder.BIG_ENDIAN);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));
    bb.rewind();
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));
}

From source file:Endians.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[12]);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));
    bb.rewind();//from  w  w  w.  ja va2 s  . com
    bb.order(ByteOrder.BIG_ENDIAN);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));
    bb.rewind();
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));

}

From source file:UDPTimeServer.java

public static void main(String[] args) throws IOException {

    int port = 37;

    ByteBuffer in = ByteBuffer.allocate(8192);
    ByteBuffer out = ByteBuffer.allocate(8);
    out.order(ByteOrder.BIG_ENDIAN);
    SocketAddress address = new InetSocketAddress(port);
    DatagramChannel channel = DatagramChannel.open();
    DatagramSocket socket = channel.socket();
    socket.bind(address);// w w w  . j  a  v a 2s.  c  o m
    System.err.println("bound to " + address);
    while (true) {
        try {
            in.clear();
            SocketAddress client = channel.receive(in);
            System.err.println(client);
            long secondsSince1970 = System.currentTimeMillis();
            out.clear();
            out.putLong(secondsSince1970);
            out.flip();

            out.position(4);
            channel.send(out, client);
        } catch (Exception ex) {
            System.err.println(ex);
        }
    }
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {

    DatagramChannel channel = DatagramChannel.open();
    SocketAddress address = new InetSocketAddress(0);
    DatagramSocket socket = channel.socket();
    socket.bind(address);/*from   ww w  . j  a  v  a2 s .c  om*/

    SocketAddress server = new InetSocketAddress("time-a.nist.gov", 37);
    channel.connect(server);

    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.order(ByteOrder.BIG_ENDIAN);
    // send a byte of data to the server
    buffer.put((byte) 0);
    buffer.flip();
    channel.write(buffer);

    // get the buffer ready to receive data
    buffer.clear();
    // fill the first four bytes with zeros
    buffer.putInt(0);
    channel.read(buffer);
    buffer.flip();

    // convert seconds since 1900 to a java.util.Date
    long secondsSince1900 = buffer.getLong();
    long differenceBetweenEpochs = 2208988800L;
    long secondsSince1970 = secondsSince1900 - differenceBetweenEpochs;
    long msSince1970 = secondsSince1970 * 1000;
    Date time = new Date(msSince1970);

    System.out.println(time);
}

From source file:UDPTimeClient.java

public static void main(String[] args) throws Exception {

    DatagramChannel channel = DatagramChannel.open();
    // port 0 selects any available port
    SocketAddress address = new InetSocketAddress(0);
    DatagramSocket socket = channel.socket();
    socket.setSoTimeout(5000);// w ww  .j a  v  a 2 s. c om
    socket.bind(address);

    SocketAddress server = new InetSocketAddress("time.nist.gov", 37);
    ByteBuffer buffer = ByteBuffer.allocate(8192);
    // time protocol always uses big-endian order
    buffer.order(ByteOrder.BIG_ENDIAN);
    // Must put at least one byte of data in the buffer;
    // it doesn't matter what it is.
    buffer.put((byte) 65);
    buffer.flip();

    channel.send(buffer, server);

    buffer.clear();
    buffer.put((byte) 0).put((byte) 0).put((byte) 0).put((byte) 0);
    channel.receive(buffer);
    buffer.flip();
    long secondsSince1970 = buffer.getLong();

    System.out.println(secondsSince1970);
    channel.close();

}

From source file:pav.Main.java

/**
 * @param args//from w w  w .  j av  a 2s .c  om
 */
public static void main(String[] args) {
    System.out.println("------------------------------");
    System.out.println("Processing Audio Visualization");
    System.out.println("------------------------------\n");

    Options options = new Options();
    options.addOption("renderer", true, "The Processing render mode to use.");
    options.addOption("width", true, "The width of the visualization window.");
    options.addOption("height", true, "The height of the visualization window.");

    options.addOption("audiosource", true, "Audio source to use (udp or fifo).");
    options.addOption("samplesize", true, "Number of samples per frame (512, 1024 or 2048)");
    options.addOption("samplerate", true, "The sample rate of the audio data.");
    options.addOption("byteorder", true, "Byte order of the samples (le or be)");

    options.addOption("path", true, "Path to the fifo the fifo audio source should use.");
    options.addOption("port", true, "Port the udp audio source should listen to.");

    CommandLineParser parser = new GnuParser();

    try {
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("renderer")) {
            Config.renderer = cmd.getOptionValue("renderer");
        } else {
            Console.out("No render mode specified, using " + Config.renderer + ".");
        }

        if (cmd.hasOption("width")) {
            String option = cmd.getOptionValue("width");

            try {
                Config.windowWidth = Integer.parseInt(option);
            } catch (NumberFormatException e) {
                Console.error("Error while parsing command line arguments: width is not a valid integer.");
            }
        } else {
            Console.out("No window width specified, using " + Config.windowWidth + ".");
        }

        if (cmd.hasOption("height")) {
            String option = cmd.getOptionValue("height");

            try {
                Config.windowHeight = Integer.parseInt(option);
            } catch (NumberFormatException e) {
                Console.error("Error while parsing command line arguments: height is not a valid integer.");
            }
        } else {
            Console.out("No window height specified, using " + Config.windowHeight + ".");
        }

        if (cmd.hasOption("audiosource")) {
            if (cmd.getOptionValue("audiosource").equals(Config.AUDIO_SOURCE_FIFO)) {
                Config.audioSource = Config.AUDIO_SOURCE_FIFO;
            } else if (cmd.getOptionValue("audiosource").equals(Config.AUDIO_SOURCE_UDP)) {
                Config.audioSource = Config.AUDIO_SOURCE_UDP;
            } else {
                Console.error("Invalid audio source specified.");
            }
        } else {
            Console.out("No audio source specified, using " + Config.audioSource + ".");
        }

        if (cmd.hasOption("samplesize")) {
            try {
                int sampleSize = Integer.parseInt(cmd.getOptionValue("samplesize"));

                if (sampleSize == 512 || sampleSize == 1024 || sampleSize == 2048) {
                    Config.sampleSize = sampleSize;
                } else {
                    Console.error("Invalid sample size specified.");
                }
            } catch (NumberFormatException e) {
                Console.error("Error while parsing command line arguments: samplesize is not a valid integer.");
            }
        } else {
            Console.out("No sample size specified, using " + Config.sampleSize + ".");
        }

        if (cmd.hasOption("samplerate")) {
            try {
                Config.sampleRate = Integer.parseInt(cmd.getOptionValue("samplerate"));
            } catch (NumberFormatException e) {
                Console.error("Error while parsing command line arguments: samplerate is not a valid integer.");
            }
        } else {
            Console.out("No sample rate specified, using " + Config.sampleRate + ".");
        }

        if (cmd.hasOption("byteorder")) {
            if (cmd.getOptionValue("byteorder").equals(Config.BYTE_ORDER_LE)) {
                Config.byteOrder = ByteOrder.LITTLE_ENDIAN;
            } else if (cmd.getOptionValue("byteorder").equals(Config.BYTE_ORDER_BE)) {
                Config.byteOrder = ByteOrder.BIG_ENDIAN;
            } else {
                Console.error("Invalid byte order specified.");
            }
        } else {
            Console.out("No byte order specified, using " + Config.BYTE_ORDER_LE + ".");
        }

        if (Config.audioSource.equals(Config.AUDIO_SOURCE_FIFO)) {
            if (cmd.hasOption("path")) {
                if (!(new File(cmd.getOptionValue("path"))).canRead()) {
                    Console.error("Unable to read the specified FIFO, aborting.");
                    return;
                }

                Config.fifoPath = cmd.getOptionValue("path");
            } else {
                Console.error("No fifo path specified, aborting.");
                return;
            }
        }

        if (Config.audioSource.equals(Config.AUDIO_SOURCE_UDP)) {
            if (cmd.hasOption("port")) {
                try {
                    Config.udpPort = Integer.parseInt(cmd.getOptionValue("port"));
                } catch (NumberFormatException e) {
                    Console.error("Error while parsing command line arguments: port is not a valid integer.");
                }
            } else {
                Console.out("No port specified, using " + Config.udpPort + ".");
            }
        }
    } catch (ParseException e) {
        Console.error("Error while parsing command line arguments: " + e.getMessage());
        new HelpFormatter().printHelp("pav", options);
    }

    PApplet.main(new String[] { "pav.PAV" });
}