Example usage for java.io DataOutputStream writeChar

List of usage examples for java.io DataOutputStream writeChar

Introduction

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

Prototype

public final void writeChar(int v) throws IOException 

Source Link

Document

Writes a char to the underlying output stream as a 2-byte value, high byte first.

Usage

From source file:Main.java

public static void writeSettings(String file, Object... objs) throws IOException {
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file), 1024));
    try {/* ww w  . j  a  v  a2  s .  c  om*/
        out.writeInt(objs.length);
        for (Object obj : objs) {
            char cl;
            if (obj instanceof Byte) {
                cl = 'Y';
            } else {
                cl = obj.getClass().getSimpleName().charAt(0);
            }

            out.writeChar(cl);
            if (obj instanceof String) {
                out.writeUTF((String) obj);
            } else if (obj instanceof Float) {
                out.writeFloat((Float) obj);
            } else if (obj instanceof Double) {
                out.writeDouble((Double) obj);
            } else if (obj instanceof Integer) {
                out.writeInt((Integer) obj);
            } else if (obj instanceof Long) {
                out.writeLong((Long) obj);
            } else if (obj instanceof Boolean) {
                out.writeBoolean((Boolean) obj);
            } else {
                throw new IllegalStateException("Unsupported type");
            }
        }
    } finally {
        out.close();
    }
}

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  av  a  2  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:ch.unil.genescore.vegas.Snp.java

public void writePosAndAllele(DataOutputStream os) throws IOException {

    // NOTE: ALSO CHANGE readBinary() IF YOU CHANGE THIS
    os.writeUTF(id_);/*from  w ww.j a v  a 2  s.  c  o m*/
    os.writeUTF(chr_);
    os.writeInt(start_);
    os.writeInt(end_);
    os.writeBoolean(posStrand_);
    os.writeChar(minorAllele_);
}

From source file:com.wsc.myexample.decisionForest.MyTestForest.java

private void testFile(String inPath, String outPath, DataConverter converter, MyDecisionForest forest,
        Dataset dataset, /*List<double[]> results,*/ Random rng, ResultAnalyzer analyzer) throws IOException {
    // create the predictions file
    DataOutputStream ofile = null;

    if (outPath != null) {
        ofile = new DataOutputStream(new FileOutputStream(outPath));
    }//from   www.j  av  a2  s  .co m

    DataInputStream input = new DataInputStream(new FileInputStream(inPath));
    try {
        Scanner scanner = new Scanner(input);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.isEmpty()) {
                continue; // skip empty lines
            }

            Instance instance = converter.convert(line);
            if (instance == null)
                continue;

            double prediction = forest.classify(dataset, rng, instance);

            if (ofile != null) {
                ofile.writeChars(Double.toString(prediction)); // write the prediction
                ofile.writeChar('\n');
            }

            //        results.add(new double[] {dataset.getLabel(instance), prediction});

            analyzer.addInstance(dataset.getLabelString(dataset.getLabel(instance)),
                    new ClassifierResult(dataset.getLabelString(prediction), 1.0));
        }

        scanner.close();
    } finally {
        Closeables.closeQuietly(input);
        ofile.close();
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.CachingDataXceiver.java

@Override
public void copyBlock(final ExtendedBlock block, final Token<BlockTokenIdentifier> blockToken)
        throws IOException {
    updateCurrentThreadName("Copying block " + block);
    // Read in the header
    if (datanode.isBlockTokenEnabled) {
        try {// ww  w . j  a v  a 2s.  c  o  m
            datanode.blockPoolTokenSecretManager.checkAccess(blockToken, null, block,
                    BlockTokenSecretManager.AccessMode.COPY);
        } catch (InvalidToken e) {
            LOG.warn("Invalid access token in request from " + remoteAddress + " for OP_COPY_BLOCK for block "
                    + block + " : " + e.getLocalizedMessage());
            sendResponse(s, ERROR_ACCESS_TOKEN, "Invalid access token", dnConf.socketWriteTimeout);
            return;
        }

    }

    if (!dataXceiverServer.balanceThrottler.acquire()) { // not able to start
        String msg = "Not able to copy block " + block.getBlockId() + " to " + s.getRemoteSocketAddress()
                + " because threads quota is exceeded.";
        LOG.info(msg);
        sendResponse(s, ERROR, msg, dnConf.socketWriteTimeout);
        return;
    }

    CachingBlockSender blockSender = null;
    DataOutputStream reply = null;
    boolean isOpSuccess = true;

    try {
        // check if the block exists or not
        blockSender = new CachingBlockSender(this.blockCache, block, 0, -1, false, false, datanode, null);

        // set up response stream
        OutputStream baseStream = NetUtils.getOutputStream(s, dnConf.socketWriteTimeout);
        reply = new DataOutputStream(new BufferedOutputStream(baseStream, HdfsConstants.SMALL_BUFFER_SIZE));

        // send status first
        writeSuccessWithChecksumInfo(blockSender, reply);
        // send block content to the target
        long read = blockSender.sendBlock(reply, baseStream, dataXceiverServer.balanceThrottler);

        datanode.metrics.incrBytesRead((int) read);
        datanode.metrics.incrBlocksRead();

        LOG.info("Copied block " + block + " to " + s.getRemoteSocketAddress());
    } catch (IOException ioe) {
        isOpSuccess = false;
        LOG.info("opCopyBlock " + block + " received exception " + ioe);
        throw ioe;
    } finally {
        dataXceiverServer.balanceThrottler.release();
        if (isOpSuccess) {
            try {
                // send one last byte to indicate that the resource is cleaned.
                reply.writeChar('d');
            } catch (IOException ignored) {
            }
        }
        IOUtils.closeStream(reply);
        IOUtils.closeStream(blockSender);
    }

    // update metrics
    datanode.metrics.addCopyBlockOp(elapsed());
}

From source file:org.apache.hadoop.hdfs.server.datanode.DataXceiver.java

/**
 * Read a block from the disk and then sends it to a destination.
 * /*from  www.  jav a2  s.c o m*/
 * @param in The stream to read from
 * @throws IOException
 */
private void copyBlock(DataInputStream in) throws IOException {
    // Read in the header
    long blockId = in.readLong(); // read block id
    Block block = new Block(blockId, 0, in.readLong());
    Token<BlockTokenIdentifier> accessToken = new Token<BlockTokenIdentifier>();
    accessToken.readFields(in);
    if (datanode.isBlockTokenEnabled) {
        try {
            datanode.blockTokenSecretManager.checkAccess(accessToken, null, block,
                    BlockTokenSecretManager.AccessMode.COPY);
        } catch (InvalidToken e) {
            LOG.warn("Invalid access token in request from " + remoteAddress + " for OP_COPY_BLOCK for block "
                    + block);
            sendResponse(s, (short) DataTransferProtocol.OP_STATUS_ERROR_ACCESS_TOKEN,
                    datanode.socketWriteTimeout);
            return;
        }
    }

    if (!dataXceiverServer.balanceThrottler.acquire()) { // not able to start
        LOG.info("Not able to copy block " + blockId + " to " + s.getRemoteSocketAddress()
                + " because threads quota is exceeded.");
        sendResponse(s, (short) DataTransferProtocol.OP_STATUS_ERROR, datanode.socketWriteTimeout);
        return;
    }

    BlockSender blockSender = null;
    DataOutputStream reply = null;
    boolean isOpSuccess = true;

    try {
        // check if the block exists or not
        blockSender = new BlockSender(block, 0, -1, false, false, false, datanode);

        // set up response stream
        OutputStream baseStream = NetUtils.getOutputStream(s, datanode.socketWriteTimeout);
        reply = new DataOutputStream(new BufferedOutputStream(baseStream, SMALL_BUFFER_SIZE));

        // send status first
        reply.writeShort((short) DataTransferProtocol.OP_STATUS_SUCCESS);
        // send block content to the target
        long read = blockSender.sendBlock(reply, baseStream, dataXceiverServer.balanceThrottler);

        datanode.myMetrics.incrBytesRead((int) read);
        datanode.myMetrics.incrBlocksRead();

        LOG.info("Copied block " + block + " to " + s.getRemoteSocketAddress());
    } catch (IOException ioe) {
        isOpSuccess = false;
        throw ioe;
    } finally {
        dataXceiverServer.balanceThrottler.release();
        if (isOpSuccess) {
            try {
                // send one last byte to indicate that the resource is cleaned.
                reply.writeChar('d');
            } catch (IOException ignored) {
            }
        }
        IOUtils.closeStream(reply);
        IOUtils.closeStream(blockSender);
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.DWRRDataXceiver.java

@Override
public void copyBlock(final ExtendedBlock block, final Token<BlockTokenIdentifier> blockToken)
        throws IOException {
    updateCurrentThreadName("Copying block " + block);
    // Read in the header
    if (datanode.isBlockTokenEnabled) {
        try {/*from  w  w  w  . ja  v  a 2  s  . c om*/
            datanode.blockPoolTokenSecretManager.checkAccess(blockToken, null, block,
                    BlockTokenSecretManager.AccessMode.COPY);
        } catch (InvalidToken e) {
            LOG.warn("Invalid access token in request from " + remoteAddress + " for OP_COPY_BLOCK for block "
                    + block + " : " + e.getLocalizedMessage());
            sendResponse(ERROR_ACCESS_TOKEN, "Invalid access token");
            return;
        }

    }

    if (!DataXceiverServer.balanceThrottler.acquire()) { // not able to start
        String msg = "Not able to copy block " + block.getBlockId() + " " + "to "
                + peer.getRemoteAddressString() + " because threads " + "quota is exceeded.";
        LOG.info(msg);
        sendResponse(ERROR, msg);
        return;
    }

    BlockSender blockSender = null;
    DataOutputStream reply = null;
    boolean isOpSuccess = true;

    try {
        // check if the block exists or not
        blockSender = new BlockSender(block, 0, -1, false, false, true, datanode, null,
                CachingStrategy.newDropBehind());

        // set up response stream
        OutputStream baseStream = getOutputStream();
        reply = new DataOutputStream(new BufferedOutputStream(baseStream, HdfsConstants.SMALL_BUFFER_SIZE));

        // send status first
        writeSuccessWithChecksumInfo(blockSender, reply);
        // send block content to the target
        long read = blockSender.sendBlock(reply, baseStream, DataXceiverServer.balanceThrottler);

        datanode.metrics.incrBytesRead((int) read);
        datanode.metrics.incrBlocksRead();

        LOG.info("Copied " + block + " to " + peer.getRemoteAddressString());
    } catch (IOException ioe) {
        isOpSuccess = false;
        LOG.info("opCopyBlock " + block + " received exception " + ioe);
        throw ioe;
    } finally {
        DataXceiverServer.balanceThrottler.release();
        if (isOpSuccess) {
            try {
                // send one last byte to indicate that the resource is cleaned.
                reply.writeChar('d');
            } catch (IOException ignored) {
            }
        }
        IOUtils.closeStream(reply);
        IOUtils.closeStream(blockSender);
    }

    //update metrics    
    datanode.metrics.addCopyBlockOp(elapsed());
}

From source file:org.apache.hadoop.io.TestArrayOutputStream.java

private void runComparison(ArrayOutputStream aos, DataOutputStream dos, ByteArrayOutputStream bos)
        throws IOException {
    Random r = new Random();
    // byte/*from w  w  w .j  ava2 s .co  m*/
    int b = r.nextInt(128);
    aos.write(b);
    dos.write(b);

    // byte[]
    byte[] bytes = new byte[10];
    r.nextBytes(bytes);
    aos.write(bytes, 0, 10);
    dos.write(bytes, 0, 10);

    // Byte
    aos.writeByte(b);
    dos.writeByte(b);

    // boolean
    boolean bool = r.nextBoolean();
    aos.writeBoolean(bool);
    dos.writeBoolean(bool);

    // short
    short s = (short) r.nextInt();
    aos.writeShort(s);
    dos.writeShort(s);

    // char
    int c = r.nextInt();
    aos.writeChar(c);
    dos.writeChar(c);

    // int
    int i = r.nextInt();
    aos.writeInt(i);
    dos.writeInt(i);

    // long
    long l = r.nextLong();
    aos.writeLong(l);
    dos.writeLong(l);

    // float
    float f = r.nextFloat();
    aos.writeFloat(f);
    dos.writeFloat(f);

    // double
    double d = r.nextDouble();
    aos.writeDouble(d);
    dos.writeDouble(d);

    // strings
    String str = RandomStringUtils.random(20);
    aos.writeBytes(str);
    aos.writeChars(str);
    aos.writeUTF(str);
    dos.writeBytes(str);
    dos.writeChars(str);
    dos.writeUTF(str);

    byte[] expected = bos.toByteArray();
    assertEquals(expected.length, aos.size());

    byte[] actual = new byte[aos.size()];
    System.arraycopy(aos.getBytes(), 0, actual, 0, actual.length);
    // serialized bytes should be the same
    assertTrue(Arrays.equals(expected, actual));
}

From source file:org.apache.jxtadoop.hdfs.server.datanode.DataXceiver.java

/**
 * Read a block from the disk and then sends it to a destination.
 * //from   w  w  w  .jav a  2 s . c o  m
 * @param in The stream to read from
 * @throws IOException
 */
private void copyBlock(DataInputStream in) throws IOException {
    LOG.debug("Mathod called : copyBlock()");
    // Read in the header
    long blockId = in.readLong(); // read block id
    Block block = new Block(blockId, 0, in.readLong());

    if (!dataXceiverServer.balanceThrottler.acquire()) { // not able to start
        LOG.info("Not able to copy block " + blockId + " to " + s.getRemoteSocketAddress()
                + " because threads quota is exceeded.");
        return;
    }

    BlockSender blockSender = null;
    DataOutputStream reply = null;
    boolean isOpSuccess = true;

    try {
        // check if the block exists or not
        blockSender = new BlockSender(block, 0, -1, false, false, false, datanode);

        // set up response stream
        //OutputStream baseStream = NetUtils.getOutputStream(s, datanode.socketWriteTimeout);
        OutputStream baseStream = s.getOutputStream();

        //reply = new DataOutputStream(new BufferedOutputStream(
        //    baseStream, SMALL_BUFFER_SIZE));
        LOG.debug("Replying to DFS client");
        reply = new DataOutputStream(new BufferedOutputStream(baseStream));

        // send block content to the target
        long read = blockSender.sendBlock(reply, baseStream, dataXceiverServer.balanceThrottler);

        datanode.myMetrics.bytesRead.inc((int) read);
        datanode.myMetrics.blocksRead.inc();

        LOG.info("Copied block " + block + " to " + s.getRemoteSocketAddress());
    } catch (IOException ioe) {
        isOpSuccess = false;
        throw ioe;
    } finally {
        dataXceiverServer.balanceThrottler.release();
        if (isOpSuccess) {
            try {
                // send one last byte to indicate that the resource is cleaned.
                reply.writeChar('d');
            } catch (IOException ignored) {
            }
        }
        LOG.debug("Finalizing : copyBlock()");
        IOUtils.closeStream(reply);
        IOUtils.closeStream(blockSender);
    }
}

From source file:org.structr.core.graph.SyncCommand.java

private static void writeObject(final DataOutputStream outputStream, final byte type, final Object value)
        throws IOException {

    switch (type) {

    case 0://  ww  w  .  jav a  2  s  .  co  m
    case 1:
        outputStream.writeByte((byte) value);
        break;

    case 2:
    case 3:
        outputStream.writeShort((short) value);
        break;

    case 4:
    case 5:
        outputStream.writeInt((int) value);
        break;

    case 6:
    case 7:
        outputStream.writeLong((long) value);
        break;

    case 8:
    case 9:
        outputStream.writeFloat((float) value);
        break;

    case 10:
    case 11:
        outputStream.writeDouble((double) value);
        break;

    case 12:
    case 13:
        outputStream.writeChar((char) value);
        break;

    case 14:
    case 15:
        serializeData(outputStream, ((String) value).getBytes("UTF-8"));

        // this doesn't work with very long strings
        //outputStream.writeUTF((String)value);
        break;

    case 16:
    case 17:
        outputStream.writeBoolean((boolean) value);
        break;
    }
}