Utility code for dealing with different endian systems : Stream « File Input Output « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » File Input Output » StreamScreenshots 
Utility code for dealing with different endian systems
   
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 
 *      http://www.apache.org/licenses/LICENSE-2.0
 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Utility code for dealing with different endian systems.
 * <p>
 * Different computer architectures adopt different conventions for
 * byte ordering. In so-called "Little Endian" architectures (eg Intel),
 * the low-order byte is stored in memory at the lowest address, and
 * subsequent bytes at higher addresses. For "Big Endian" architectures
 * (eg Motorola), the situation is reversed.
 * This class helps you solve this incompatability.
 * <p>
 * Origin of code: Excalibur
 *
 @author <a href="mailto:peter@apache.org">Peter Donald</a>
 @version $Id: EndianUtils.java 539632 2007-05-18 23:37:59Z bayard $
 @see org.apache.commons.io.input.SwappedDataInputStream
 */
public class EndianUtils {

    /**
     * Instances should NOT be constructed in standard programming.
     */
    public EndianUtils() {
        super();
    }

    // Swapping routines

    /**
     * Converts a "short" value between endian systems.
     @param value value to convert
     @return the converted value
     */
    public static short swapShort(short value) {
        return (short) ( ( ( ( value >> 0xff << +
            ( ( ( value >> 0xff << ) );
    }

    /**
     * Converts a "int" value between endian systems.
     @param value value to convert
     @return the converted value
     */
    public static int swapInteger(int value) {
        return
            ( ( ( value >> 0xff << 24 +
            ( ( ( value >> 0xff << 16 +
            ( ( ( value >> 16 0xff << +
            ( ( ( value >> 24 0xff << );
    }

    /**
     * Converts a "long" value between endian systems.
     @param value value to convert
     @return the converted value
     */
    public static long swapLong(long value) {
        return
            ( ( ( value >> 0xff << 56 +
            ( ( ( value >> 0xff << 48 +
            ( ( ( value >> 16 0xff << 40 +
            ( ( ( value >> 24 0xff << 32 +
            ( ( ( value >> 32 0xff << 24 +
            ( ( ( value >> 40 0xff << 16 +
            ( ( ( value >> 48 0xff << +
            ( ( ( value >> 56 0xff << );
    }

    /**
     * Converts a "float" value between endian systems.
     @param value value to convert
     @return the converted value
     */
    public static float swapFloat(float value) {
        return Float.intBitsToFloatswapIntegerFloat.floatToIntBitsvalue ) ) );
    }

    /**
     * Converts a "double" value between endian systems.
     @param value value to convert
     @return the converted value
     */
    public static double swapDouble(double value) {
        return Double.longBitsToDoubleswapLongDouble.doubleToLongBitsvalue ) ) );
    }

    // Swapping read/write routines

    /**
     * Writes a "short" value to a byte array at a given offset. The value is
     * converted to the opposed endian system while writing.
     @param data target byte array
     @param offset starting offset in the byte array
     @param value value to write
     */
    public static void writeSwappedShort(byte[] data, int offset, short value) {
        dataoffset + (byte)( ( value >> 0xff );
        dataoffset + (byte)( ( value >> 0xff );
    }

    /**
     * Reads a "short" value from a byte array at a given offset. The value is
     * converted to the opposed endian system while reading.
     @param data source byte array
     @param offset starting offset in the byte array
     @return the value read
     */
    public static short readSwappedShort(byte[] data, int offset) {
        return (short)( ( ( dataoffset + 0xff << +
            ( ( dataoffset + 0xff << ) );
    }

    /**
     * Reads an unsigned short (16-bit) value from a byte array at a given
     * offset. The value is converted to the opposed endian system while
     * reading.
     @param data source byte array
     @param offset starting offset in the byte array
     @return the value read
     */
    public static int readSwappedUnsignedShort(byte[] data, int offset) {
        return ( ( ( dataoffset + 0xff << +
            ( ( dataoffset + 0xff << ) );
    }

    /**
     * Writes a "int" value to a byte array at a given offset. The value is
     * converted to the opposed endian system while writing.
     @param data target byte array
     @param offset starting offset in the byte array
     @param value value to write
     */
    public static void writeSwappedInteger(byte[] data, int offset, int value) {
        dataoffset + (byte)( ( value >> 0xff );
        dataoffset + (byte)( ( value >> 0xff );
        dataoffset + (byte)( ( value >> 16 0xff );
        dataoffset + (byte)( ( value >> 24 0xff );
    }

    /**
     * Reads a "int" value from a byte array at a given offset. The value is
     * converted to the opposed endian system while reading.
     @param data source byte array
     @param offset starting offset in the byte array
     @return the value read
     */
    public static int readSwappedInteger(byte[] data, int offset) {
        return ( ( ( dataoffset + 0xff << +
            ( ( dataoffset + 0xff << +
            ( ( dataoffset + 0xff << 16 +
            ( ( dataoffset + 0xff << 24 ) );
    }

    /**
     * Reads an unsigned integer (32-bit) value from a byte array at a given
     * offset. The value is converted to the opposed endian system while
     * reading.
     @param data source byte array
     @param offset starting offset in the byte array
     @return the value read
     */
    public static long readSwappedUnsignedInteger(byte[] data, int offset) {
        long low = ( ( ( dataoffset + 0xff << +
                     ( ( dataoffset + 0xff << +
                     ( ( dataoffset + 0xff << 16 ) );

        long high = dataoffset + 0xff;

        return (high << 24(0xffffffffL & low)
    }

    /**
     * Writes a "long" value to a byte array at a given offset. The value is
     * converted to the opposed endian system while writing.
     @param data target byte array
     @param offset starting offset in the byte array
     @param value value to write
     */
    public static void writeSwappedLong(byte[] data, int offset, long value) {
        dataoffset + (byte)( ( value >> 0xff );
        dataoffset + (byte)( ( value >> 0xff );
        dataoffset + (byte)( ( value >> 16 0xff );
        dataoffset + (byte)( ( value >> 24 0xff );
        dataoffset + (byte)( ( value >> 32 0xff );
        dataoffset + (byte)( ( value >> 40 0xff );
        dataoffset + (byte)( ( value >> 48 0xff );
        dataoffset + (byte)( ( value >> 56 0xff );
    }

    /**
     * Reads a "long" value from a byte array at a given offset. The value is
     * converted to the opposed endian system while reading.
     @param data source byte array
     @param offset starting offset in the byte array
     @return the value read
     */
    public static long readSwappedLong(byte[] data, int offset) {
        long low = 
            ( ( dataoffset + 0xff << +
            ( ( dataoffset + 0xff << +
            ( ( dataoffset + 0xff << 16 +
            ( ( dataoffset + 0xff << 24 );
        long high = 
            ( ( dataoffset + 0xff << +
            ( ( dataoffset + 0xff << +
            ( ( dataoffset + 0xff << 16 +
            ( ( dataoffset + 0xff << 24 );
        return (high << 32(0xffffffffL & low)
    }

    /**
     * Writes a "float" value to a byte array at a given offset. The value is
     * converted to the opposed endian system while writing.
     @param data target byte array
     @param offset starting offset in the byte array
     @param value value to write
     */
    public static void writeSwappedFloat(byte[] data, int offset, float value) {
        writeSwappedIntegerdata, offset, Float.floatToIntBitsvalue ) );
    }

    /**
     * Reads a "float" value from a byte array at a given offset. The value is
     * converted to the opposed endian system while reading.
     @param data source byte array
     @param offset starting offset in the byte array
     @return the value read
     */
    public static float readSwappedFloat(byte[] data, int offset) {
        return Float.intBitsToFloatreadSwappedIntegerdata, offset ) );
    }

    /**
     * Writes a "double" value to a byte array at a given offset. The value is
     * converted to the opposed endian system while writing.
     @param data target byte array
     @param offset starting offset in the byte array
     @param value value to write
     */
    public static void writeSwappedDouble(byte[] data, int offset, double value) {
        writeSwappedLongdata, offset, Double.doubleToLongBitsvalue ) );
    }

    /**
     * Reads a "double" value from a byte array at a given offset. The value is
     * converted to the opposed endian system while reading.
     @param data source byte array
     @param offset starting offset in the byte array
     @return the value read
     */
    public static double readSwappedDouble(byte[] data, int offset) {
        return Double.longBitsToDoublereadSwappedLongdata, offset ) );
    }

    /**
     * Writes a "short" value to an OutputStream. The value is
     * converted to the opposed endian system while writing.
     @param output target OutputStream
     @param value value to write
     @throws IOException in case of an I/O problem
     */
    public static void writeSwappedShort(OutputStream output, short value)
        throws IOException
    {
        output.write( (byte)( ( value >> 0xff ) );
        output.write( (byte)( ( value >> 0xff ) );
    }

    /**
     * Reads a "short" value from an InputStream. The value is
     * converted to the opposed endian system while reading.
     @param input source InputStream
     @return the value just read
     @throws IOException in case of an I/O problem
     */
    public static short readSwappedShort(InputStream input)
        throws IOException
    {
        return (short)( ( ( readinput 0xff << +
            ( ( readinput 0xff << ) );
    }

    /**
     * Reads a unsigned short (16-bit) from an InputStream. The value is
     * converted to the opposed endian system while reading.
     @param input source InputStream
     @return the value just read
     @throws IOException in case of an I/O problem
     */
    public static int readSwappedUnsignedShort(InputStream input)
        throws IOException
    {
        int value1 = readinput );
        int value2 = readinput );

        return ( ( ( value1 & 0xff << +
            ( ( value2 & 0xff << ) );
    }

    /**
     * Writes a "int" value to an OutputStream. The value is
     * converted to the opposed endian system while writing.
     @param output target OutputStream
     @param value value to write
     @throws IOException in case of an I/O problem
     */
    public static void writeSwappedInteger(OutputStream output, int value)
        throws IOException
    {
        output.write( (byte)( ( value >> 0xff ) );
        output.write( (byte)( ( value >> 0xff ) );
        output.write( (byte)( ( value >> 16 0xff ) );
        output.write( (byte)( ( value >> 24 0xff ) );
    }

    /**
     * Reads a "int" value from an InputStream. The value is
     * converted to the opposed endian system while reading.
     @param input source InputStream
     @return the value just read
     @throws IOException in case of an I/O problem
     */
    public static int readSwappedInteger(InputStream input)
        throws IOException
    {
        int value1 = readinput );
        int value2 = readinput );
        int value3 = readinput );
        int value4 = readinput );

        return ( ( value1 & 0xff << +
            ( ( value2 & 0xff << +
            ( ( value3 & 0xff << 16 +
            ( ( value4 & 0xff << 24 );
    }

    /**
     * Reads a unsigned integer (32-bit) from an InputStream. The value is
     * converted to the opposed endian system while reading.
     @param input source InputStream
     @return the value just read
     @throws IOException in case of an I/O problem
     */
    public static long readSwappedUnsignedInteger(InputStream input)
        throws IOException
    {
        int value1 = readinput );
        int value2 = readinput );
        int value3 = readinput );
        int value4 = readinput );

        long low = ( ( ( value1 & 0xff << +
                     ( ( value2 & 0xff << +
                     ( ( value3 & 0xff << 16 ) );

        long high = value4 & 0xff;

        return (high << 24(0xffffffffL & low)
    }

    /**
     * Writes a "long" value to an OutputStream. The value is
     * converted to the opposed endian system while writing.
     @param output target OutputStream
     @param value value to write
     @throws IOException in case of an I/O problem
     */
    public static void writeSwappedLong(OutputStream output, long value)
        throws IOException
    {
        output.write( (byte)( ( value >> 0xff ) );
        output.write( (byte)( ( value >> 0xff ) );
        output.write( (byte)( ( value >> 16 0xff ) );
        output.write( (byte)( ( value >> 24 0xff ) );
        output.write( (byte)( ( value >> 32 0xff ) );
        output.write( (byte)( ( value >> 40 0xff ) );
        output.write( (byte)( ( value >> 48 0xff ) );
        output.write( (byte)( ( value >> 56 0xff ) );
    }

    /**
     * Reads a "long" value from an InputStream. The value is
     * converted to the opposed endian system while reading.
     @param input source InputStream
     @return the value just read
     @throws IOException in case of an I/O problem
     */
    public static long readSwappedLong(InputStream input)
        throws IOException
    {
        byte[] bytes = new byte[8];
        for int i=0; i<8; i++ ) {
            bytes[i(bytereadinput );
        }
        return readSwappedLongbytes, );
    }

    /**
     * Writes a "float" value to an OutputStream. The value is
     * converted to the opposed endian system while writing.
     @param output target OutputStream
     @param value value to write
     @throws IOException in case of an I/O problem
     */
    public static void writeSwappedFloat(OutputStream output, float value)
        throws IOException
    {
        writeSwappedIntegeroutput, Float.floatToIntBitsvalue ) );
    }

    /**
     * Reads a "float" value from an InputStream. The value is
     * converted to the opposed endian system while reading.
     @param input source InputStream
     @return the value just read
     @throws IOException in case of an I/O problem
     */
    public static float readSwappedFloat(InputStream input)
        throws IOException
    {
        return Float.intBitsToFloatreadSwappedIntegerinput ) );
    }

    /**
     * Writes a "double" value to an OutputStream. The value is
     * converted to the opposed endian system while writing.
     @param output target OutputStream
     @param value value to write
     @throws IOException in case of an I/O problem
     */
    public static void writeSwappedDouble(OutputStream output, double value)
        throws IOException
    {
        writeSwappedLongoutput, Double.doubleToLongBitsvalue ) );
    }

    /**
     * Reads a "double" value from an InputStream. The value is
     * converted to the opposed endian system while reading.
     @param input source InputStream
     @return the value just read
     @throws IOException in case of an I/O problem
     */
    public static double readSwappedDouble(InputStream input)
        throws IOException
    {
        return Double.longBitsToDoublereadSwappedLonginput ) );
    }

    /**
     * Reads the next byte from the input stream.
     @param input  the stream
     @return the byte
     @throws IOException if the end of file is reached
     */
    private static int read(InputStream input)
        throws IOException
    {
        int value = input.read();

        if-== value ) {
            throw new EOFException"Unexpected EOF reached" );
        }

        return value;
    }
}

   
    
    
  
Related examples in the same category
1. Show the content of a file
2. Some general utility functions for dealing with Streams
3. Utilities related to file and stream handling.
4. Utility functions related to Streams
5. Utility methods for handling streams
6. Various utility methods that have something to do with I/O
7. General IO Stream manipulation
8. General IO stream manipulation utilities
9. Count the number of bytes read through the stream
10. Count OutputStream
11. File utilities for file read and write
12. An InputStream class that terminates the stream when it encounters a particular byte sequence.
13. An InputStream that implements HTTP/1.1 chunking
14. An OutputStream which relays all data written into it into a list of given OutputStreams
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.