Example usage for java.io DataOutput write

List of usage examples for java.io DataOutput write

Introduction

In this page you can find the example usage for java.io DataOutput write.

Prototype

void write(byte b[]) throws IOException;

Source Link

Document

Writes to the output stream all the bytes in array b.

Usage

From source file:Main.java

public static void writeByteBuffer(byte[] buf, int offset, int length, DataOutput out) throws Exception {
    if (buf != null) {
        out.write(1);
        out.writeInt(length);/*from w w w . j  a  v  a2s  .  c om*/
        out.write(buf, offset, length);
    } else {
        out.write(0);
    }
}

From source file:backup.store.BackupUtil.java

public static void writeShortString(String s, DataOutput out) throws IOException {
    int length = s.length();
    byte[] bs = s.getBytes();
    out.writeShort(length);// w  w w . j  ava 2s .  c o m
    out.write(bs);
}

From source file:com.bah.culvert.util.Bytes.java

/**
 * Write the bytes to the output stream/*from  www . jav a  2  s  . co  m*/
 * @param out to write to
 * @param bytes to write
 * @throws IOException on failure to write
 */
public static void writeByteArray(DataOutput out, byte[] bytes) throws IOException {
    out.writeInt(bytes.length);
    out.write(bytes);
}

From source file:io.mycat.util.ByteBufferUtil.java

public static void writeWithLength(byte[] bytes, DataOutput out) throws IOException {
    out.writeInt(bytes.length);//  w  w  w  .  j a  va  2s . c o  m
    out.write(bytes);
}

From source file:com.bigdata.dastor.utils.FBUtilities.java

public static void writeByteArray(byte[] bytes, DataOutput out) throws IOException {
    out.writeInt(bytes.length);/*from www.j a  v a2  s . co m*/
    out.write(bytes);
}

From source file:com.linkedin.cubert.io.rubix.RubixFile.java

private static void extract(List<RubixFile<Tuple, Object>> rfiles, long blockId, int numBlocks, String output)
        throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
    Configuration conf = new JobConf();
    File outFile = new File(output);
    if (outFile.exists()) {
        outFile.delete();//w  w w .  jav a  2  s .  c o m
    }
    outFile.createNewFile();
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFile));
    ByteArrayOutputStream keySectionStream = new ByteArrayOutputStream();
    DataOutput keySectionOut = new DataOutputStream(keySectionStream);
    SerializationFactory serializationFactory = new SerializationFactory(conf);
    RubixFile<Tuple, Object> lastrFile = null;
    JsonNode json;
    long totalLength = 0;

    final int BUF_SIZE = 32 * 1024;
    long blockIds[] = new long[numBlocks];
    int foundBlocks = 0;

    for (int i = 0; i < numBlocks; i++)
        blockIds[i] = blockId + i;

    for (int i = 0; i < numBlocks; i++) {
        boolean found = false;
        for (RubixFile<Tuple, Object> rfile : rfiles) {
            print.f("Checking %s", rfile.path.toString());
            List<KeyData<Tuple>> keyDataList = rfile.getKeyData();
            for (KeyData<Tuple> keyData : keyDataList) {
                if (keyData.getBlockId() == blockIds[i]) {
                    long offset = keyData.getOffset();
                    long length = keyData.getLength();
                    Tuple key = keyData.getKey();
                    print.f("Extracting block %d (off=%d len=%d) from %s", keyData.getBlockId(), offset, length,
                            rfile.path.toString());

                    // copy the data
                    if (length > 0) {
                        FileSystem fs = FileSystem.get(conf);
                        FSDataInputStream in = fs.open(rfile.path);
                        in.seek(offset);

                        byte[] data = new byte[BUF_SIZE];
                        long toRead = length;
                        while (toRead > 0) {
                            int thisRead = toRead > BUF_SIZE ? BUF_SIZE : (int) toRead;
                            in.readFully(data, 0, thisRead);
                            bos.write(data, 0, thisRead);
                            toRead -= thisRead;
                            System.out.print(".");
                        }
                        System.out.println();
                    }
                    // copy the key section
                    Serializer<Tuple> keySerializer = serializationFactory.getSerializer(rfile.getKeyClass());
                    keySerializer.open(keySectionStream);

                    keySerializer.serialize(key);
                    keySectionOut.writeLong(totalLength); // position
                    keySectionOut.writeLong(keyData.getBlockId());
                    keySectionOut.writeLong(keyData.getNumRecords());
                    foundBlocks++;
                    totalLength += length;
                    lastrFile = rfile;

                    found = true;
                    break;

                }
            }
            if (found) {
                break;
            }
        }
        if (!found)
            System.err.println("Cannot locate block with id " + blockIds[i]);
    }
    byte[] trailerBytes = keySectionStream.toByteArray();

    json = JsonUtils.cloneNode(lastrFile.metadataJson);
    ((ObjectNode) json).put("numberOfBlocks", foundBlocks);

    DataOutput out = new DataOutputStream(bos);
    out.writeUTF(json.toString());
    out.writeInt(trailerBytes.length);
    out.write(trailerBytes);
    out.writeLong(totalLength); // trailer start offset
    bos.close();
}

From source file:com.bigdata.dastor.utils.FBUtilities.java

public static void serialize(TSerializer serializer, TBase struct, DataOutput out) throws IOException {
    assert serializer != null;
    assert struct != null;
    assert out != null;
    byte[] bytes;
    try {//  ww  w . j  a v a2  s.  c  o  m
        bytes = serializer.serialize(struct);
    } catch (TException e) {
        throw new RuntimeException(e);
    }
    out.writeInt(bytes.length);
    out.write(bytes);
}

From source file:com.iflytek.spider.parse.ParseText.java

public final void write(DataOutput out) throws IOException {
    out.write(VERSION);
    Text.writeString(out, text);//w w w.j  av a2 s . c o m
}

From source file:com.smartitengineering.cms.api.impl.workspace.WorkspaceIdImpl.java

@Override
public void writeExternal(DataOutput out) throws IOException {
    out.write(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(toString()));
}

From source file:com.smartitengineering.jetty.session.replication.SessionDataId.java

@Override
public void writeExternal(DataOutput output) throws IOException {
    output.write(StringUtils.getBytesUtf8(toString()));
}