Example usage for java.lang Long reverseBytes

List of usage examples for java.lang Long reverseBytes

Introduction

In this page you can find the example usage for java.lang Long reverseBytes.

Prototype

@HotSpotIntrinsicCandidate
public static long reverseBytes(long i) 

Source Link

Document

Returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified long value.

Usage

From source file:Main.java

public static void main(String[] args) {

    long l = 8;//w  w w .  j  a  va  2s.  c o  m
    System.out.println("Number = " + l);

    System.out.println("Binary = " + Long.toBinaryString(l));

    System.out.println("After reversing = " + Long.reverseBytes(l));
}

From source file:Main.java

public static void writeLong(DataOutputStream os, long l) throws IOException {
    os.writeLong(Long.reverseBytes(l));
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

/**
 * Finishes the addition of entries to this archive, without closing it.
 * //from  w ww  .  j a v a2 s  . c  o m
 * @throws IOException if archive is already closed.
 */
public void finish() throws IOException {
    if (finished) {
        throw new IOException("This archive has already been finished");
    }
    finished = true;

    final long headerPosition = file.getFilePointer();

    final ByteArrayOutputStream headerBaos = new ByteArrayOutputStream();
    final DataOutputStream header = new DataOutputStream(headerBaos);

    writeHeader(header);
    header.flush();
    final byte[] headerBytes = headerBaos.toByteArray();
    file.write(headerBytes);

    final CRC32 crc32 = new CRC32();

    // signature header
    file.seek(0);
    file.write(SevenZFile.sevenZSignature);
    // version
    file.write(0);
    file.write(2);

    // start header
    final ByteArrayOutputStream startHeaderBaos = new ByteArrayOutputStream();
    final DataOutputStream startHeaderStream = new DataOutputStream(startHeaderBaos);
    startHeaderStream.writeLong(Long.reverseBytes(headerPosition - SevenZFile.SIGNATURE_HEADER_SIZE));
    startHeaderStream.writeLong(Long.reverseBytes(0xffffFFFFL & headerBytes.length));
    crc32.reset();
    crc32.update(headerBytes);
    startHeaderStream.writeInt(Integer.reverseBytes((int) crc32.getValue()));
    startHeaderStream.flush();
    final byte[] startHeaderBytes = startHeaderBaos.toByteArray();
    crc32.reset();
    crc32.update(startHeaderBytes);
    file.writeInt(Integer.reverseBytes((int) crc32.getValue()));
    file.write(startHeaderBytes);
}

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private StartHeader readStartHeader(final long startHeaderCrc) throws IOException {
    final StartHeader startHeader = new StartHeader();
    DataInputStream dataInputStream = null;
    try {/*from  www .j a va2s.  co m*/
        dataInputStream = new DataInputStream(new CRC32VerifyingInputStream(
                new BoundedRandomAccessFileInputStream(file, 20), 20, startHeaderCrc));
        startHeader.nextHeaderOffset = Long.reverseBytes(dataInputStream.readLong());
        startHeader.nextHeaderSize = Long.reverseBytes(dataInputStream.readLong());
        startHeader.nextHeaderCrc = 0xffffFFFFL & Integer.reverseBytes(dataInputStream.readInt());
        return startHeader;
    } finally {
        if (dataInputStream != null) {
            dataInputStream.close();
        }
    }
}

From source file:com.diablominer.DiabloMiner.NetworkState.JSONRPCNetworkState.java

WorkState doGetWorkMessage(boolean longPoll) throws IOException {
    ObjectNode getWorkMessage = mapper.createObjectNode();

    getWorkMessage.put("method", "getwork");
    getWorkMessage.putArray("params");
    getWorkMessage.put("id", 1);

    JsonNode responseMessage = doJSONRPCCall(longPoll, getWorkMessage);

    String datas;//  w w  w. j  a v  a 2  s  .  c o  m
    String midstates;
    String targets;

    try {
        datas = responseMessage.get("data").asText();
        midstates = responseMessage.get("midstate").asText();
        targets = responseMessage.get("target").asText();
    } catch (Exception e) {
        throw new IOException("Bitcoin returned unparsable JSON");
    }

    WorkState workState = new WorkState(this);

    String parse;

    for (int i = 0; i < 32; i++) {
        parse = datas.substring(i * 8, (i * 8) + 8);
        workState.setData(i, Integer.reverseBytes((int) Long.parseLong(parse, 16)));
    }

    for (int i = 0; i < 8; i++) {
        parse = midstates.substring(i * 8, (i * 8) + 8);
        workState.setMidstate(i, Integer.reverseBytes((int) Long.parseLong(parse, 16)));
    }

    for (int i = 0; i < 8; i++) {
        parse = targets.substring(i * 8, (i * 8) + 8);
        workState.setTarget(i, (Long.reverseBytes(Long.parseLong(parse, 16) << 16)) >>> 16);
    }

    return workState;
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

private void writeFileCTimes(final DataOutput header) throws IOException {
    int numCreationDates = 0;
    for (final SevenZArchiveEntry entry : files) {
        if (entry.getHasCreationDate()) {
            ++numCreationDates;//from w w  w . j ava  2s .  co  m
        }
    }
    if (numCreationDates > 0) {
        header.write(NID.kCTime);

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final DataOutputStream out = new DataOutputStream(baos);
        if (numCreationDates != files.size()) {
            out.write(0);
            final BitSet cTimes = new BitSet(files.size());
            for (int i = 0; i < files.size(); i++) {
                cTimes.set(i, files.get(i).getHasCreationDate());
            }
            writeBits(out, cTimes, files.size());
        } else {
            out.write(1); // "allAreDefined" == true
        }
        out.write(0);
        for (final SevenZArchiveEntry entry : files) {
            if (entry.getHasCreationDate()) {
                out.writeLong(
                        Long.reverseBytes(SevenZArchiveEntry.javaTimeToNtfsTime(entry.getCreationDate())));
            }
        }
        out.flush();
        final byte[] contents = baos.toByteArray();
        writeUint64(header, contents.length);
        header.write(contents);
    }
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

private void writeFileATimes(final DataOutput header) throws IOException {
    int numAccessDates = 0;
    for (final SevenZArchiveEntry entry : files) {
        if (entry.getHasAccessDate()) {
            ++numAccessDates;/* w  ww  .  java  2 s. c  o  m*/
        }
    }
    if (numAccessDates > 0) {
        header.write(NID.kATime);

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final DataOutputStream out = new DataOutputStream(baos);
        if (numAccessDates != files.size()) {
            out.write(0);
            final BitSet aTimes = new BitSet(files.size());
            for (int i = 0; i < files.size(); i++) {
                aTimes.set(i, files.get(i).getHasAccessDate());
            }
            writeBits(out, aTimes, files.size());
        } else {
            out.write(1); // "allAreDefined" == true
        }
        out.write(0);
        for (final SevenZArchiveEntry entry : files) {
            if (entry.getHasAccessDate()) {
                out.writeLong(Long.reverseBytes(SevenZArchiveEntry.javaTimeToNtfsTime(entry.getAccessDate())));
            }
        }
        out.flush();
        final byte[] contents = baos.toByteArray();
        writeUint64(header, contents.length);
        header.write(contents);
    }
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

private void writeFileMTimes(final DataOutput header) throws IOException {
    int numLastModifiedDates = 0;
    for (final SevenZArchiveEntry entry : files) {
        if (entry.getHasLastModifiedDate()) {
            ++numLastModifiedDates;/*from   www .  j  a  v  a2 s . c o  m*/
        }
    }
    if (numLastModifiedDates > 0) {
        header.write(NID.kMTime);

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final DataOutputStream out = new DataOutputStream(baos);
        if (numLastModifiedDates != files.size()) {
            out.write(0);
            final BitSet mTimes = new BitSet(files.size());
            for (int i = 0; i < files.size(); i++) {
                mTimes.set(i, files.get(i).getHasLastModifiedDate());
            }
            writeBits(out, mTimes, files.size());
        } else {
            out.write(1); // "allAreDefined" == true
        }
        out.write(0);
        for (final SevenZArchiveEntry entry : files) {
            if (entry.getHasLastModifiedDate()) {
                out.writeLong(
                        Long.reverseBytes(SevenZArchiveEntry.javaTimeToNtfsTime(entry.getLastModifiedDate())));
            }
        }
        out.flush();
        final byte[] contents = baos.toByteArray();
        writeUint64(header, contents.length);
        header.write(contents);
    }
}

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private void readFilesInfo(final DataInput header, final Archive archive) throws IOException {
    final long numFiles = readUint64(header);
    final SevenZArchiveEntry[] files = new SevenZArchiveEntry[(int) numFiles];
    for (int i = 0; i < files.length; i++) {
        files[i] = new SevenZArchiveEntry();
    }//from  ww  w  . j  a  v  a2s  .c om
    BitSet isEmptyStream = null;
    BitSet isEmptyFile = null;
    BitSet isAnti = null;
    while (true) {
        final int propertyType = header.readUnsignedByte();
        if (propertyType == 0) {
            break;
        }
        final long size = readUint64(header);
        switch (propertyType) {
        case NID.kEmptyStream: {
            isEmptyStream = readBits(header, files.length);
            break;
        }
        case NID.kEmptyFile: {
            if (isEmptyStream == null) { // protect against NPE
                throw new IOException("Header format error: kEmptyStream must appear before kEmptyFile");
            }
            isEmptyFile = readBits(header, isEmptyStream.cardinality());
            break;
        }
        case NID.kAnti: {
            if (isEmptyStream == null) { // protect against NPE
                throw new IOException("Header format error: kEmptyStream must appear before kAnti");
            }
            isAnti = readBits(header, isEmptyStream.cardinality());
            break;
        }
        case NID.kName: {
            final int external = header.readUnsignedByte();
            if (external != 0) {
                throw new IOException("Not implemented");
            }
            if (((size - 1) & 1) != 0) {
                throw new IOException("File names length invalid");
            }
            final byte[] names = new byte[(int) (size - 1)];
            header.readFully(names);
            int nextFile = 0;
            int nextName = 0;
            for (int i = 0; i < names.length; i += 2) {
                if (names[i] == 0 && names[i + 1] == 0) {
                    files[nextFile].setName(new String(names, nextName, i - nextName, CharsetNames.UTF_16LE));
                    nextName = i + 2;
                    this.mapFilename.put(files[nextFile].getName(), nextFile);
                    nextFile++;
                }
            }
            if (nextName != names.length || nextFile != files.length) {
                throw new IOException("Error parsing file names");
            }
            break;
        }
        case NID.kCTime: {
            final BitSet timesDefined = readAllOrBits(header, files.length);
            final int external = header.readUnsignedByte();
            if (external != 0) {
                throw new IOException("Unimplemented");
            }
            for (int i = 0; i < files.length; i++) {
                files[i].setHasCreationDate(timesDefined.get(i));
                if (files[i].getHasCreationDate()) {
                    files[i].setCreationDate(Long.reverseBytes(header.readLong()));
                }
            }
            break;
        }
        case NID.kATime: {
            final BitSet timesDefined = readAllOrBits(header, files.length);
            final int external = header.readUnsignedByte();
            if (external != 0) {
                throw new IOException("Unimplemented");
            }
            for (int i = 0; i < files.length; i++) {
                files[i].setHasAccessDate(timesDefined.get(i));
                if (files[i].getHasAccessDate()) {
                    files[i].setAccessDate(Long.reverseBytes(header.readLong()));
                }
            }
            break;
        }
        case NID.kMTime: {
            final BitSet timesDefined = readAllOrBits(header, files.length);
            final int external = header.readUnsignedByte();
            if (external != 0) {
                throw new IOException("Unimplemented");
            }
            for (int i = 0; i < files.length; i++) {
                files[i].setHasLastModifiedDate(timesDefined.get(i));
                if (files[i].getHasLastModifiedDate()) {
                    files[i].setLastModifiedDate(Long.reverseBytes(header.readLong()));
                }
            }
            break;
        }
        case NID.kWinAttributes: {
            final BitSet attributesDefined = readAllOrBits(header, files.length);
            final int external = header.readUnsignedByte();
            if (external != 0) {
                throw new IOException("Unimplemented");
            }
            for (int i = 0; i < files.length; i++) {
                files[i].setHasWindowsAttributes(attributesDefined.get(i));
                if (files[i].getHasWindowsAttributes()) {
                    files[i].setWindowsAttributes(Integer.reverseBytes(header.readInt()));
                }
            }
            break;
        }
        case NID.kStartPos: {
            throw new IOException("kStartPos is unsupported, please report");
        }
        case NID.kDummy: {
            // 7z 9.20 asserts the content is all zeros and ignores the property
            // Compress up to 1.8.1 would throw an exception, now we ignore it (see COMPRESS-287

            if (skipBytesFully(header, size) < size) {
                throw new IOException("Incomplete kDummy property");
            }
            break;
        }

        default: {
            // Compress up to 1.8.1 would throw an exception, now we ignore it (see COMPRESS-287
            if (skipBytesFully(header, size) < size) {
                throw new IOException("Incomplete property of type " + propertyType);
            }
            break;
        }
        }
    }
    int nonEmptyFileCounter = 0;
    int emptyFileCounter = 0;
    for (int i = 0; i < files.length; i++) {
        files[i].setHasStream(isEmptyStream == null ? true : !isEmptyStream.get(i));
        if (files[i].hasStream()) {
            files[i].setDirectory(false);
            files[i].setAntiItem(false);
            files[i].setHasCrc(archive.subStreamsInfo.hasCrc.get(nonEmptyFileCounter));
            files[i].setCrcValue(archive.subStreamsInfo.crcs[nonEmptyFileCounter]);
            files[i].setSize(archive.subStreamsInfo.unpackSizes[nonEmptyFileCounter]);
            ++nonEmptyFileCounter;
        } else {
            files[i].setDirectory(isEmptyFile == null ? true : !isEmptyFile.get(emptyFileCounter));
            files[i].setAntiItem(isAnti == null ? false : isAnti.get(emptyFileCounter));
            files[i].setHasCrc(false);
            files[i].setSize(0);

            ++emptyFileCounter;
        }
    }
    archive.files = files;
    calculateStreamMap(archive);
}

From source file:org.apache.hadoop.hbase.filter.DirectoryRowFilter.java

private static long hash24(long k0, long k1, byte[] data, int offset, int len, boolean reversed) {
    long v0 = 0x736f6d6570736575L ^ k0;
    long v1 = 0x646f72616e646f6dL ^ k1;
    long v2 = 0x6c7967656e657261L ^ k0;
    long v3 = 0x7465646279746573L ^ k1;
    long m;//w ww .ja  va2s .  c o  m
    int last = len / 8 * 8;

    int i = 0;

    // processing 8 bytes blocks in data
    while (i < last) {
        // pack a block to long, as LE 8 bytes
        /*
        m = (data[!reversed ? offset + i++ : offset + (len - 1) - i++] & 0xffL)
            | (data[!reversed ? offset + i++ : offset + (len - 1) - i++] & 0xffL) << 8
            | (data[!reversed ? offset + i++ : offset + (len - 1) - i++] & 0xffL) << 16
            | (data[!reversed ? offset + i++ : offset + (len - 1) - i++] & 0xffL) << 24
            | (data[!reversed ? offset + i++ : offset + (len - 1) - i++] & 0xffL) << 32
            | (data[!reversed ? offset + i++ : offset + (len - 1) - i++] & 0xffL) << 40
            | (data[!reversed ? offset + i++ : offset + (len - 1) - i++] & 0xffL) << 48
            | (data[!reversed ? offset + i++ : offset + (len - 1) - i++] & 0xffL) << 56;
        */

        if (!reversed) {
            m = UNSAFE.getLong(data, base + offset + i);
            //m = Long.reverseBytes(m);
        } else {
            m = UNSAFE.getLong(data, base + offset + len - 1 - (i + 7));
            m = Long.reverseBytes(m);
        }

        i += 8;

        // MSGROUND {
        v3 ^= m;

        /*
         * SIPROUND wih hand reordering
         * 
         * SIPROUND in siphash24.c:
         * A: v0 += v1;
         * B: v1=ROTL(v1,13);
         * C: v1 ^= v0;
         * D: v0=ROTL(v0,32);
         * E: v2 += v3;
         * F: v3=ROTL(v3,16);
         * G: v3 ^= v2;
         * H: v0 += v3;
         * I: v3=ROTL(v3,21);
         * J: v3 ^= v0;
         * K: v2 += v1;
         * L: v1=ROTL(v1,17);
         * M: v1 ^= v2;
         * N: v2=ROTL(v2,32);
         * 
         * Each dependency:
         * B -> A
         * C -> A, B
         * D -> C
         * F -> E
         * G -> E, F
         * H -> D, G
         * I -> H
         * J -> H, I
         * K -> C, G
         * L -> K
         * M -> K, L
         * N -> M
         * 
         * Dependency graph:
         * D -> C -> B -> A
         * G -> F -> E
         * J -> I -> H -> D, G
         * N -> M -> L -> K -> C, G
         * 
         * Resulting parallel friendly execution order:
         * -> ABCDHIJ
         * -> EFGKLMN
         */

        // SIPROUND {
        v0 += v1;
        v2 += v3;
        v1 = (v1 << 13) | v1 >>> 51;
        v3 = (v3 << 16) | v3 >>> 48;
        v1 ^= v0;
        v3 ^= v2;
        v0 = (v0 << 32) | v0 >>> 32;
        v2 += v1;
        v0 += v3;
        v1 = (v1 << 17) | v1 >>> 47;
        v3 = (v3 << 21) | v3 >>> 43;
        v1 ^= v2;
        v3 ^= v0;
        v2 = (v2 << 32) | v2 >>> 32;
        // }
        // SIPROUND {
        v0 += v1;
        v2 += v3;
        v1 = (v1 << 13) | v1 >>> 51;
        v3 = (v3 << 16) | v3 >>> 48;
        v1 ^= v0;
        v3 ^= v2;
        v0 = (v0 << 32) | v0 >>> 32;
        v2 += v1;
        v0 += v3;
        v1 = (v1 << 17) | v1 >>> 47;
        v3 = (v3 << 21) | v3 >>> 43;
        v1 ^= v2;
        v3 ^= v0;
        v2 = (v2 << 32) | v2 >>> 32;
        // }
        v0 ^= m;
        // }
    }

    // packing the last block to long, as LE 0-7 bytes + the length in the top
    // byte
    m = 0;
    for (i = len - 1; i >= last; --i) {
        m <<= 8;
        m |= data[!reversed ? offset + i : offset + (len - 1) - i] & 0xffL;
    }
    m |= (long) len << 56;

    // MSGROUND {
    v3 ^= m;
    // SIPROUND {
    v0 += v1;
    v2 += v3;
    v1 = (v1 << 13) | v1 >>> 51;
    v3 = (v3 << 16) | v3 >>> 48;
    v1 ^= v0;
    v3 ^= v2;
    v0 = (v0 << 32) | v0 >>> 32;
    v2 += v1;
    v0 += v3;
    v1 = (v1 << 17) | v1 >>> 47;
    v3 = (v3 << 21) | v3 >>> 43;
    v1 ^= v2;
    v3 ^= v0;
    v2 = (v2 << 32) | v2 >>> 32;
    // }
    // SIPROUND {
    v0 += v1;
    v2 += v3;
    v1 = (v1 << 13) | v1 >>> 51;
    v3 = (v3 << 16) | v3 >>> 48;
    v1 ^= v0;
    v3 ^= v2;
    v0 = (v0 << 32) | v0 >>> 32;
    v2 += v1;
    v0 += v3;
    v1 = (v1 << 17) | v1 >>> 47;
    v3 = (v3 << 21) | v3 >>> 43;
    v1 ^= v2;
    v3 ^= v0;
    v2 = (v2 << 32) | v2 >>> 32;
    // }
    v0 ^= m;
    // }

    // finishing...
    v2 ^= 0xff;
    // SIPROUND {
    v0 += v1;
    v2 += v3;
    v1 = (v1 << 13) | v1 >>> 51;
    v3 = (v3 << 16) | v3 >>> 48;
    v1 ^= v0;
    v3 ^= v2;
    v0 = (v0 << 32) | v0 >>> 32;
    v2 += v1;
    v0 += v3;
    v1 = (v1 << 17) | v1 >>> 47;
    v3 = (v3 << 21) | v3 >>> 43;
    v1 ^= v2;
    v3 ^= v0;
    v2 = (v2 << 32) | v2 >>> 32;
    // }
    // SIPROUND {
    v0 += v1;
    v2 += v3;
    v1 = (v1 << 13) | v1 >>> 51;
    v3 = (v3 << 16) | v3 >>> 48;
    v1 ^= v0;
    v3 ^= v2;
    v0 = (v0 << 32) | v0 >>> 32;
    v2 += v1;
    v0 += v3;
    v1 = (v1 << 17) | v1 >>> 47;
    v3 = (v3 << 21) | v3 >>> 43;
    v1 ^= v2;
    v3 ^= v0;
    v2 = (v2 << 32) | v2 >>> 32;
    // }
    // SIPROUND {
    v0 += v1;
    v2 += v3;
    v1 = (v1 << 13) | v1 >>> 51;
    v3 = (v3 << 16) | v3 >>> 48;
    v1 ^= v0;
    v3 ^= v2;
    v0 = (v0 << 32) | v0 >>> 32;
    v2 += v1;
    v0 += v3;
    v1 = (v1 << 17) | v1 >>> 47;
    v3 = (v3 << 21) | v3 >>> 43;
    v1 ^= v2;
    v3 ^= v0;
    v2 = (v2 << 32) | v2 >>> 32;
    // }
    // SIPROUND {
    v0 += v1;
    v2 += v3;
    v1 = (v1 << 13) | v1 >>> 51;
    v3 = (v3 << 16) | v3 >>> 48;
    v1 ^= v0;
    v3 ^= v2;
    v0 = (v0 << 32) | v0 >>> 32;
    v2 += v1;
    v0 += v3;
    v1 = (v1 << 17) | v1 >>> 47;
    v3 = (v3 << 21) | v3 >>> 43;
    v1 ^= v2;
    v3 ^= v0;
    v2 = (v2 << 32) | v2 >>> 32;
    // }
    return v0 ^ v1 ^ v2 ^ v3;
}