Example usage for java.io DataOutput writeByte

List of usage examples for java.io DataOutput writeByte

Introduction

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

Prototype

void writeByte(int v) throws IOException;

Source Link

Document

Writes to the output stream the eight low- order bits of the argument v.

Usage

From source file:org.apache.marmotta.kiwi.io.KiWiIO.java

/**
 * Efficiently serialize a KiWiNode to a DataOutput destination. The type of node will be encoded with a single
 * byte usinbg the TYPE_* constants defined in this class
 *
 * @param output  DataOutput destination
 * @param node  KiWiNode to serialize// w  ww.  ja v  a  2 s. com
 * @throws IOException
 */
public static void writeNode(DataOutput output, KiWiNode node) throws IOException {
    if (node == null) {
        output.writeByte(0);
    } else {
        int type = classTable.get(node.getClass());
        output.writeByte(type);
        switch (type) {
        case TYPE_URI:
            writeURI(output, (KiWiUriResource) node);
            break;
        case TYPE_BNODE:
            writeBNode(output, (KiWiAnonResource) node);
            break;
        case TYPE_BOOLEAN:
            writeBooleanLiteral(output, (KiWiBooleanLiteral) node);
            break;
        case TYPE_DATE:
            writeDateLiteral(output, (KiWiDateLiteral) node);
            break;
        case TYPE_DOUBLE:
            writeDoubleLiteral(output, (KiWiDoubleLiteral) node);
            break;
        case TYPE_INT:
            writeIntLiteral(output, (KiWiIntLiteral) node);
            break;
        case TYPE_STRING:
            writeStringLiteral(output, (KiWiStringLiteral) node);
            break;
        default:
            throw new IllegalArgumentException("unknown KiWiNode type: " + node.getClass());
        }
    }
}

From source file:org.apache.marmotta.kiwi.io.KiWiIO.java

/**
 * Efficiently serialize a KiWiUriResource to a DataOutput destination, using prefix compression for commonly used
 * prefixes./*  w ww  .  j  a  va  2  s .com*/
 *
 * @param out  DataOutput destination
 * @param uri  KiWiUriResource to serialize
 * @throws IOException
 */
public static void writeURI(DataOutput out, KiWiUriResource uri) throws IOException {
    if (uri == null) {
        out.writeLong(-1L);
    } else {
        out.writeLong(uri.getId());

        // compression for commonly used constant prefixes
        if (uri.stringValue().startsWith(XSD.NAMESPACE)) {
            out.writeByte(PREFIX_XSD);
            DataIO.writeString(out, uri.stringValue().substring(XSD.NAMESPACE.length()));
        } else if (uri.stringValue().startsWith(RDF.NAMESPACE)) {
            out.writeByte(PREFIX_RDF);
            DataIO.writeString(out, uri.stringValue().substring(RDF.NAMESPACE.length()));
        } else if (uri.stringValue().startsWith(RDFS.NAMESPACE)) {
            out.writeByte(PREFIX_RDFS);
            DataIO.writeString(out, uri.stringValue().substring(RDFS.NAMESPACE.length()));
        } else if (uri.stringValue().startsWith(SKOS.NAMESPACE)) {
            out.writeByte(PREFIX_SKOS);
            DataIO.writeString(out, uri.stringValue().substring(SKOS.NAMESPACE.length()));
        } else if (uri.stringValue().startsWith(DC.NAMESPACE)) {
            out.writeByte(PREFIX_DC);
            DataIO.writeString(out, uri.stringValue().substring(DC.NAMESPACE.length()));
        } else if (uri.stringValue().startsWith(DCTERMS.NAMESPACE)) {
            out.writeByte(PREFIX_DCT);
            DataIO.writeString(out, uri.stringValue().substring(DCTERMS.NAMESPACE.length()));
        } else if (uri.stringValue().startsWith(OWL.NAMESPACE)) {
            out.writeByte(PREFIX_OWL);
            DataIO.writeString(out, uri.stringValue().substring(OWL.NAMESPACE.length()));
        } else if (uri.stringValue().startsWith(SCHEMA.NAMESPACE)) {
            out.writeByte(PREFIX_SCHEMA);
            DataIO.writeString(out, uri.stringValue().substring(SCHEMA.NAMESPACE.length()));
        } else if (uri.stringValue().startsWith(NS_REDLINK)) {
            out.writeByte(PREFIX_REDLINK);
            DataIO.writeString(out, uri.stringValue().substring(NS_REDLINK.length()));
        } else if (uri.stringValue().startsWith(NS_DBPEDIA)) {
            out.writeByte(PREFIX_DBPEDIA);
            DataIO.writeString(out, uri.stringValue().substring(NS_DBPEDIA.length()));
        } else if (uri.stringValue().startsWith(NS_FREEBASE)) {
            out.writeByte(PREFIX_FREEBASE);
            DataIO.writeString(out, uri.stringValue().substring(NS_FREEBASE.length()));
        } else if (uri.stringValue().startsWith(HTTP_LOCALHOST)) {
            out.writeByte(PREFIX_LOCAL);
            DataIO.writeString(out, uri.stringValue().substring(HTTP_LOCALHOST.length()));
        } else {
            out.writeByte(PREFIX_UNKNOWN);
            DataIO.writeString(out, uri.stringValue());
        }

        out.writeLong(uri.getCreated().getTime());
    }
}

From source file:org.apache.marmotta.kiwi.io.KiWiIO.java

/**
 * Efficiently serialize a KiWiStringLiteral to a DataOutput destination.
 *
 * @param out the destination// w  w  w . j  a va2  s.co m
 * @param literal the KiWiStringLiteral to serialize
 * @throws IOException
 */
public static void writeStringLiteral(DataOutput out, KiWiStringLiteral literal) throws IOException {
    if (literal == null) {
        out.writeLong(-1L);
    } else {
        out.writeLong(literal.getId());
        writeContent(out, literal.getContent());
        if (langTable.containsKey(literal.getLanguage())) {
            out.writeByte(langTable.get(literal.getLanguage()));
        } else {
            out.writeByte(LANG_UNKNOWN);
            DataIO.writeString(out, literal.getLanguage());
        }
        writeURI(out, literal.getType());
        out.writeLong(literal.getCreated().getTime());
    }
}

From source file:org.apache.marmotta.kiwi.io.KiWiIO.java

/**
 * Efficiently serialize a KiWiTriple to a DataOutput destination.
 *
 * @param output the destination//from w  ww  . j  a  va2 s .c o  m
 * @param triple the KiWiTriple to serialize
 * @throws IOException
 */
public static void writeTriple(DataOutput output, KiWiTriple triple) throws IOException {
    output.writeLong(triple.getId());

    // in case subject and object are both uris we use a special prefix-compressed mode
    if (triple.getSubject().isUriResource() && triple.getObject().isUriResource()) {
        String sUri = triple.getSubject().stringValue();
        String oUri = triple.getObject().stringValue();

        String prefix = StringUtils.getCommonPrefix(sUri, oUri);

        output.writeByte(MODE_PREFIX);
        DataIO.writeString(output, prefix);

        output.writeLong(triple.getSubject().getId());
        DataIO.writeString(output, sUri.substring(prefix.length()));
        output.writeLong(triple.getSubject().getCreated().getTime());

        writeURI(output, triple.getPredicate());

        output.writeLong(triple.getObject().getId());
        DataIO.writeString(output, oUri.substring(prefix.length()));
        output.writeLong(triple.getObject().getCreated().getTime());
    } else {
        output.writeByte(MODE_DEFAULT);

        writeNode(output, triple.getSubject());
        writeURI(output, triple.getPredicate());
        writeNode(output, triple.getObject());

    }

    writeNode(output, triple.getContext());
    writeNode(output, triple.getCreator());
    output.writeBoolean(triple.isDeleted());
    output.writeBoolean(triple.isInferred());
    output.writeBoolean(triple.isNewTriple());
    output.writeLong(triple.getCreated().getTime());
    if (triple.getDeletedAt() != null) {
        output.writeLong(triple.getDeletedAt().getTime());
    } else {
        output.writeLong(0);
    }
}

From source file:org.apache.marmotta.kiwi.io.KiWiIO.java

/**
 * Write a string to the data output. In case the string length exceeds LITERAL_COMPRESS_LENGTH, uses a LZW
 * compressed format, otherwise writes the plain bytes.
 *
 * @param out      output destination to write to
 * @param content  string to write/*  w w w  . ja v  a  2s .  c o m*/
 * @throws IOException
 */
private static void writeContent(DataOutput out, String content) throws IOException {
    if (content.length() > LITERAL_COMPRESS_LENGTH) {
        // temporary buffer of the size of bytes in the content string (assuming that the compressed data will fit into it)
        byte[] data = content.getBytes("UTF-8");
        byte[] buffer = new byte[data.length];

        Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION, true);
        compressor.setInput(data);
        compressor.finish();

        int length = compressor.deflate(buffer);

        // only use compressed version if it is smaller than the number of bytes used by the string
        if (length < buffer.length) {
            log.debug("compressed string with {} bytes; compression ratio {}", data.length,
                    (double) length / data.length);

            out.writeByte(MODE_COMPRESSED);
            out.writeInt(data.length);
            out.writeInt(length);
            out.write(buffer, 0, length);
        } else {
            log.warn("compressed length exceeds string buffer: {} > {}", length, buffer.length);

            out.writeByte(MODE_DEFAULT);
            DataIO.writeString(out, content);
        }

        compressor.end();
    } else {
        out.writeByte(MODE_DEFAULT);
        DataIO.writeString(out, content);
    }
}

From source file:org.apache.nutch.crawl.CrawlDatum.java

public void write(DataOutput out) throws IOException {
    out.writeByte(CUR_VERSION); // store current version
    out.writeByte(status);/* w ww. ja v a 2  s .  c o m*/
    out.writeLong(fetchTime);
    out.writeByte(retries);
    out.writeInt(fetchInterval);
    out.writeFloat(score);
    out.writeLong(modifiedTime);
    if (signature == null) {
        out.writeByte(0);
    } else {
        out.writeByte(signature.length);
        out.write(signature);
    }
    if (metaData != null && metaData.size() > 0) {
        out.writeBoolean(true);
        metaData.write(out);
    } else {
        out.writeBoolean(false);
    }
}

From source file:org.apache.nutch.crawl.MapWritable.java

public void write(DataOutput out) throws IOException {
    out.writeInt(size());/*from w  ww.  java2  s.  c  om*/

    if (size() > 0) {
        // scan for unknown classes;
        createInternalIdClassEntries();
        // write internal map
        out.writeByte(fIdCount);
        if (fIdCount > 0) {
            ClassIdEntry entry = fIdFirst;
            while (entry != null) {
                out.writeByte(entry.fId);
                Text.writeString(out, entry.fclazz.getName());
                entry = entry.fNextIdEntry;
            }
        }
        // write meta data
        KeyValueEntry entry = fFirst;
        while (entry != null) {
            out.writeByte(entry.fKeyClassId);
            out.writeByte(entry.fValueClassId);

            entry.fKey.write(out);
            entry.fValue.write(out);

            entry = entry.fNextEntry;
        }

    }

}

From source file:org.apache.nutch.parse.ParseData.java

public final void write(DataOutput out) throws IOException {
    out.writeByte(VERSION); // write version
    status.write(out); // write status
    Text.writeString(out, title); // write title

    out.writeInt(outlinks.length); // write outlinks
    for (int i = 0; i < outlinks.length; i++) {
        outlinks[i].write(out);// ww  w.j  a  v a 2s .c o m
    }
    contentMeta.write(out); // write content metadata
    parseMeta.write(out);
}

From source file:org.apache.nutch.searcher.Query.java

public void write(DataOutput out) throws IOException {
    out.writeByte(clauses.size());
    for (int i = 0; i < clauses.size(); i++)
        clauses.get(i).write(out);//from w  ww .j a va2  s. co m
    params.write(out);
}

From source file:org.apache.pig.data.BinInterSedes.java

@Override
@SuppressWarnings("unchecked")
public void writeDatum(DataOutput out, Object val, byte type) throws IOException {
    switch (type) {
    case DataType.TUPLE:
        writeTuple(out, (Tuple) val);
        break;/*from   w w  w .j  a  v a  2 s.c om*/

    case DataType.BAG:
        writeBag(out, (DataBag) val);
        break;

    case DataType.MAP: {
        writeMap(out, (Map<String, Object>) val);
        break;
    }

    case DataType.INTERNALMAP: {
        out.writeByte(INTERNALMAP);
        Map<Object, Object> m = (Map<Object, Object>) val;
        out.writeInt(m.size());
        Iterator<Map.Entry<Object, Object>> i = m.entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry<Object, Object> entry = i.next();
            writeDatum(out, entry.getKey());
            writeDatum(out, entry.getValue());
        }
        break;
    }

    case DataType.INTEGER:
        int i = (Integer) val;
        if (i == 0) {
            out.writeByte(INTEGER_0);
        } else if (i == 1) {
            out.writeByte(INTEGER_1);
        } else if (Byte.MIN_VALUE <= i && i <= Byte.MAX_VALUE) {
            out.writeByte(INTEGER_INBYTE);
            out.writeByte(i);
        } else if (Short.MIN_VALUE <= i && i <= Short.MAX_VALUE) {
            out.writeByte(INTEGER_INSHORT);
            out.writeShort(i);
        } else {
            out.writeByte(INTEGER);
            out.writeInt(i);
        }
        break;

    case DataType.LONG:
        long lng = (Long) val;
        if (lng == 0) {
            out.writeByte(LONG_0);
        } else if (lng == 1) {
            out.writeByte(LONG_1);
        } else if (Byte.MIN_VALUE <= lng && lng <= Byte.MAX_VALUE) {
            out.writeByte(LONG_INBYTE);
            out.writeByte((int) lng);
        } else if (Short.MIN_VALUE <= lng && lng <= Short.MAX_VALUE) {
            out.writeByte(LONG_INSHORT);
            out.writeShort((int) lng);
        } else if (Integer.MIN_VALUE <= lng && lng <= Integer.MAX_VALUE) {
            out.writeByte(LONG_ININT);
            out.writeInt((int) lng);
        } else {
            out.writeByte(LONG);
            out.writeLong(lng);
        }
        break;

    case DataType.DATETIME:
        out.writeByte(DATETIME);
        out.writeLong(((DateTime) val).getMillis());
        out.writeShort(((DateTime) val).getZone().getOffset((DateTime) val) / ONE_MINUTE);
        break;

    case DataType.FLOAT:
        out.writeByte(FLOAT);
        out.writeFloat((Float) val);
        break;

    case DataType.BIGINTEGER:
        out.writeByte(BIGINTEGER);
        writeBigInteger(out, (BigInteger) val);
        break;

    case DataType.BIGDECIMAL:
        out.writeByte(BIGDECIMAL);
        writeBigDecimal(out, (BigDecimal) val);
        break;

    case DataType.DOUBLE:
        out.writeByte(DOUBLE);
        out.writeDouble((Double) val);
        break;

    case DataType.BOOLEAN:
        if ((Boolean) val)
            out.writeByte(BOOLEAN_TRUE);
        else
            out.writeByte(BOOLEAN_FALSE);
        break;

    case DataType.BYTE:
        out.writeByte(BYTE);
        out.writeByte((Byte) val);
        break;

    case DataType.BYTEARRAY: {
        DataByteArray bytes = (DataByteArray) val;
        SedesHelper.writeBytes(out, bytes.mData);
        break;

    }

    case DataType.CHARARRAY: {
        SedesHelper.writeChararray(out, (String) val);
        break;
    }
    case DataType.GENERIC_WRITABLECOMPARABLE:
        out.writeByte(GENERIC_WRITABLECOMPARABLE);
        // store the class name, so we know the class to create on read
        writeDatum(out, val.getClass().getName());
        Writable writable = (Writable) val;
        writable.write(out);
        break;

    case DataType.NULL:
        out.writeByte(NULL);
        break;

    default:
        throw new RuntimeException("Unexpected data type " + val.getClass().getName() + " found in stream. "
                + "Note only standard Pig type is supported when you output from UDF/LoadFunc");
    }
}