Example usage for java.io DataOutputStream writeInt

List of usage examples for java.io DataOutputStream writeInt

Introduction

In this page you can find the example usage for java.io DataOutputStream writeInt.

Prototype

public final void writeInt(int v) throws IOException 

Source Link

Document

Writes an int to the underlying output stream as four bytes, high byte first.

Usage

From source file:org.apache.fontbox.ttf.TTFSubFont.java

private static void writeUint32(DataOutputStream dos, long l) throws IOException {
    dos.writeInt((int) l);
}

From source file:org.spout.engine.filesystem.WorldFiles.java

public static void writeColumn(OutputStream out, SpoutColumn column, AtomicInteger lowestY,
        BlockMaterial[][] topmostBlocks) {
    DataOutputStream dataStream = new DataOutputStream(out);
    try {//from  w  w  w .  j a va 2  s. c  o m
        for (int x = 0; x < SpoutColumn.BLOCKS.SIZE; x++) {
            for (int z = 0; z < SpoutColumn.BLOCKS.SIZE; z++) {
                dataStream.writeInt(column.getAtomicInteger(x, z).get());
            }
        }
        dataStream.writeInt(COLUMN_VERSION);
        dataStream.writeInt(lowestY.get());
        StringMap global = ((SpoutEngine) Spout.getEngine()).getEngineItemMap();
        StringMap itemMap;
        itemMap = column.getWorld().getItemMap();
        for (int x = 0; x < SpoutColumn.BLOCKS.SIZE; x++) {
            for (int z = 0; z < SpoutColumn.BLOCKS.SIZE; z++) {
                Material m = topmostBlocks[x][z];
                if (m == null) {
                    dataStream.writeBoolean(false);
                    continue;
                } else {
                    dataStream.writeBoolean(true);
                }
                short blockId = m.getId();
                short blockData = m.getData();
                blockId = (short) global.convertTo(itemMap, blockId);
                dataStream.writeInt(BlockFullState.getPacked(blockId, blockData));
            }
        }
        //Biome manager is serialized with:
        // - boolean, if a biome manager exists
        // - String, the class name
        // - int, the number of bytes of data to read
        // - byte[], size of the above int in length
        BiomeManager manager = column.getBiomeManager();
        if (manager != null) {
            dataStream.writeBoolean(true);
            dataStream.writeUTF(manager.getClass().getName());
            byte[] data = manager.serialize();
            dataStream.writeInt(data.length);
            dataStream.write(data);
        } else {
            dataStream.writeBoolean(false);
        }
        dataStream.flush();
    } catch (IOException e) {
        Spout.getLogger()
                .severe("Error writing column height-map for column" + column.getX() + ", " + column.getZ());
    }
}

From source file:org.apache.fontbox.ttf.TTFSubFont.java

/**
 * @param dos The data output stream./*from w  ww. j a va  2  s. c o m*/
 * @param nTables The number of table.
 * @return The file offset of the first TTF table to write.
 * @throws IOException Upon errors.
 */
private static long writeFileHeader(DataOutputStream dos, int nTables) throws IOException {
    dos.writeInt(0x00010000);
    dos.writeShort(nTables);

    int mask = Integer.highestOneBit(nTables);
    int searchRange = mask * 16;
    dos.writeShort(searchRange);

    int entrySelector = log2i(mask);

    dos.writeShort(entrySelector);

    // numTables * 16 - searchRange
    int last = 16 * nTables - searchRange;
    dos.writeShort(last);

    return 0x00010000L + buildUint32(nTables, searchRange) + buildUint32(entrySelector, last);
}

From source file:com.ebay.nest.io.sede.lazy.LazyUtils.java

/**
 * Write out a binary representation of a PrimitiveObject to a byte stream.
 *
 * @param out ByteStream.Output, an unsynchronized version of ByteArrayOutputStream, used as a
 *            backing buffer for the the DataOutputStream
 * @param o the PrimitiveObject//from ww w . j  av  a  2 s.co m
 * @param oi the PrimitiveObjectInspector
 * @throws IOException on error during the write operation
 */
public static void writePrimitive(OutputStream out, Object o, PrimitiveObjectInspector oi) throws IOException {

    DataOutputStream dos = new DataOutputStream(out);

    try {
        switch (oi.getPrimitiveCategory()) {
        case BOOLEAN:
            boolean b = ((BooleanObjectInspector) oi).get(o);
            dos.writeBoolean(b);
            break;

        case BYTE:
            byte bt = ((ByteObjectInspector) oi).get(o);
            dos.writeByte(bt);
            break;

        case SHORT:
            short s = ((ShortObjectInspector) oi).get(o);
            dos.writeShort(s);
            break;

        case INT:
            int i = ((IntObjectInspector) oi).get(o);
            dos.writeInt(i);
            break;

        case LONG:
            long l = ((LongObjectInspector) oi).get(o);
            dos.writeLong(l);
            break;

        case FLOAT:
            float f = ((FloatObjectInspector) oi).get(o);
            dos.writeFloat(f);
            break;

        case DOUBLE:
            double d = ((DoubleObjectInspector) oi).get(o);
            dos.writeDouble(d);
            break;

        default:
            throw new RuntimeException("Hive internal error.");
        }
    } finally {
        // closing the underlying ByteStream should have no effect, the data should still be
        // accessible
        dos.close();
    }
}

From source file:com.google.gwt.dev.javac.CachedCompilationUnit.java

public static void save(SourceFileCompilationUnit unit, OutputStream outputStream) throws Exception {
    DataOutputStream dos = null;
    try {//from   w ww  .  j  a v a2  s.c om
        dos = new DataOutputStream(new BufferedOutputStream(outputStream));
        // version
        dos.writeLong(CompilationUnitDiskCache.CACHE_VERSION);
        // simple stuff
        dos.writeLong(unit.getLastModified());
        dos.writeUTF(unit.getDisplayLocation());
        dos.writeUTF(unit.getTypeName());
        dos.writeUTF(unit.getContentId().get());
        dos.writeBoolean(unit.isSuperSource());
        // compiled classes
        {
            Collection<CompiledClass> compiledClasses = unit.getCompiledClasses();
            int size = compiledClasses.size();
            dos.writeInt(size);
            if (size > 0) {
                // sort in enclosing order to be able to restore enclosing classes by name
                CompiledClass[] compiledClassesArray = compiledClasses
                        .toArray(new CompiledClass[compiledClasses.size()]);
                Arrays.sort(compiledClassesArray, new Comparator<CompiledClass>() {
                    public int compare(CompiledClass o1, CompiledClass o2) {
                        int o1count = countMatches(o1.getInternalName(), Signature.C_DOLLAR);
                        int o2count = countMatches(o2.getInternalName(), Signature.C_DOLLAR);
                        return o1count - o2count;
                    }
                });
                // store
                for (CompiledClass compiledClass : compiledClassesArray) {
                    // internal name
                    dos.writeUTF(compiledClass.getInternalName());
                    // is local
                    dos.writeBoolean(compiledClass.isLocal());
                    // bytes
                    byte[] bytes = compiledClass.getBytes();
                    dos.writeInt(bytes.length);
                    dos.write(bytes);
                    // enclosing class, write the name only
                    CompiledClass enclosingClass = compiledClass.getEnclosingClass();
                    String enclosingClassName = enclosingClass != null ? enclosingClass.getInternalName() : "";
                    dos.writeUTF(enclosingClassName);
                }
            }
        }
        // dependencies
        {
            Set<ContentId> dependencies = unit.getDependencies();
            int size = dependencies.size();
            dos.writeInt(size);
            if (size > 0) {
                for (ContentId contentId : dependencies) {
                    dos.writeUTF(contentId.get());
                }
            }
        }
        // JSNI methods
        {
            List<JsniMethod> jsniMethods = unit.getJsniMethods();
            int size = jsniMethods.size();
            dos.writeInt(size);
            if (size > 0) {
                for (JsniMethod jsniMethod : jsniMethods) {
                    dos.writeUTF(jsniMethod.name());
                    JsFunction function = jsniMethod.function();
                    SourceInfo sourceInfo = function.getSourceInfo();
                    dos.writeInt(sourceInfo.getStartPos());
                    dos.writeInt(sourceInfo.getEndPos());
                    dos.writeInt(sourceInfo.getStartLine());
                    dos.writeUTF(function.toSource());
                }
            }
        }
        // Method lookup
        {
            MethodArgNamesLookup methodArgs = unit.getMethodArgs();
            MethodArgNamesLookup.save(methodArgs, dos);
        }
    } finally {
        IOUtils.closeQuietly(dos);
    }
}

From source file:CounterServer.java

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpSession session = req.getSession(true);
    int count = 1;
    Integer i = (Integer) session.getAttribute(COUNTER_KEY);
    if (i != null) {
        count = i.intValue() + 5;/*from  w  w w .  j  a va  2s .com*/
    }
    session.setAttribute(COUNTER_KEY, new Integer(count));
    DataInputStream in = new DataInputStream(req.getInputStream());
    resp.setContentType("application/octet-stream");
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(byteOut);
    out.writeInt(count);
    out.flush();
    byte[] buf = byteOut.toByteArray();
    resp.setContentLength(buf.length);
    ServletOutputStream servletOut = resp.getOutputStream();
    servletOut.write(buf);
    servletOut.close();
}

From source file:additionalpipes.inventory.components.PropertyStrArray.java

@Override
public void writeData(DataOutputStream data) throws IOException {
    data.writeInt(value.length);
    for (String v : value) {
        data.writeUTF(v);/*from   w  ww . jav a 2  s.  co  m*/
    }
}

From source file:org.openmrs.module.odkconnector.serialization.serializer.custom.SerializedCohortSerializer.java

/**
 * Write the data to the output stream.//from  w ww  .ja v  a 2  s .c  o  m
 *
 * @param stream the output stream
 * @param data   the data that need to be written to the output stream
 */
@Override
public void write(final OutputStream stream, final Object data) throws IOException {

    SerializedCohort cohort = (SerializedCohort) data;

    DataOutputStream outputStream = new DataOutputStream(stream);

    outputStream.writeInt(cohort.getId());
    outputStream.writeUTF(cohort.getName());
    outputStream.flush();
}

From source file:org.openmrs.module.odkconnector.serialization.serializer.openmrs.CohortSerializer.java

/**
 * Write the data to the output stream./*  w  ww .  j  ava  2 s .  co  m*/
 *
 * @param stream the output stream
 * @param data   the data that need to be written to the output stream
 */
@Override
public void write(final OutputStream stream, final Object data) throws IOException {

    Cohort cohort = (Cohort) data;

    DataOutputStream outputStream = new DataOutputStream(stream);

    outputStream.writeInt(cohort.getCohortId());
    outputStream.writeUTF(cohort.getName());
    outputStream.flush();
}

From source file:org.apache.cassandra.net.OutboundTcpConnection.java

public static void write(Message message, String id, DataOutputStream out) throws IOException {
    /*/*  www. j  a va 2 s .c  o  m*/
     Setting up the protocol header. This is 4 bytes long
     represented as an integer. The first 2 bits indicate
     the serializer type. The 3rd bit indicates if compression
     is turned on or off. It is turned off by default. The 4th
     bit indicates if we are in streaming mode. It is turned off
     by default. The 5th-8th bits are reserved for future use.
     The next 8 bits indicate a version number. Remaining 15 bits
     are not used currently.
    */
    int header = 0;
    // Setting up the serializer bit
    header |= MessagingService.serializerType_.ordinal();
    // set compression bit.
    if (false)
        header |= 4;
    // Setting up the version bit
    header |= (message.getVersion() << 8);

    out.writeInt(MessagingService.PROTOCOL_MAGIC);
    out.writeInt(header);
    // compute total Message length for compatibility w/ 0.8 and earlier
    byte[] bytes = message.getMessageBody();
    int total = messageLength(message.header_, id, bytes);
    out.writeInt(total);
    out.writeUTF(id);
    Header.serializer().serialize(message.header_, out, message.getVersion());
    out.writeInt(bytes.length);
    out.write(bytes);
}