Search in sources :

Example 21 with ByteOrder

use of java.nio.ByteOrder in project sis by apache.

the class ChannelImageOutputStream method writeUTF.

/**
 * Writes two bytes of length information to the output stream, followed by the modified UTF-8 representation
 * of every character in the {@code str} string. Each character is converted to a group of one, two, or three
 * bytes, depending on the character code point value.
 *
 * @param  s  the string to be written.
 * @throws IOException if an error occurred while writing the stream.
 */
@Override
public void writeUTF(final String s) throws IOException {
    byte[] data = s.getBytes("UTF-8");
    if (data.length > Short.MAX_VALUE) {
        throw new IllegalArgumentException(Resources.format(Resources.Keys.ExcessiveStringSize_3, filename, Short.MAX_VALUE, data.length));
    }
    final ByteOrder oldOrder = buffer.order();
    buffer.order(ByteOrder.BIG_ENDIAN);
    try {
        writeShort(data.length);
        write(data);
    } finally {
        buffer.order(oldOrder);
    }
}
Also used : ByteOrder(java.nio.ByteOrder)

Example 22 with ByteOrder

use of java.nio.ByteOrder in project sulong by graalvm.

the class ElfFile method create.

public static ElfFile create(ByteBuffer data) {
    checkIdent(data);
    ByteOrder order = isBigEndian(data) ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
    data.order(order).position(EI_NIDENT);
    boolean is64Bit = is64Bit(data);
    ElfHeader header = ElfHeader.create(data, is64Bit);
    ElfSectionHeaderTable sectionHeaderTable = ElfSectionHeaderTable.create(header, data, is64Bit);
    ElfDynamicSection dynamicSection = ElfDynamicSection.create(sectionHeaderTable, data, is64Bit);
    return new ElfFile(header, sectionHeaderTable, dynamicSection);
}
Also used : ByteOrder(java.nio.ByteOrder)

Example 23 with ByteOrder

use of java.nio.ByteOrder in project openj9 by eclipse.

the class CoreFileResolver method readFooter.

private void readFooter() throws IOException {
    // the footer is a serialised java object and so always BIG_ENDIAN
    ByteOrder bo = stream.getByteOrder();
    try {
        stream.setByteOrder(ByteOrder.BIG_ENDIAN);
        long streamLength = stream.length();
        stream.seek(streamLength - 8);
        int length = stream.readInt();
        SlidingFileInputStream in = new SlidingFileInputStream(stream, streamLength - length - 8, length);
        ObjectInputStream objin = new ObjectInputStream(in);
        try {
            footer = (Footer) objin.readObject();
            logger.fine(footer.toString());
        } catch (ClassNotFoundException e) {
            logger.log(WARNING, "Could find footer class", e);
        }
        objin.close();
    } finally {
        // restore the stream to the original byte order
        stream.setByteOrder(bo);
    }
}
Also used : ByteOrder(java.nio.ByteOrder) ObjectInputStream(java.io.ObjectInputStream)

Example 24 with ByteOrder

use of java.nio.ByteOrder in project openj9 by eclipse.

the class FileSniffer method isElfCoreFile.

/* Check that an elf file is definitely an elf core file.
	 * (It could also be a library, executable etc...)
	 */
private static boolean isElfCoreFile(ImageInputStream iis) throws IOException {
    // First 16 bytes are e_ident, after that
    // e_type is a 16 bit value that tells us what type of elf file
    // this is.
    // #define ET_NONE         0               /* No file type */
    // #define ET_REL          1               /* Relocatable file */
    // #define ET_EXEC         2               /* Executable file */
    // #define ET_DYN          3               /* Shared object file */
    // /#define ET_CORE         4               /* Core file */
    // Core files will have 4 in that space.
    // Before that byte 6 tells us if we are big or little endian, which we also need to know
    // as e_type is two bytes long.
    boolean isCore = false;
    ByteOrder originalOrder = iis.getByteOrder();
    iis.seek(5);
    byte order = iis.readByte();
    iis.seek(16);
    short type = 0;
    if (order == 1) {
        iis.setByteOrder(ByteOrder.LITTLE_ENDIAN);
        type = iis.readShort();
    } else if (order == 2) {
        iis.setByteOrder(ByteOrder.BIG_ENDIAN);
        type = iis.readShort();
    } else {
    // Invalid byte, not an Elf file. Don't set type.
    }
    if (type == 4) {
        isCore = true;
    }
    iis.setByteOrder(originalOrder);
    return isCore;
}
Also used : ByteOrder(java.nio.ByteOrder)

Example 25 with ByteOrder

use of java.nio.ByteOrder in project sagacity-sqltoy by chenrenfei.

the class TaskController method hash.

/**
 * @todo 提供字符串hash算法,产生vo对象的serialVersionUID值
 * @param key
 * @return
 */
private static long hash(String key) {
    ByteBuffer buf = ByteBuffer.wrap(key.getBytes());
    int seed = 0x1234ABCD;
    ByteOrder byteOrder = buf.order();
    buf.order(ByteOrder.LITTLE_ENDIAN);
    long m = 0xc6a4a7935bd1e995L;
    int r = 47;
    long h = seed ^ (buf.remaining() * m);
    long k;
    while (buf.remaining() >= 8) {
        k = buf.getLong();
        k *= m;
        k ^= k >>> r;
        k *= m;
        h ^= k;
        h *= m;
    }
    if (buf.remaining() > 0) {
        ByteBuffer finish = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN);
        // for big-endian version, do this first:
        // finish.position(8-buf.remaining());
        finish.put(buf).rewind();
        h ^= finish.getLong();
        h *= m;
    }
    h ^= h >>> r;
    h *= m;
    h ^= h >>> r;
    buf.order(byteOrder);
    return Math.abs(h);
}
Also used : ByteOrder(java.nio.ByteOrder) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteOrder (java.nio.ByteOrder)111 ByteBuffer (java.nio.ByteBuffer)39 IOException (java.io.IOException)8 Test (org.junit.Test)7 QuickTest (com.hazelcast.test.annotation.QuickTest)5 DataInputStream (java.io.DataInputStream)5 FileInputStream (java.io.FileInputStream)5 UUID (java.util.UUID)5 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)4 HKL (ffx.crystal.HKL)3 DataOutputStream (java.io.DataOutputStream)3 EOFException (java.io.EOFException)3 FileOutputStream (java.io.FileOutputStream)3 ShortBuffer (java.nio.ShortBuffer)3 ArrayList (java.util.ArrayList)3 Element (org.jdom.Element)3 InvalidDumpFormatException (com.ibm.j9ddr.corereaders.InvalidDumpFormatException)2 Point (com.revolsys.geometry.model.Point)2 Crystal (ffx.crystal.Crystal)2 BigInteger (java.math.BigInteger)2