Example usage for com.google.common.io LittleEndianDataOutputStream LittleEndianDataOutputStream

List of usage examples for com.google.common.io LittleEndianDataOutputStream LittleEndianDataOutputStream

Introduction

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

Prototype

public LittleEndianDataOutputStream(OutputStream out) 

Source Link

Document

Creates a LittleEndianDataOutputStream that wraps the given stream.

Usage

From source file:org.glukit.dexcom.sync.LittleEndianDataOutputFactory.java

@Override
public DataOutput create(OutputStream outputStream) {
    return new LittleEndianDataOutputStream(outputStream);
}

From source file:io.airlift.slice.OutputStreamSliceOutput.java

@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
public OutputStreamSliceOutput(OutputStream outputStream) {
    checkNotNull(outputStream, "outputStream is null");
    countingOutputStream = new CountingOutputStream(outputStream);
    dataOutputStream = new LittleEndianDataOutputStream(countingOutputStream);
}

From source file:org.apache.hadoop.hive.ql.io.slice.OutputStreamSliceOutput.java

@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
public OutputStreamSliceOutput(OutputStream outputStream) {
    countingOutputStream = new CountingOutputStream(outputStream);
    dataOutputStream = new LittleEndianDataOutputStream(countingOutputStream);
}

From source file:terralib.World.java

public final void write(File file) throws IOException {
    OutputStream stream = new BufferedOutputStream(new FileOutputStream(file));

    write(new LittleEndianDataOutputStream(stream));

    stream.flush();//from  www .ja v a 2  s .  co m
    stream.close();
}

From source file:org.bimserver.serializers.binarygeometry.BinaryGeometrySerializer.java

private void writeGeometries(OutputStream outputStream) throws IOException {
    long start = System.nanoTime();

    LittleEndianDataOutputStream dataOutputStream = new LittleEndianDataOutputStream(outputStream);
    // Identifier for clients to determine if this server is even serving binary geometry
    dataOutputStream.writeUTF("BGS");

    // Version of the current format being outputted, should be changed for every (released) change in protocol 
    dataOutputStream.writeByte(FORMAT_VERSION);

    Bounds modelBounds = new Bounds();
    int nrObjects = 0;

    // All access to EClass is being done generically to support multiple IFC schema's with 1 serializer
    EClass productClass = getModel().getPackageMetaData().getEClass("IfcProduct");

    List<IdEObject> products = getModel().getAllWithSubTypes(productClass);

    // First iteration, to determine number of objects with geometry and calculate model bounds
    for (IdEObject ifcProduct : products) {
        GeometryInfo geometryInfo = (GeometryInfo) ifcProduct
                .eGet(ifcProduct.eClass().getEStructuralFeature("geometry"));
        if (geometryInfo != null && geometryInfo.getTransformation() != null) {
            Bounds objectBounds = new Bounds(
                    new Float3(geometryInfo.getMinBounds().getX(), geometryInfo.getMinBounds().getY(),
                            geometryInfo.getMinBounds().getZ()),
                    new Float3(geometryInfo.getMaxBounds().getX(), geometryInfo.getMaxBounds().getY(),
                            geometryInfo.getMaxBounds().getZ()));
            modelBounds.integrate(objectBounds);
            nrObjects++;/*from  w  ww  .  j a  va  2s .  c  om*/
        }
    }
    modelBounds.writeTo(dataOutputStream);
    dataOutputStream.writeInt(nrObjects);
    int bytesSaved = 0;
    int bytesTotal = 0;

    // Keeping track of geometry already sent, this can be used for instancing of reused geometry
    Set<Long> concreteGeometrySent = new HashSet<>();

    // Flushing here so the client can show progressbar etc...
    dataOutputStream.flush();

    int bytes = 6;
    int counter = 0;

    // Second iteration actually writing the geometry
    for (IdEObject ifcProduct : products) {
        GeometryInfo geometryInfo = (GeometryInfo) ifcProduct
                .eGet(ifcProduct.eClass().getEStructuralFeature("geometry"));
        if (geometryInfo != null && geometryInfo.getTransformation() != null) {
            String type = ifcProduct.eClass().getName();
            dataOutputStream.writeUTF(type);
            dataOutputStream.writeLong(ifcProduct.getOid());

            GeometryData geometryData = geometryInfo.getData();
            byte[] vertices = geometryData.getVertices();

            // BEWARE, ByteOrder is always LITTLE_ENDIAN, because that's what GPU's seem to prefer, Java's ByteBuffer default is BIG_ENDIAN though!

            bytesTotal += vertices.length;
            byte geometryType = concreteGeometrySent.contains(geometryData.getOid()) ? GEOMETRY_TYPE_INSTANCE
                    : GEOMETRY_TYPE_TRIANGLES;
            dataOutputStream.write(geometryType);

            bytes += (type.getBytes(Charsets.UTF_8).length + 3);

            // This is an ugly hack to align the bytes, but for 2 different kinds of output (this first one is the websocket implementation)
            int skip = 4 - (bytes % 4); // TODO fix
            if (skip != 0 && skip != 4) {
                dataOutputStream.write(new byte[skip]);
            }

            bytes = 0;

            dataOutputStream.write(geometryInfo.getTransformation());

            if (concreteGeometrySent.contains(geometryData.getOid())) {
                // Reused geometry, only send the id of the reused geometry data
                dataOutputStream.writeLong(geometryData.getOid());
                bytesSaved += vertices.length;
            } else {
                ByteBuffer vertexByteBuffer = ByteBuffer.wrap(vertices);
                dataOutputStream.writeLong(geometryData.getOid());

                Bounds objectBounds = new Bounds(geometryInfo.getMinBounds(), geometryInfo.getMaxBounds());
                objectBounds.writeTo(dataOutputStream);

                ByteBuffer indicesBuffer = ByteBuffer.wrap(geometryData.getIndices());
                dataOutputStream.writeInt(indicesBuffer.capacity() / 4);
                dataOutputStream.write(indicesBuffer.array());

                dataOutputStream.writeInt(vertexByteBuffer.capacity() / 4);
                dataOutputStream.write(vertexByteBuffer.array());

                ByteBuffer normalsBuffer = ByteBuffer.wrap(geometryData.getNormals());
                dataOutputStream.writeInt(normalsBuffer.capacity() / 4);
                dataOutputStream.write(normalsBuffer.array());

                // Only when materials are used we send them
                if (geometryData.getMaterials() != null) {
                    ByteBuffer materialsByteBuffer = ByteBuffer.wrap(geometryData.getMaterials());

                    dataOutputStream.writeInt(materialsByteBuffer.capacity() / 4);
                    dataOutputStream.write(materialsByteBuffer.array());
                } else {
                    // No materials used
                    dataOutputStream.writeInt(0);
                }

                concreteGeometrySent.add(geometryData.getOid());
            }
            counter++;
            if (counter % 12 == 0) {
                // Flushing in batches, this is to limit the amount of WebSocket messages
                dataOutputStream.flush();
            }
        }
    }
    dataOutputStream.flush();
    if (bytesTotal != 0 && bytesSaved != 0) {
        LOGGER.info((100 * bytesSaved / bytesTotal) + "% saved");
    }
    long end = System.nanoTime();
    LOGGER.debug(((end - start) / 1000000) + " ms");
}

From source file:de.nx42.maps4cim.map.TextureMap.java

/**
 * Stores the texture map in the specified output stream, storing the integers
 * in little endian byte order/*  w ww.j  av a  2s . c  o  m*/
 * @param out the output stream to write the texture map into
 * @param textureMap the texture map to write
 * @throws MapGeneratorException when the size of the texture map is invalid
 * @throws IOException when the resulting map can't be written
 */
protected void storeByteStream(OutputStream out, int[][] textureMap) throws MapGeneratorException, IOException {

    if (isValidSize(textureMap)) {
        // byte outputput stream, wrapped by a little endian integer processor
        ByteArrayOutputStream bos = new ByteArrayOutputStream(byteAmount);
        LittleEndianDataOutputStream dos = new LittleEndianDataOutputStream(bos);

        // pass the resulting integers to the little endian byte output stream
        for (int y = 0; y < edgeLength; y++) {
            for (int x = 0; x < edgeLength; x++) {
                dos.writeInt(textureMap[x][y]);
            }
        }

        // writeTo to the user defined output streame
        bos.writeTo(out);

        // close streams
        dos.close();
        bos.close();
    } else {
        throw new MapGeneratorException(
                "The size of the texture map is invalid. " + "Only Maps with 2048 * 2048 blocks are allowed");
    }

}

From source file:org.bimserver.serializers.binarygeometry.BinaryGeometryMessagingStreamingSerializer2.java

@Override
public boolean writeMessage(OutputStream outputStream, ProgressReporter progressReporter)
        throws IOException, SerializerException {
    dataOutputStream = new LittleEndianDataOutputStream(outputStream);
    switch (mode) {
    case LOAD: {//from w w  w. j  a v  a2 s.c  om
        load();
        mode = Mode.START;
    }
    case START:
        writeStart();
        mode = Mode.DATA;
        break;
    case DATA:
        if (!writeData()) {
            mode = Mode.END;
            return true;
        }
        break;
    case END:
        writeEnd();
        return false;
    default:
        break;
    }
    return true;
}

From source file:org.bimserver.serializers.binarygeometry.BinaryGeometryMessagingSerializer.java

private boolean writeStart(OutputStream outputStream) throws IOException {
    LittleEndianDataOutputStream dataOutputStream = new LittleEndianDataOutputStream(outputStream);
    // Identifier for clients to determine if this server is even serving binary geometry
    dataOutputStream.writeByte(MessageType.INIT.getId());
    dataOutputStream.writeUTF("BGS");

    // Version of the current format being outputted, should be changed for every (released) change in protocol 
    dataOutputStream.writeByte(FORMAT_VERSION);

    Bounds modelBounds = new Bounds();
    int nrObjects = 0;

    // All access to EClass is being done generically to support multiple IFC schema's with 1 serializer
    EClass productClass = model.getPackageMetaData().getEClass("IfcProduct");

    List<IdEObject> products = model.getAllWithSubTypes(productClass);

    // First iteration, to determine number of objects with geometry and calculate model bounds
    for (IdEObject ifcProduct : products) {
        GeometryInfo geometryInfo = (GeometryInfo) ifcProduct
                .eGet(ifcProduct.eClass().getEStructuralFeature("geometry"));
        if (geometryInfo != null && geometryInfo.getTransformation() != null) {
            Bounds objectBounds = new Bounds(
                    new Float3(geometryInfo.getMinBounds().getX(), geometryInfo.getMinBounds().getY(),
                            geometryInfo.getMinBounds().getZ()),
                    new Float3(geometryInfo.getMaxBounds().getX(), geometryInfo.getMaxBounds().getY(),
                            geometryInfo.getMaxBounds().getZ()));
            modelBounds.integrate(objectBounds);
            nrObjects++;/*w ww .  jav a2  s.  co m*/
        }
    }

    int skip = 4 - (7 % 4);
    if (skip != 0 && skip != 4) {
        dataOutputStream.write(new byte[skip]);
    }

    modelBounds.writeTo(dataOutputStream);
    dataOutputStream.writeInt(nrObjects);

    concreteGeometrySent = new HashMap<Long, Object>();
    EClass productEClass = packageMetaData.getEClass("IfcProduct");
    iterator = model.getAllWithSubTypes(productEClass).iterator();

    return nrObjects > 0;
}

From source file:sx.kenji.sharpserializerjvm.SharpSerializer.java

public void serialize(Property property) {
    try {//from www  . ja v  a  2  s.co m
        FileOutputStream baseStream = new FileOutputStream(targetFile, true);

        try (LittleEndianDataOutputStream stream = new LittleEndianDataOutputStream(baseStream)) {

            baseStream.getChannel().position(baseStream.getChannel().size());

            Serializer serializer = new Serializer(stream);
            serializer.serialize(property);
        }
    } catch (IOException e) {
        logger.error("Error opening target file '%s' for serializing: `{}`.", targetFile, e.getMessage());
    }
}

From source file:de.nx42.maps4cim.map.ReliefMap.java

/**
 * Stores the relief map in the specified output stream, transforming
 * the floats to little endian 32 bit integers.
 * @param out the output stream to write the relief map into
 * @param reliefMap the relief map to write
 * @throws MapGeneratorException when the size of the relief map is invalid
 * @throws IOException when the resulting map can't be written
 *//*from  w ww . j a va 2 s .c o  m*/
protected void storeByteStream(OutputStream out, float[][] reliefMap)
        throws MapGeneratorException, IOException {

    if (isValidSize(reliefMap)) {
        // byte outputput stream, wrapped by a little endian integer processor
        ByteArrayOutputStream bos = new ByteArrayOutputStream(byteAmount);
        LittleEndianDataOutputStream dos = new LittleEndianDataOutputStream(bos);

        // pass the resulting integers to the little endian byte output stream
        for (int y = 0; y < edgeLength; y++) {
            for (int x = 0; x < edgeLength; x++) {
                // convert the floats (meter) to integers (millimeter)
                dos.writeInt((int) (reliefMap[y][x] * 1000));
            }
        }

        // write to the user defined output stream
        bos.writeTo(out);

        // close streams
        dos.close();
        bos.close();
    } else {
        throw new MapGeneratorException("The size of the relief map is invalid. "
                + "Only Maps with 2049 * 2049 control points are allowed");
    }

}