Example usage for java.nio ByteBuffer getInt

List of usage examples for java.nio ByteBuffer getInt

Introduction

In this page you can find the example usage for java.nio ByteBuffer getInt.

Prototype

public abstract int getInt(int index);

Source Link

Document

Returns the int at the specified index.

Usage

From source file:com.codestation.henkakuserver.HenkakuServer.java

private Pair<ArrayList<Integer>, List<Byte>> preprocessRop(byte[] urop) throws Exception {

    byte[] loader = new byte[urop.length + ((-urop.length) & 3)];
    System.arraycopy(urop, 0, loader, 0, urop.length);

    ByteBuffer buf = ByteBuffer.wrap(loader).order(ByteOrder.LITTLE_ENDIAN);

    int header_size = 0x40;

    int dsize = buf.getInt(0x10);
    int csize = buf.getInt(0x20);
    int reloc_size = buf.getInt(0x30);
    int symtab_size = buf.getInt(0x38);

    if (csize % 4 != 0) {
        throw new Exception("csize % 4 != 0???");
    }//from ww w  . j  av  a  2  s. co m

    int reloc_offset = header_size + dsize + csize;
    int symtab = reloc_offset + reloc_size;
    int symtab_n = symtab_size / 8;

    Map<Integer, String> reloc_map = new HashMap<>();

    for (int x = 0; x < symtab_n; ++x) {
        int sym_id = buf.getInt(symtab + 8 * x);
        int str_offset = buf.getInt(symtab + 8 * x + 4);
        int end = str_offset;

        while (loader[end] != 0) {
            end += 1;
        }

        String name = new String(Arrays.copyOfRange(loader, str_offset, end));
        reloc_map.put(sym_id, name);
    }

    Map<Pair<String, Integer>, Integer> reloc_type_map = new HashMap<>();

    reloc_type_map.put(new Pair<>("rop.data", 0), 1);
    reloc_type_map.put(new Pair<>("SceWebKit", 0), 2);
    reloc_type_map.put(new Pair<>("SceLibKernel", 0), 3);
    reloc_type_map.put(new Pair<>("SceLibc", 0), 4);
    reloc_type_map.put(new Pair<>("SceLibHttp", 0), 5);
    reloc_type_map.put(new Pair<>("SceNet", 0), 6);
    reloc_type_map.put(new Pair<>("SceAppMgr", 0), 7);

    int want_len = 0x40 + dsize + csize;

    ArrayList<Integer> urop_js = new ArrayList<>();
    byte[] relocs = new byte[want_len / 4];

    int reloc_n = reloc_size / 8;
    for (int x = 0; x < reloc_n; ++x) {
        int reloc_type = buf.getShort(reloc_offset + 8 * x);
        int sym_id = buf.getShort(reloc_offset + 8 * x + 2);
        int offset = buf.getInt(reloc_offset + 8 * x + 4);

        if (offset % 4 != 0) {
            throw new Exception("offset % 4 != 0???");
        }

        if (relocs[offset / 4] != 0) {
            throw new Exception("symbol relocated twice, not supported");
        }

        Integer wk_reloc_type = reloc_type_map.get(new Pair<>(reloc_map.get(sym_id), reloc_type));

        if (wk_reloc_type == null) {
            throw new Exception("unsupported relocation type");
        }

        relocs[offset / 4] = wk_reloc_type.byteValue();
    }

    for (int x = 0; x < want_len; x += 4) {
        urop_js.add(buf.getInt(x));
    }

    List<Byte> relocsArray = Arrays.asList(ArrayUtils.toObject(relocs));

    return new Pair<>(urop_js, relocsArray);
}

From source file:org.kalypso.grid.BinaryGeoGrid.java

/**
 * Gets the statistically values of this grid.
 * /*from   w  ww .j a  va  2  s. c o m*/
 * @throws IOException
 *           If the file position is not valid.
 */
private void readStatistically() throws IOException {
    final long pos = BinaryGeoGridHeader.HEADER_SIZE + getSizeX() * getSizeY() * 4;

    final ByteBuffer buffer = ByteBuffer.allocate(8);

    m_channel.position(pos);
    m_channel.read(buffer);

    m_unscaledMin = buffer.getInt(0);
    m_unscaledMax = buffer.getInt(4);
}

From source file:org.getspout.spout.packet.PacketBlockData.java

public void run(int playerId) {
    if (data != null) {
        ByteBuffer result = ByteBuffer.allocate(data.length).put(data);
        for (int i = 0; i < data.length / 15; i++) {
            int index = i * 15;
            int id = result.get(index);
            short rawData = result.get(index + 1);
            Block block = MaterialData.getBlock(id, rawData);
            if (block != null) {
                block.setHardness(result.getFloat(index + 2));
                block.setLightLevel(result.getInt(index + 6));
                block.setFriction(result.getFloat(index + 10));
                block.setOpaque(result.get(index + 14) != 0);
            }//  w  w w. j a  va2 s  .com
        }
    }
}

From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffReader.java

private void readIndexMap() throws IOException, MMException {
    long offset = readOffsetHeaderAndOffset(MultipageTiffWriter.INDEX_MAP_OFFSET_HEADER, 8);
    ByteBuffer header = readIntoBuffer(offset, 8);
    if (header.getInt(0) != MultipageTiffWriter.INDEX_MAP_HEADER) {
        throw new MMException("Error reading index map header");
    }/*from   www.  j  av a  2s . c  om*/
    int numMappings = header.getInt(4);
    indexMap_ = new HashMap<String, Long>();
    ByteBuffer mapBuffer = readIntoBuffer(offset + 8, 20 * numMappings);
    for (int i = 0; i < numMappings; i++) {
        int channel = mapBuffer.getInt(i * 20);
        int slice = mapBuffer.getInt(i * 20 + 4);
        int frame = mapBuffer.getInt(i * 20 + 8);
        int position = mapBuffer.getInt(i * 20 + 12);
        long imageOffset = unsignInt(mapBuffer.getInt(i * 20 + 16));
        indexMap_.put(MDUtils.generateLabel(channel, slice, frame, position), imageOffset);
    }
}

From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffReader.java

private JSONObject readComments() {
    try {//from www .  j av a  2s  . c  om
        long offset = readOffsetHeaderAndOffset(MultipageTiffWriter.COMMENTS_OFFSET_HEADER, 24);
        ByteBuffer header = readIntoBuffer(offset, 8);
        if (header.getInt(0) != MultipageTiffWriter.COMMENTS_HEADER) {
            ReportingUtils.logError("Can't find image comments in file: " + file_.getName());
            return null;
        }
        ByteBuffer buffer = readIntoBuffer(offset + 8, header.getInt(4));
        return new JSONObject(getString(buffer));
    } catch (Exception ex) {
        ReportingUtils.logError("Can't find image comments in file: " + file_.getName());
        return null;
    }
}

From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffReader.java

private JSONArray readDisplaySettings() {
    try {/*  w ww  . ja  v  a  2s . c o  m*/
        long offset = readOffsetHeaderAndOffset(MultipageTiffWriter.DISPLAY_SETTINGS_OFFSET_HEADER, 16);
        ByteBuffer header = readIntoBuffer(offset, 8);
        if (header.getInt(0) != MultipageTiffWriter.DISPLAY_SETTINGS_HEADER) {
            ReportingUtils.logError("Can't find display settings in file: " + file_.getName());
            return null;
        }
        ByteBuffer buffer = readIntoBuffer(offset + 8, header.getInt(4));
        return new JSONArray(getString(buffer));
    } catch (Exception ex) {
        ReportingUtils.logError("Can't find display settings in file: " + file_.getName());
        return null;
    }
}

From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffReader.java

private long readOffsetHeaderAndOffset(int offsetHeaderVal, int startOffset) throws IOException {
    ByteBuffer buffer1 = readIntoBuffer(startOffset, 8);
    int offsetHeader = buffer1.getInt(0);
    if (offsetHeader != offsetHeaderVal) {
        throw new IOException(
                "Offset header incorrect, expected: " + offsetHeaderVal + "   found: " + offsetHeader);
    }/*from  ww  w .j  a v  a2s .c o m*/
    return unsignInt(buffer1.getInt(4));
}

From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffReader.java

private JSONObject readSummaryMD() {
    try {//from  w w  w.java2s. c om
        ByteBuffer mdInfo = ByteBuffer.allocate(8).order(byteOrder_);
        fileChannel_.read(mdInfo, 32);
        int header = mdInfo.getInt(0);
        int length = mdInfo.getInt(4);

        if (header != MultipageTiffWriter.SUMMARY_MD_HEADER) {
            ReportingUtils.logError("Summary Metadata Header Incorrect");
            return null;
        }

        ByteBuffer mdBuffer = ByteBuffer.allocate(length);
        fileChannel_.read(mdBuffer, 40);
        JSONObject summaryMD = new JSONObject(getString(mdBuffer));

        //Summary MD written start of acquisition and never changed, this code makes sure acquisition comment
        //field is current
        if (displayAndComments_ != null && displayAndComments_.has("Comments")
                && displayAndComments_.getJSONObject("Comments").has("Summary")) {
            summaryMD.put("Comment", displayAndComments_.getJSONObject("Comments").getString("Summary"));
        }
        return summaryMD;
    } catch (Exception ex) {
        ReportingUtils.showError("Couldn't read summary Metadata from file: " + file_.getName());
        return null;
    }
}

From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffReader.java

private IFDEntry readDirectoryEntry(int offset, ByteBuffer buffer) throws IOException {
    char tag = buffer.getChar(offset);
    char type = buffer.getChar(offset + 2);
    long count = unsignInt(buffer.getInt(offset + 4));
    long value;//from   w  ww  . ja va 2 s .c  om
    if (type == 3 && count == 1) {
        value = buffer.getChar(offset + 8);
    } else {
        value = unsignInt(buffer.getInt(offset + 8));
    }
    return (new IFDEntry(tag, type, count, value));
}

From source file:guru.benson.pinch.Pinch.java

/**
 * Get a {@link java.net.HttpURLConnection} that has its {@link java.io.InputStream} pointing at
 * the file data of the given {@link guru.benson.pinch.ExtendedZipEntry}.
 *
 * @throws IOException//w  w w. j  av a  2  s.c o  m
 */
private HttpURLConnection getEntryInputStream(ExtendedZipEntry entry) throws IOException {
    HttpURLConnection conn;
    InputStream is;

    // Define the local header range
    long start = entry.getOffset();
    long end = start + ZipConstants.LOCHDR;

    conn = openConnection();
    conn.setRequestProperty("Range", "bytes=" + start + "-" + end);
    conn.setInstanceFollowRedirects(true);
    conn.connect();

    int responseCode = conn.getResponseCode();
    if (responseCode != HttpURLConnection.HTTP_PARTIAL) {
        throw new IOException("Unexpected HTTP server response: " + responseCode);
    }

    byte[] dataBuffer = new byte[2048];
    int read, bytes = 0;

    is = conn.getInputStream();
    while ((read = is.read(dataBuffer)) != -1) {
        bytes += read;
    }
    close(is);
    disconnect(conn);
    if (bytes < ZipConstants.LOCHDR) {
        throw new IOException("Unable to fetch the local header");
    }

    ByteBuffer buffer = ByteBuffer.allocate(ZipConstants.LOCHDR);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.put(dataBuffer, 0, ZipConstants.LOCHDR);

    final int headerSignature = buffer.getInt(0);
    if (headerSignature != 0x04034b50) {
        disconnect(conn);
        throw new IOException("Local file header signature mismatch");
    }

    final int localCompressedSize = buffer.getInt(ZipConstants.LOCSIZ);
    final short localFileNameLength = buffer.getShort(ZipConstants.LOCNAM);
    final short localExtraLength = buffer.getShort(ZipConstants.LOCEXT);

    // Define the local file range
    start = entry.getOffset() + ZipConstants.LOCHDR + localFileNameLength + localExtraLength;
    end = start + localCompressedSize;

    // Open a new one with
    conn = openConnection();
    conn.setRequestProperty("Range", "bytes=" + start + "-" + end);
    conn.setInstanceFollowRedirects(true);
    conn.connect();

    responseCode = conn.getResponseCode();
    if (responseCode != HttpURLConnection.HTTP_PARTIAL) {
        disconnect(conn);
        close(is);
        throw new IOException("Unexpected HTTP server response: " + responseCode);
    }

    return conn;
}