Search in sources :

Example 1 with LittleEndianDataInputStream

use of com.google.common.io.LittleEndianDataInputStream in project BIMserver by opensourceBIM.

the class ClientIfcModel method processGeometryInputStream.

private void processGeometryInputStream(InputStream inputStream, Map<Long, Long> geometryInfoOidToOid) throws IOException, GeometryException, IfcModelInterfaceException {
    try (LittleEndianDataInputStream dataInputStream = new LittleEndianDataInputStream(inputStream)) {
        boolean done = false;
        while (!done) {
            byte type = dataInputStream.readByte();
            if (type == 0) {
                String protocol = dataInputStream.readUTF();
                if (!protocol.equals("BGS")) {
                    throw new GeometryException("Protocol != BGS (" + protocol + ")");
                }
                byte formatVersion = dataInputStream.readByte();
                if (formatVersion != 11) {
                    throw new GeometryException("Unsupported version " + formatVersion + " / 11");
                }
                int skip = 4 - (7 % 4);
                if (skip != 0 && skip != 4) {
                    dataInputStream.readFully(new byte[skip]);
                }
                for (int i = 0; i < 6; i++) {
                    dataInputStream.readDouble();
                }
            } else if (type == 5) {
                dataInputStream.readFully(new byte[7]);
                // roid
                dataInputStream.readLong();
                long geometryInfoOid = dataInputStream.readLong();
                GeometryInfo geometryInfo = (GeometryInfo) get(geometryInfoOid);
                if (geometryInfo == null) {
                    geometryInfo = create(GeometryInfo.class);
                }
                ((IdEObjectImpl) geometryInfo).setOid(geometryInfoOid);
                ((IdEObjectImpl) geometryInfo).setLoadingState(State.LOADING);
                add(geometryInfoOid, geometryInfo);
                Long ifcProductOid = geometryInfoOidToOid.get(geometryInfoOid);
                if (ifcProductOid == null) {
                    throw new GeometryException("Missing geometry info id: " + geometryInfoOid);
                }
                IdEObject ifcProduct = get(ifcProductOid);
                EStructuralFeature geometryFeature = getPackageMetaData().getEClass("IfcProduct").getEStructuralFeature("geometry");
                ifcProduct.eSet(geometryFeature, geometryInfo);
                org.bimserver.models.geometry.Vector3f minBounds = GeometryFactory.eINSTANCE.createVector3f();
                minBounds.setX(dataInputStream.readDouble());
                minBounds.setY(dataInputStream.readDouble());
                minBounds.setZ(dataInputStream.readDouble());
                org.bimserver.models.geometry.Vector3f maxBounds = GeometryFactory.eINSTANCE.createVector3f();
                maxBounds.setX(dataInputStream.readDouble());
                maxBounds.setY(dataInputStream.readDouble());
                maxBounds.setZ(dataInputStream.readDouble());
                geometryInfo.setMinBounds(minBounds);
                geometryInfo.setMaxBounds(maxBounds);
                byte[] transformation = new byte[16 * 8];
                dataInputStream.readFully(transformation);
                geometryInfo.setTransformation(transformation);
                long geometryDataOid = dataInputStream.readLong();
                GeometryData geometryData = (GeometryData) get(geometryDataOid);
                if (geometryData == null) {
                    geometryData = GeometryFactory.eINSTANCE.createGeometryData();
                    add(geometryDataOid, geometryData);
                }
                geometryInfo.setData(geometryData);
                ((IdEObjectImpl) geometryData).setLoadingState(State.LOADED);
            } else if (type == 3) {
                throw new GeometryException("Parts not supported");
            } else if (type == 1) {
                dataInputStream.readFully(new byte[7]);
                long geometryDataOid = dataInputStream.readLong();
                GeometryData geometryData = (GeometryData) get(geometryDataOid);
                if (geometryData == null) {
                    geometryData = GeometryFactory.eINSTANCE.createGeometryData();
                    add(geometryDataOid, geometryData);
                }
                ((IdEObjectImpl) geometryData).setOid(geometryDataOid);
                ((IdEObjectImpl) geometryData).setLoadingState(State.LOADING);
                int nrIndices = dataInputStream.readInt();
                byte[] indices = new byte[nrIndices * 4];
                dataInputStream.readFully(indices);
                geometryData.setIndices(indices);
                int colorType = dataInputStream.readInt();
                if (colorType == 1) {
                    dataInputStream.readFloat();
                    dataInputStream.readFloat();
                    dataInputStream.readFloat();
                    dataInputStream.readFloat();
                }
                int nrVertices = dataInputStream.readInt();
                byte[] vertices = new byte[nrVertices * 4];
                dataInputStream.readFully(vertices);
                geometryData.setVertices(vertices);
                int nrNormals = dataInputStream.readInt();
                byte[] normals = new byte[nrNormals * 4];
                dataInputStream.readFully(normals);
                geometryData.setNormals(normals);
                int nrMaterials = dataInputStream.readInt();
                byte[] materials = new byte[nrMaterials * 4];
                dataInputStream.readFully(materials);
                geometryData.setMaterials(materials);
                ((IdEObjectImpl) geometryData).setLoadingState(State.LOADED);
            } else if (type == 6) {
                done = true;
            } else {
                throw new GeometryException("Unimplemented type: " + type);
            }
        }
    } catch (EOFException e) {
    // 
    } catch (ObjectAlreadyExistsException e) {
        e.printStackTrace();
    }
}
Also used : IdEObject(org.bimserver.emf.IdEObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) GeometryData(org.bimserver.models.geometry.GeometryData) LittleEndianDataInputStream(com.google.common.io.LittleEndianDataInputStream) GeometryInfo(org.bimserver.models.geometry.GeometryInfo) EOFException(java.io.EOFException) ObjectAlreadyExistsException(org.bimserver.plugins.ObjectAlreadyExistsException)

Example 2 with LittleEndianDataInputStream

use of com.google.common.io.LittleEndianDataInputStream in project ultimate-java by pantinor.

the class TestJaxb method testReadSaveGame.

// @Test
public void testReadSaveGame() throws Exception {
    InputStream is = new FileInputStream(Constants.PARTY_SAV_BASE_FILENAME);
    LittleEndianDataInputStream dis = new LittleEndianDataInputStream(is);
    SaveGame sg = new SaveGame();
    sg.read(dis);
    // SaveGame.SaveGamePlayerRecord avatar = sg.new SaveGamePlayerRecord();
    // avatar.name = "paul";
    // avatar.hp = 199;
    // 
    // sg.food = 30000;
    // sg.gold = 200;
    // sg.reagents[Reagent.GINSENG.ordinal()] = 3;
    // sg.reagents[Reagent.GARLIC.ordinal()] = 4;
    // sg.reagents[Reagent.NIGHTSHADE.ordinal()] = 9;
    // sg.reagents[Reagent.MANDRAKE.ordinal()] = 6;
    // sg.torches = 2;
    // 
    // sg.players[0] = avatar;
    // 
    // sg.write(Constants.PARTY_SAV_BASE_FILENAME);
    Party p = new Party(sg);
    // for (int i=0;i<8;i++)
    // System.err.println(Virtue.get(i) + " " + sg.karma[i]);
    // System.err.println("---------------");
    p.adjustKarma(KarmaAction.ATTACKED_GOOD);
    // System.err.println(Virtue.get(i) + " " + sg.karma[i]);
    for (int i = 0; i < 8; i++) {
        Virtue v = Constants.Virtue.get(i);
        String st = ((sg.stones & (1 << i)) > 0 ? "+STONE" : "");
        String ru = ((sg.runes & (1 << i)) > 0 ? "+RUNE" : "");
    // System.err.println(v + " " + st + " " + ru);
    }
    System.err.println("---------------");
    sg.runes |= Virtue.HUMILITY.getLoc();
    for (int i = 0; i < 8; i++) {
        Virtue v = Constants.Virtue.get(i);
        String st = ((sg.stones & (1 << i)) > 0 ? "+STONE" : "");
        String ru = ((sg.runes & (1 << i)) > 0 ? "+RUNE" : "");
    // System.err.println(v + " " + st + " " + ru);
    }
    sg.items |= Item.BELL.getLoc();
    for (Item item : Constants.Item.values()) {
    // System.err.println((sg.items & (1 << item.ordinal())) > 0 ? item.getDesc() : "") ;
    }
    sg.items |= Item.PARCH.getLoc();
    for (Item item : Constants.Item.values()) {
        System.err.println((sg.items & item.getLoc()) > 0 ? item.getDesc() : "");
    }
    sg.lastrage = 1;
    sg.write("test.sav");
}
Also used : Item(ultima.Constants.Item) Party(objects.Party) LittleEndianDataInputStream(com.google.common.io.LittleEndianDataInputStream) SaveGame(objects.SaveGame) LittleEndianDataInputStream(com.google.common.io.LittleEndianDataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Virtue(ultima.Constants.Virtue) FileInputStream(java.io.FileInputStream)

Example 3 with LittleEndianDataInputStream

use of com.google.common.io.LittleEndianDataInputStream in project ultimate-java by pantinor.

the class SaveGame method read.

public void read(String strFilePath) throws Exception {
    InputStream is;
    LittleEndianDataInputStream dis = null;
    try {
        is = new FileInputStream(Gdx.files.internal(strFilePath).file());
        dis = new LittleEndianDataInputStream(is);
    } catch (Exception e) {
        throw new Exception("Could not read save game file.");
    // is = this.getClass().getResourceAsStream("/data/" + PARTY_SAV_BASE_FILENAME);
    // dis = new LittleEndianDataInputStream(is);
    }
    read(dis);
}
Also used : LittleEndianDataInputStream(com.google.common.io.LittleEndianDataInputStream) LittleEndianDataInputStream(com.google.common.io.LittleEndianDataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream)

Example 4 with LittleEndianDataInputStream

use of com.google.common.io.LittleEndianDataInputStream in project tutorials by eugenp.

the class GuavaIOUnitTest method whenWriteReadLittleEndian_thenCorrect.

@Test
public void whenWriteReadLittleEndian_thenCorrect() throws IOException {
    final int value = 100;
    final LittleEndianDataOutputStream writer = new LittleEndianDataOutputStream(new FileOutputStream("src/test/resources/test_le.txt"));
    writer.writeInt(value);
    writer.close();
    final LittleEndianDataInputStream reader = new LittleEndianDataInputStream(new DataInputStream(new FileInputStream("src/test/resources/test_le.txt")));
    final int result = reader.readInt();
    reader.close();
    assertEquals(value, result);
}
Also used : LittleEndianDataInputStream(com.google.common.io.LittleEndianDataInputStream) FileOutputStream(java.io.FileOutputStream) LittleEndianDataOutputStream(com.google.common.io.LittleEndianDataOutputStream) DataInputStream(java.io.DataInputStream) LittleEndianDataInputStream(com.google.common.io.LittleEndianDataInputStream) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 5 with LittleEndianDataInputStream

use of com.google.common.io.LittleEndianDataInputStream in project android-classyshark by google.

the class XmlDecompressor method decompressXml.

public String decompressXml(InputStream is) throws IOException {
    StringBuilder result = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    try (LittleEndianDataInputStream dis = new LittleEndianDataInputStream(is)) {
        // Getting and checking the marker for a valid XML file
        int fileMarker = dis.readInt();
        if (fileMarker != PACKED_XML_IDENTIFIER) {
            throw new IOException(String.format(ERROR_INVALID_MAGIC_NUMBER, PACKED_XML_IDENTIFIER, fileMarker));
        }
        dis.skipBytes(4);
        List<String> packedStrings = parseStrings(dis);
        int ident = 0;
        int tag = dis.readShort();
        do {
            int headerSize = dis.readShort();
            int chunkSize = dis.readInt();
            switch(tag) {
                case RES_XML_FIRST_CHUNK_TYPE:
                    {
                        dis.skipBytes(chunkSize - 8);
                        break;
                    }
                case RES_XML_RESOURCE_MAP_TYPE:
                    {
                        dis.skipBytes(chunkSize - 8);
                        break;
                    }
                case START_ELEMENT_TAG:
                    {
                        parseStartTag(result, dis, packedStrings, ident);
                        ident++;
                        break;
                    }
                case END_ELEMENT_TAG:
                    {
                        ident--;
                        parseEndTag(result, dis, packedStrings, ident);
                        break;
                    }
                case CDATA_TAG:
                    {
                        parseCDataTag(result, dis, packedStrings, ident);
                        break;
                    }
                default:
                    System.err.println(String.format(ERROR_UNKNOWN_TAG, tag));
            }
            tag = dis.readShort();
        } while (tag != END_DOC_TAG);
        return result.toString();
    }
}
Also used : LittleEndianDataInputStream(com.google.common.io.LittleEndianDataInputStream) IOException(java.io.IOException)

Aggregations

LittleEndianDataInputStream (com.google.common.io.LittleEndianDataInputStream)5 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)2 LittleEndianDataOutputStream (com.google.common.io.LittleEndianDataOutputStream)1 DataInputStream (java.io.DataInputStream)1 EOFException (java.io.EOFException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Party (objects.Party)1 SaveGame (objects.SaveGame)1 IdEObject (org.bimserver.emf.IdEObject)1 GeometryData (org.bimserver.models.geometry.GeometryData)1 GeometryInfo (org.bimserver.models.geometry.GeometryInfo)1 ObjectAlreadyExistsException (org.bimserver.plugins.ObjectAlreadyExistsException)1 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)1 Test (org.junit.Test)1 Item (ultima.Constants.Item)1 Virtue (ultima.Constants.Virtue)1