Example usage for com.google.common.io ByteStreams newDataInput

List of usage examples for com.google.common.io ByteStreams newDataInput

Introduction

In this page you can find the example usage for com.google.common.io ByteStreams newDataInput.

Prototype

public static ByteArrayDataInput newDataInput(byte[] bytes, int start) 

Source Link

Document

Returns a new ByteArrayDataInput instance to read from the bytes array, starting at the given position.

Usage

From source file:de.nx42.maps4cim.header.HeaderParser.java

/**
 * Actually parses the relevant part of the header and writes the results
 * into a new CustomHeader-object/*from   w  ww . j  av a 2  s  . c  o m*/
 * @param header a byte-array containing at least the relevant part of the
 * map's header (can be retrieved via {@link HeaderParser#getRelevantPart(InputStream)})
 * @return the CustomHeader-object containing the data of this header
 * @throws ParseException if there is an error parsing the header
 */
protected static CustomHeader execute(byte[] header) throws ParseException {
    CustomHeader ch = CustomHeader.newEmpty();

    // read intro
    int introEnd = readToString(header, 0);
    ch.intro = Arrays.copyOfRange(header, 0, introEnd);

    // read date/times
    int dateStart = readAfterGap(header, introEnd, 3);
    int dateEnd = readAfterBytes(header, dateStart, CustomHeader.staticBinary01)
            - CustomHeader.staticBinary01.length;

    // read 64 bit integers (date/time) until end is reached
    int dateAmount = (dateEnd - dateStart) / 8;
    long[] dateTimeStamps = new long[dateAmount];
    ByteArrayDataInput bin = ByteStreams.newDataInput(header, dateStart);

    for (int i = 0; i < dateAmount; i++) {
        dateTimeStamps[i] = bin.readLong();
    }

    // interpret dates
    if (dateAmount >= 6) {
        // current format (custom map)
        ch.unusedDate1 = DateUtils.ticksToDate(dateTimeStamps[0]);
        ch.unusedDate2 = DateUtils.ticksToDate(dateTimeStamps[1]);
        ch.lastSaved = DateUtils.ticksToDate(dateTimeStamps[2]);
        ch.mapCreated = DateUtils.ticksToDate(dateTimeStamps[3]);
        ch.workTime1 = dateTimeStamps[4];
        ch.workTime2 = dateTimeStamps[5];
    } else if (dateAmount >= 4) {
        // old format (campaign map), without mapCreated and workTime2
        ch.unusedDate1 = DateUtils.ticksToDate(dateTimeStamps[0]);
        ch.unusedDate2 = DateUtils.ticksToDate(dateTimeStamps[1]);
        ch.lastSaved = ch.mapCreated = DateUtils.ticksToDate(dateTimeStamps[2]);
        ch.workTime1 = ch.workTime2 = dateTimeStamps[3];
    } else {
        // fail, just write some default values
        log.warn("Can't read date & time values: unexpected format");
        ch.unusedDate1 = ch.unusedDate2 = CustomHeader.unusedDateDefault;
        ch.lastSaved = ch.mapCreated = DateUtils.getDateUTC(2013, 1, 1, 12, 0, 0);
    }

    // read map name
    int nameStart = readAfterString(header, dateEnd, CustomHeader.staticString02);
    ch.mapName = parseHeaderString(header, nameStart);

    // get minimap PNG length (watch out for euro update strings!)
    int mapNameEnd = readAfterString(header, nameStart);
    int beginPngLen = mapNameEnd;
    int firstStringAfterMapName = readToString(header, mapNameEnd);
    if (mapNameEnd == firstStringAfterMapName) {
        beginPngLen = readAfterString(header, firstStringAfterMapName);
    }

    // read minimap image
    ch.pngLength = Arrays.copyOfRange(header, beginPngLen, beginPngLen + 3);
    int beginPng = beginPngLen + 3;
    int pngLength = int24parse(ch.pngLength);
    int pngEnd = beginPng + pngLength;
    ch.png = Arrays.copyOfRange(header, beginPng, pngEnd);

    // find out if euro building style
    int firstStringAfterPng = readToString(header, pngEnd);

    String europeIdentifier = parseHeaderString(header, firstStringAfterPng);
    ch.buildingSet = europeIdentifier.contains("cim2.europe") ? BuildingSet.EUROPEAN : BuildingSet.AMERICAN;

    return ch;
}