Example usage for java.lang Integer reverseBytes

List of usage examples for java.lang Integer reverseBytes

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static int reverseBytes(int i) 

Source Link

Document

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

Usage

From source file:org.apache.kylin.common.util.Bytes.java

/**
 * Put an int value out to the specified byte array position (Unsafe).
 *
 * @param bytes  the byte array/*from w w  w.j ava2 s  .co m*/
 * @param offset position in the array
 * @param val    int to write out
 * @return incremented offset
 */
public static int putIntUnsafe(byte[] bytes, int offset, int val) {
    if (org.apache.kylin.common.util.Bytes.LexicographicalComparerHolder.UnsafeComparer.littleEndian) {
        val = Integer.reverseBytes(val);
    }
    org.apache.kylin.common.util.Bytes.LexicographicalComparerHolder.UnsafeComparer.theUnsafe.putInt(bytes,
            (long) offset
                    + org.apache.kylin.common.util.Bytes.LexicographicalComparerHolder.UnsafeComparer.BYTE_ARRAY_BASE_OFFSET,
            val);
    return offset + SIZEOF_INT;
}

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   w  w w  . j  a  v a2  s . c o  m*/
    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:com.zpci.firstsignhairclipdemo.MainActivity.java

public void savewavefile(byte[] ra) {
    //prepend 44 byte wave header to data

    int sampleRate = 8000; // audio sample rate is 8000 SPS
    int numSecs = ra.length / sampleRate; // number of seconds of audio to record
    int samples = sampleRate * numSecs; // number of samples in file
    short bitsPerSample = 8; // one byte per sample
    int filesize = samples + 44; // check this?
    int fmtChunkSize = 16; // size of 'fmt' chunk
    short channels = 1; // mono
    int byteRate = sampleRate * channels * bitsPerSample / 8; // will be 8K for us
    short format = 1; // 1 == uncompressed pcm
    short blockalign = (short) (channels * bitsPerSample / 8); // bytes per sample
    int audiolen = samples * channels * bitsPerSample / 8; // length of audio in bytes

    try {/*from ww w  .j  a v  a  2  s  . c  o m*/
        //OutputStream os = openFileOutput("diagaudio.wav", Context.MODE_PRIVATE);
        String state = Environment.getExternalStorageState();
        Log.d(TAG, "External storage state: " + state);
        if (Environment.MEDIA_MOUNTED.equals(state)) {

            //create firstsign directory
            File rootPath = new File(Environment.getExternalStorageDirectory(), "firstsign");
            if (!rootPath.exists()) {
                rootPath.mkdirs();
                Log.d(TAG, "mkdirs");
            }
            File file = new File(rootPath, "hairclipaudio.wav");
            file.createNewFile();
            OutputStream os = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            DataOutputStream wf = new DataOutputStream(bos);

            wf.write("RIFF".getBytes());
            wf.writeInt(Integer.reverseBytes(filesize - 8));
            wf.write("WAVE".getBytes());
            wf.write("fmt ".getBytes());
            wf.writeInt(Integer.reverseBytes(fmtChunkSize));
            wf.writeShort(Short.reverseBytes(format));
            wf.writeShort(Short.reverseBytes(channels));
            wf.writeInt(Integer.reverseBytes(sampleRate));
            wf.writeInt(Integer.reverseBytes(byteRate));
            wf.writeShort(Short.reverseBytes(blockalign));
            wf.writeShort(Short.reverseBytes(bitsPerSample));
            wf.write("data".getBytes());
            wf.writeInt(Integer.reverseBytes(audiolen));
            wf.write(ra);

            wf.close();
            bos.close();
            os.close();

            Log.d(TAG, "wavefile write complete");
        } else {
            Toast.makeText(this, "SDCard not mounted", Toast.LENGTH_LONG).show();
        } //what do i do?

    } catch (Exception e) {
        Log.e(TAG, "exception in savewavefile");
        e.printStackTrace();
    }

}