Example usage for java.io DataOutputStream writeBoolean

List of usage examples for java.io DataOutputStream writeBoolean

Introduction

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

Prototype

public final void writeBoolean(boolean v) throws IOException 

Source Link

Document

Writes a boolean to the underlying output stream as a 1-byte value.

Usage

From source file:org.apache.jackrabbit.core.persistence.util.Serializer.java

/**
 * Serializes the specified <code>PropertyState</code> object to the given
 * binary <code>stream</code>. Binary values are stored in the specified
 * <code>BLOBStore</code>.//w ww .  j  a v a  2 s  . c  om
 *
 * @param state     <code>state</code> to serialize
 * @param stream    the stream where the <code>state</code> should be
 *                  serialized to
 * @param blobStore handler for BLOB data
 * @throws Exception if an error occurs during the serialization
 * @see #deserialize(PropertyState, InputStream,BLOBStore)
 */
public static void serialize(PropertyState state, OutputStream stream, BLOBStore blobStore) throws Exception {
    DataOutputStream out = new DataOutputStream(stream);

    // type
    out.writeInt(state.getType());
    // multiValued
    out.writeBoolean(state.isMultiValued());
    // definitionId
    out.writeUTF("");
    // modCount
    out.writeShort(state.getModCount());
    // values
    InternalValue[] values = state.getValues();
    out.writeInt(values.length); // count
    for (int i = 0; i < values.length; i++) {
        InternalValue val = values[i];
        if (state.getType() == PropertyType.BINARY) {
            // special handling required for binary value:
            // put binary value in BLOB store
            InputStream in = val.getStream();
            String blobId = blobStore.createId(state.getPropertyId(), i);
            try {
                blobStore.put(blobId, in, val.getLength());
            } finally {
                IOUtils.closeQuietly(in);
            }
            // store id of BLOB as property value
            out.writeUTF(blobId); // value
            // replace value instance with value backed by resource
            // in BLOB store and discard old value instance (e.g. temp file)
            if (blobStore instanceof ResourceBasedBLOBStore) {
                // optimization: if the BLOB store is resource-based
                // retrieve the resource directly rather than having
                // to read the BLOB from an input stream
                FileSystemResource fsRes = ((ResourceBasedBLOBStore) blobStore).getResource(blobId);
                values[i] = InternalValue.create(fsRes);
            } else {
                in = blobStore.get(blobId);
                try {
                    values[i] = InternalValue.create(in);
                } finally {
                    IOUtils.closeQuietly(in);
                }
            }
            val.discard();
        } else {
            /**
             * because writeUTF(String) has a size limit of 65k,
             * Strings are serialized as <length><byte[]>
             */
            //out.writeUTF(val.toString());   // value
            byte[] bytes = val.toString().getBytes(ENCODING);
            out.writeInt(bytes.length); // lenght of byte[]
            out.write(bytes); // byte[]
        }
    }
}

From source file:ml.shifu.shifu.core.dtrain.dt.BinaryDTSerializer.java

public static void save(ModelConfig modelConfig, List<ColumnConfig> columnConfigList,
        List<List<TreeNode>> baggingTrees, String loss, int inputCount, OutputStream output)
        throws IOException {
    DataOutputStream fos = null;

    try {/*from   w w w. j  a  v a  2  s  . c o m*/
        fos = new DataOutputStream(new GZIPOutputStream(output));
        // version
        fos.writeInt(CommonConstants.TREE_FORMAT_VERSION);
        fos.writeUTF(modelConfig.getAlgorithm());
        fos.writeUTF(loss);
        fos.writeBoolean(modelConfig.isClassification());
        fos.writeBoolean(modelConfig.getTrain().isOneVsAll());
        fos.writeInt(inputCount);

        Map<Integer, String> columnIndexNameMapping = new HashMap<Integer, String>();
        Map<Integer, List<String>> columnIndexCategoricalListMapping = new HashMap<Integer, List<String>>();
        Map<Integer, Double> numericalMeanMapping = new HashMap<Integer, Double>();
        for (ColumnConfig columnConfig : columnConfigList) {
            if (columnConfig.isFinalSelect()) {
                columnIndexNameMapping.put(columnConfig.getColumnNum(), columnConfig.getColumnName());
            }
            if (columnConfig.isCategorical() && CollectionUtils.isNotEmpty(columnConfig.getBinCategory())) {
                columnIndexCategoricalListMapping.put(columnConfig.getColumnNum(),
                        columnConfig.getBinCategory());
            }

            if (columnConfig.isNumerical() && columnConfig.getMean() != null) {
                numericalMeanMapping.put(columnConfig.getColumnNum(), columnConfig.getMean());
            }
        }

        if (columnIndexNameMapping.size() == 0) {
            boolean hasCandidates = CommonUtils.hasCandidateColumns(columnConfigList);
            for (ColumnConfig columnConfig : columnConfigList) {
                if (CommonUtils.isGoodCandidate(columnConfig, hasCandidates)) {
                    columnIndexNameMapping.put(columnConfig.getColumnNum(), columnConfig.getColumnName());
                }
            }
        }

        // serialize numericalMeanMapping
        fos.writeInt(numericalMeanMapping.size());
        for (Entry<Integer, Double> entry : numericalMeanMapping.entrySet()) {
            fos.writeInt(entry.getKey());
            // for some feature, it is null mean value, it is not selected, just set to 0d to avoid NPE
            fos.writeDouble(entry.getValue() == null ? 0d : entry.getValue());
        }
        // serialize columnIndexNameMapping
        fos.writeInt(columnIndexNameMapping.size());
        for (Entry<Integer, String> entry : columnIndexNameMapping.entrySet()) {
            fos.writeInt(entry.getKey());
            fos.writeUTF(entry.getValue());
        }
        // serialize columnIndexCategoricalListMapping
        fos.writeInt(columnIndexCategoricalListMapping.size());
        for (Entry<Integer, List<String>> entry : columnIndexCategoricalListMapping.entrySet()) {
            List<String> categories = entry.getValue();
            if (categories != null) {
                fos.writeInt(entry.getKey());
                fos.writeInt(categories.size());
                for (String category : categories) {
                    // There is 16k limitation when using writeUTF() function.
                    // if the category value is larger than 10k, write a marker -1 and write bytes instead of
                    // writeUTF;
                    // in read part logic should be changed also to readByte not readUTF according to the marker
                    if (category.length() < Constants.MAX_CATEGORICAL_VAL_LEN) {
                        fos.writeUTF(category);
                    } else {
                        fos.writeShort(UTF_BYTES_MARKER); // marker here
                        byte[] bytes = category.getBytes("UTF-8");
                        fos.writeInt(bytes.length);
                        for (int i = 0; i < bytes.length; i++) {
                            fos.writeByte(bytes[i]);
                        }
                    }
                }
            }
        }

        Map<Integer, Integer> columnMapping = getColumnMapping(columnConfigList);
        fos.writeInt(columnMapping.size());
        for (Entry<Integer, Integer> entry : columnMapping.entrySet()) {
            fos.writeInt(entry.getKey());
            fos.writeInt(entry.getValue());
        }

        // after model version 4 (>=4), IndependentTreeModel support bagging, here write a default RF/GBT size 1
        fos.writeInt(baggingTrees.size());
        for (int i = 0; i < baggingTrees.size(); i++) {
            List<TreeNode> trees = baggingTrees.get(i);
            int treeLength = trees.size();
            fos.writeInt(treeLength);
            for (TreeNode treeNode : trees) {
                treeNode.write(fos);
            }
        }
    } catch (IOException e) {
        LOG.error("Error in writing output.", e);
    } finally {
        IOUtils.closeStream(fos);
    }
}

From source file:gridool.util.xfer.TransferUtils.java

public static void send(@Nonnull final FastByteArrayOutputStream data, @Nonnull final String fileName,
        @Nullable final String writeDirPath, @Nonnull final InetAddress dstAddr, final int dstPort,
        final boolean append, final boolean sync, @Nullable final TransferClientHandler handler)
        throws IOException {
    final SocketAddress dstSockAddr = new InetSocketAddress(dstAddr, dstPort);
    SocketChannel channel = null;
    Socket socket = null;//w  ww  .  ja v a  2  s. co  m
    final OutputStream out;
    try {
        channel = SocketChannel.open();
        socket = channel.socket();
        socket.connect(dstSockAddr);
        out = socket.getOutputStream();
    } catch (IOException e) {
        LOG.error("failed to connect: " + dstSockAddr, e);
        IOUtils.closeQuietly(channel);
        NetUtils.closeQuietly(socket);
        throw e;
    }

    DataInputStream din = null;
    if (sync) {
        InputStream in = socket.getInputStream();
        din = new DataInputStream(in);
    }
    final DataOutputStream dos = new DataOutputStream(out);
    final StopWatch sw = new StopWatch();
    try {
        IOUtils.writeString(fileName, dos);
        IOUtils.writeString(writeDirPath, dos);
        long nbytes = data.size();
        dos.writeLong(nbytes);
        dos.writeBoolean(append);
        dos.writeBoolean(sync);
        if (handler == null) {
            dos.writeBoolean(false);
        } else {
            dos.writeBoolean(true);
            handler.writeAdditionalHeader(dos);
        }

        // send file using zero-copy send
        data.writeTo(out);

        if (LOG.isDebugEnabled()) {
            LOG.debug("Sent a file data '" + fileName + "' of " + nbytes + " bytes to " + dstSockAddr.toString()
                    + " in " + sw.toString());
        }

        if (sync) {// receive ack in sync mode
            long remoteRecieved = din.readLong();
            if (remoteRecieved != nbytes) {
                throw new IllegalStateException(
                        "Sent " + nbytes + " bytes, but remote node received " + remoteRecieved + " bytes");
            }
        }
    } catch (FileNotFoundException e) {
        LOG.error(PrintUtils.prettyPrintStackTrace(e, -1));
        throw e;
    } catch (IOException e) {
        LOG.error(PrintUtils.prettyPrintStackTrace(e, -1));
        throw e;
    } finally {
        IOUtils.closeQuietly(din, dos);
        IOUtils.closeQuietly(channel);
        NetUtils.closeQuietly(socket);
    }
}

From source file:RealFunctionValidation.java

public static Object readAndWritePrimitiveValue(final DataInputStream in, final DataOutputStream out,
        final Class<?> type) throws IOException {

    if (!type.isPrimitive()) {
        throw new IllegalArgumentException("type must be primitive");
    }//w w w .j  a  v a2  s .c o m
    if (type.equals(Boolean.TYPE)) {
        final boolean x = in.readBoolean();
        out.writeBoolean(x);
        return Boolean.valueOf(x);
    } else if (type.equals(Byte.TYPE)) {
        final byte x = in.readByte();
        out.writeByte(x);
        return Byte.valueOf(x);
    } else if (type.equals(Character.TYPE)) {
        final char x = in.readChar();
        out.writeChar(x);
        return Character.valueOf(x);
    } else if (type.equals(Double.TYPE)) {
        final double x = in.readDouble();
        out.writeDouble(x);
        return Double.valueOf(x);
    } else if (type.equals(Float.TYPE)) {
        final float x = in.readFloat();
        out.writeFloat(x);
        return Float.valueOf(x);
    } else if (type.equals(Integer.TYPE)) {
        final int x = in.readInt();
        out.writeInt(x);
        return Integer.valueOf(x);
    } else if (type.equals(Long.TYPE)) {
        final long x = in.readLong();
        out.writeLong(x);
        return Long.valueOf(x);
    } else if (type.equals(Short.TYPE)) {
        final short x = in.readShort();
        out.writeShort(x);
        return Short.valueOf(x);
    } else {
        // This should never occur.
        throw new IllegalStateException();
    }
}

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  w w  w.j  a v a 2 s  .  c om*/
 * @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:org.apache.hadoop.hive.serde2.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 a va2s . 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;

        case BINARY: {
            BytesWritable bw = ((BinaryObjectInspector) oi).getPrimitiveWritableObject(o);
            out.write(bw.getBytes(), 0, bw.getLength());
            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:gridool.util.xfer.TransferUtils.java

public static void sendfile(@Nonnull final File file, final long fromPos, final long count,
        @Nullable final String writeDirPath, @Nonnull final InetAddress dstAddr, final int dstPort,
        final boolean append, final boolean sync, @Nonnull final TransferClientHandler handler)
        throws IOException {
    if (!file.exists()) {
        throw new IllegalArgumentException(file.getAbsolutePath() + " does not exist");
    }/* w  w w . j  a  va  2  s. c  o m*/
    if (!file.isFile()) {
        throw new IllegalArgumentException(file.getAbsolutePath() + " is not file");
    }
    if (!file.canRead()) {
        throw new IllegalArgumentException(file.getAbsolutePath() + " cannot read");
    }

    final SocketAddress dstSockAddr = new InetSocketAddress(dstAddr, dstPort);
    SocketChannel channel = null;
    Socket socket = null;
    final OutputStream out;
    try {
        channel = SocketChannel.open();
        socket = channel.socket();
        socket.connect(dstSockAddr);
        out = socket.getOutputStream();
    } catch (IOException e) {
        LOG.error("failed to connect: " + dstSockAddr, e);
        IOUtils.closeQuietly(channel);
        NetUtils.closeQuietly(socket);
        throw e;
    }

    DataInputStream din = null;
    if (sync) {
        InputStream in = socket.getInputStream();
        din = new DataInputStream(in);
    }
    final DataOutputStream dos = new DataOutputStream(out);
    final StopWatch sw = new StopWatch();
    FileInputStream src = null;
    final long nbytes;
    try {
        src = new FileInputStream(file);
        FileChannel fc = src.getChannel();

        String fileName = file.getName();
        IOUtils.writeString(fileName, dos);
        IOUtils.writeString(writeDirPath, dos);
        long xferBytes = (count == -1L) ? fc.size() : count;
        dos.writeLong(xferBytes);
        dos.writeBoolean(append); // append=false
        dos.writeBoolean(sync);
        if (handler == null) {
            dos.writeBoolean(false);
        } else {
            dos.writeBoolean(true);
            handler.writeAdditionalHeader(dos);
        }

        // send file using zero-copy send
        nbytes = fc.transferTo(fromPos, xferBytes, channel);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Sent a file '" + file.getAbsolutePath() + "' of " + nbytes + " bytes to "
                    + dstSockAddr.toString() + " in " + sw.toString());
        }

        if (sync) {// receive ack in sync mode
            long remoteRecieved = din.readLong();
            if (remoteRecieved != xferBytes) {
                throw new IllegalStateException(
                        "Sent " + xferBytes + " bytes, but remote node received " + remoteRecieved + " bytes");
            }
        }

    } catch (FileNotFoundException e) {
        LOG.error(PrintUtils.prettyPrintStackTrace(e, -1));
        throw e;
    } catch (IOException e) {
        LOG.error(PrintUtils.prettyPrintStackTrace(e, -1));
        throw e;
    } finally {
        IOUtils.closeQuietly(src);
        IOUtils.closeQuietly(din, dos);
        IOUtils.closeQuietly(channel);
        NetUtils.closeQuietly(socket);
    }
}

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

public static void save(SourceFileCompilationUnit unit, OutputStream outputStream) throws Exception {
    DataOutputStream dos = null;
    try {//ww  w  . jav  a2  s. co m
        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:uk.ac.ebi.mdk.io.ReactionMatrixIO.java

public static void writeCompressedBasicStoichiometricMatrix(StoichiometricMatrix<?, ?> s, OutputStream writer)
        throws IOException {

    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(writer, 2048));

    int n = s.getMoleculeCount();
    int m = s.getReactionCount();

    out.writeInt(n);/* w w w.  ja v  a2 s.  c om*/
    out.writeInt(m);

    for (int j = 0; j < m; j++) {
        out.writeUTF(s.getReaction(j).toString());
    }

    for (int i = 0; i < n; i++) {
        out.writeUTF(s.getMolecule(i).toString());
    }

    out.writeBoolean(convertDoubles);
    out.writeInt(s.getNonNullCount());

    for (int i = 0; i < n; i++) {

        for (int j = 0; j < m; j++) {

            // if the value is null
            if (convertDoubles) {
                int value = s.get(i, j).intValue();
                if (value != 0) {
                    out.writeInt(i);
                    out.writeInt(j);
                    out.writeInt(value);
                }
            } else {
                double value = s.get(i, j);
                if (value != 0d) {
                    out.writeInt(i);
                    out.writeInt(j);
                    out.writeDouble(value);
                }
            }

        }
    }

    out.close();
}

From source file:net.datenwerke.transloader.primitive.WrapperConverter.java

private static void write(Object wrapper, DataOutputStream dataStream) throws IOException {
    char typeCode = getTypeCode(wrapper);
    switch (typeCode) {
    case 'B':
        dataStream.writeByte(intValue(wrapper));
        break;/*from ww w. j  a v a2s  .  c om*/
    case 'C':
        dataStream.writeChar(charValue(wrapper));
        break;
    case 'S':
        dataStream.writeShort(intValue(wrapper));
        break;
    case 'I':
        dataStream.writeInt(intValue(wrapper));
        break;
    case 'J':
        dataStream.writeLong(number(wrapper).longValue());
        break;
    case 'F':
        dataStream.writeFloat(number(wrapper).floatValue());
        break;
    case 'D':
        dataStream.writeDouble(number(wrapper).doubleValue());
        break;
    case 'Z':
        dataStream.writeBoolean(booleanValue(wrapper));
    }
}