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:bobs.is.compress.sevenzip.SevenZOutputFile.java

private void writeFileCTimes(final DataOutput header) throws IOException {
    int numCreationDates = 0;
    for (final SevenZArchiveEntry entry : files) {
        if (entry.getHasCreationDate()) {
            ++numCreationDates;/*from   w  w  w. j  a  v  a 2 s  .  co m*/
        }
    }
    if (numCreationDates > 0) {
        header.write(NID.kCTime);

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final DataOutputStream out = new DataOutputStream(baos);
        if (numCreationDates != files.size()) {
            out.write(0);
            final BitSet cTimes = new BitSet(files.size());
            for (int i = 0; i < files.size(); i++) {
                cTimes.set(i, files.get(i).getHasCreationDate());
            }
            writeBits(out, cTimes, files.size());
        } else {
            out.write(1); // "allAreDefined" == true
        }
        out.write(0);
        for (final SevenZArchiveEntry entry : files) {
            if (entry.getHasCreationDate()) {
                out.writeLong(
                        Long.reverseBytes(SevenZArchiveEntry.javaTimeToNtfsTime(entry.getCreationDate())));
            }
        }
        out.flush();
        final byte[] contents = baos.toByteArray();
        writeUint64(header, contents.length);
        header.write(contents);
    }
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

private void writeFileEmptyStreams(final DataOutput header) throws IOException {
    boolean hasEmptyStreams = false;
    for (final SevenZArchiveEntry entry : files) {
        if (!entry.hasStream()) {
            hasEmptyStreams = true;//from  ww  w .j  a va 2s  .  c  om
            break;
        }
    }
    if (hasEmptyStreams) {
        header.write(NID.kEmptyStream);
        final BitSet emptyStreams = new BitSet(files.size());
        for (int i = 0; i < files.size(); i++) {
            emptyStreams.set(i, !files.get(i).hasStream());
        }
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final DataOutputStream out = new DataOutputStream(baos);
        writeBits(out, emptyStreams, files.size());
        out.flush();
        final byte[] contents = baos.toByteArray();
        writeUint64(header, contents.length);
        header.write(contents);
    }
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

private void writeFileMTimes(final DataOutput header) throws IOException {
    int numLastModifiedDates = 0;
    for (final SevenZArchiveEntry entry : files) {
        if (entry.getHasLastModifiedDate()) {
            ++numLastModifiedDates;/*from  w w w .j ava  2 s .c o m*/
        }
    }
    if (numLastModifiedDates > 0) {
        header.write(NID.kMTime);

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final DataOutputStream out = new DataOutputStream(baos);
        if (numLastModifiedDates != files.size()) {
            out.write(0);
            final BitSet mTimes = new BitSet(files.size());
            for (int i = 0; i < files.size(); i++) {
                mTimes.set(i, files.get(i).getHasLastModifiedDate());
            }
            writeBits(out, mTimes, files.size());
        } else {
            out.write(1); // "allAreDefined" == true
        }
        out.write(0);
        for (final SevenZArchiveEntry entry : files) {
            if (entry.getHasLastModifiedDate()) {
                out.writeLong(
                        Long.reverseBytes(SevenZArchiveEntry.javaTimeToNtfsTime(entry.getLastModifiedDate())));
            }
        }
        out.flush();
        final byte[] contents = baos.toByteArray();
        writeUint64(header, contents.length);
        header.write(contents);
    }
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

private void writeFileWindowsAttributes(final DataOutput header) throws IOException {
    int numWindowsAttributes = 0;
    for (final SevenZArchiveEntry entry : files) {
        if (entry.getHasWindowsAttributes()) {
            ++numWindowsAttributes;// w ww .jav  a  2 s  .  c o m
        }
    }
    if (numWindowsAttributes > 0) {
        header.write(NID.kWinAttributes);

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final DataOutputStream out = new DataOutputStream(baos);
        if (numWindowsAttributes != files.size()) {
            out.write(0);
            final BitSet attributes = new BitSet(files.size());
            for (int i = 0; i < files.size(); i++) {
                attributes.set(i, files.get(i).getHasWindowsAttributes());
            }
            writeBits(out, attributes, files.size());
        } else {
            out.write(1); // "allAreDefined" == true
        }
        out.write(0);
        for (final SevenZArchiveEntry entry : files) {
            if (entry.getHasWindowsAttributes()) {
                out.writeInt(Integer.reverseBytes(entry.getWindowsAttributes()));
            }
        }
        out.flush();
        final byte[] contents = baos.toByteArray();
        writeUint64(header, contents.length);
        header.write(contents);
    }
}

From source file:edu.umn.cs.spatialHadoop.visualization.FrequencyMap.java

@Override
public void write(DataOutput out) throws IOException {
    super.write(out);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = new GZIPOutputStream(baos);
    ByteBuffer bbuffer = ByteBuffer.allocate(getHeight() * 4 + 8);
    bbuffer.putInt(getWidth());/*from  w w w  . j  a  v  a2s  .c  o m*/
    bbuffer.putInt(getHeight());
    gzos.write(bbuffer.array(), 0, bbuffer.position());
    for (int x = 0; x < getWidth(); x++) {
        bbuffer.clear();
        for (int y = 0; y < getHeight(); y++) {
            bbuffer.putFloat(frequencies[x][y]);
        }
        gzos.write(bbuffer.array(), 0, bbuffer.position());
    }
    gzos.close();

    byte[] serializedData = baos.toByteArray();
    out.writeInt(serializedData.length);
    out.write(serializedData);
}

From source file:com.fiorano.openesb.application.DmiObject.java

/**
 *  This method writes the string specified by the <code>str</code> argument
 *  to the output stream specified by the <code>os</code> argument.
 *
 * @param os output stream/*from w  w w  .  j av  a 2  s.  co  m*/
 * @param str string to be written
 * @exception IOException IOException
 */
protected void writeUTF(DataOutput os, String str) throws IOException {
    if (str == null) {
        os.writeInt(-1);
    } else {
        byte[] bytes = str.getBytes("UTF-8");

        os.writeInt(bytes.length);
        os.write(bytes);
    }
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

private void writeUint64(final DataOutput header, long value) throws IOException {
    int firstByte = 0;
    int mask = 0x80;
    int i;// w  ww .j  a va 2  s.c  o  m
    for (i = 0; i < 8; i++) {
        if (value < ((1L << (7 * (i + 1))))) {
            firstByte |= (value >>> (8 * i));
            break;
        }
        firstByte |= mask;
        mask >>>= 1;
    }
    header.write(firstByte);
    for (; i > 0; i--) {
        header.write((int) (0xff & value));
        value >>>= 8;
    }
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

private void writeFolder(final DataOutput header, final SevenZArchiveEntry entry) throws IOException {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int numCoders = 0;
    for (final SevenZMethodConfiguration m : getContentMethods(entry)) {
        numCoders++;//  w  w  w  .  j  a  v  a 2  s . co m
        writeSingleCodec(m, bos);
    }

    writeUint64(header, numCoders);
    header.write(bos.toByteArray());
    for (int i = 0; i < numCoders - 1; i++) {
        writeUint64(header, i + 1);
        writeUint64(header, i);
    }
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

private void writeFileAntiItems(final DataOutput header) throws IOException {
    boolean hasAntiItems = false;
    final BitSet antiItems = new BitSet(0);
    int antiItemCounter = 0;
    for (final SevenZArchiveEntry file1 : files) {
        if (!file1.hasStream()) {
            final boolean isAnti = file1.isAntiItem();
            antiItems.set(antiItemCounter++, isAnti);
            hasAntiItems |= isAnti;//ww  w  .j av  a2 s.c  o  m
        }
    }
    if (hasAntiItems) {
        header.write(NID.kAnti);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final DataOutputStream out = new DataOutputStream(baos);
        writeBits(out, antiItems, antiItemCounter);
        out.flush();
        final byte[] contents = baos.toByteArray();
        writeUint64(header, contents.length);
        header.write(contents);
    }
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

private void writeFileEmptyFiles(final DataOutput header) throws IOException {
    boolean hasEmptyFiles = false;
    int emptyStreamCounter = 0;
    final BitSet emptyFiles = new BitSet(0);
    for (final SevenZArchiveEntry file1 : files) {
        if (!file1.hasStream()) {
            final boolean isDir = file1.isDirectory();
            emptyFiles.set(emptyStreamCounter++, !isDir);
            hasEmptyFiles |= !isDir;//  w w w .  j  a  v a2 s. co  m
        }
    }
    if (hasEmptyFiles) {
        header.write(NID.kEmptyFile);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final DataOutputStream out = new DataOutputStream(baos);
        writeBits(out, emptyFiles, emptyStreamCounter);
        out.flush();
        final byte[] contents = baos.toByteArray();
        writeUint64(header, contents.length);
        header.write(contents);
    }
}