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:org.apache.vxquery.jsonparser.JSONParser.java

public int parse(Reader input, ArrayBackedValueStorage result) throws HyracksDataException {
    int items = 0;
    try {//from www .  j a  v  a 2 s  .  c  o m
        DataOutput outResult = result.getDataOutput();
        JsonParser parser = factory.createParser(input);
        JsonToken token = parser.nextToken();
        checkItem = null;
        levelArray = 0;
        levelObject = 0;
        sb.reset(result);
        while (token != null) {
            if (itemStack.size() > 1) {
                checkItem = itemStack.get(itemStack.size() - 2);
            }
            switch (token) {
            case START_ARRAY:
                startArray();
                break;
            case START_OBJECT:
                startObject();
                break;
            case FIELD_NAME:
                startFieldName(parser);
                break;
            case VALUE_NUMBER_INT:
                startAtomicValues(ValueTag.XS_INTEGER_TAG, parser);
                break;
            case VALUE_STRING:
                startAtomicValues(ValueTag.XS_STRING_TAG, parser);
                break;
            case VALUE_NUMBER_FLOAT:
                startAtomicValues(ValueTag.XS_DOUBLE_TAG, parser);
                break;
            case END_ARRAY:
                abStack.get(levelArray - 1).finish();
                if (itemStack.size() > 1) {
                    if (checkItem == itemType.ARRAY) {
                        abStack.get(levelArray - 2).addItem(abvsStack.get(levelArray + levelObject));
                    } else if (checkItem == itemType.OBJECT) {
                        obStack.get(levelObject - 1).addItem(spStack.get(levelObject - 1),
                                abvsStack.get(levelArray + levelObject));
                    }
                }
                itemStack.remove(itemStack.size() - 1);
                levelArray--;
                if (levelArray + levelObject == 0) {
                    sb.addItem(abvsStack.get(1));
                    items++;
                }
                break;
            case END_OBJECT:
                obStack.get(levelObject - 1).finish();
                if (itemStack.size() > 1) {
                    if (checkItem == itemType.OBJECT) {
                        obStack.get(levelObject - 2).addItem(spStack.get(levelObject - 2),
                                abvsStack.get(levelArray + levelObject));
                    } else if (checkItem == itemType.ARRAY) {
                        abStack.get(levelArray - 1).addItem(abvsStack.get(levelArray + levelObject));
                    }
                }
                itemStack.remove(itemStack.size() - 1);
                levelObject--;
                if (levelObject + levelArray == 0) {
                    sb.addItem(abvsStack.get(1));
                    items++;
                }
                break;
            default:
                break;
            }
            token = parser.nextToken();
        }
        sb.finish();
        outResult.write(result.getByteArray());
    } catch (Exception e) {
        throw new HyracksDataException("Accessing or writing in out of bounds space", e);
    }
    return items;
}

From source file:org.apache.vxquery.jsonparser.JSONParser.java

public void atomicValues(int tag, JsonParser parser, DataOutput out, StringValueBuilder svb, int levelArray,
        int levelObject) throws IOException {
    abvsStack.get(0).reset();/* ww w.j a  va  2 s .com*/
    out.write(tag);
    if (tag == ValueTag.XS_DOUBLE_TAG) {
        out.writeDouble(parser.getDoubleValue());
    } else if (tag == ValueTag.XS_STRING_TAG) {
        svb.write(parser.getText(), out);
    } else if (tag == ValueTag.XS_INTEGER_TAG) {
        out.writeLong(parser.getLongValue());
    }
    if (!itemStack.isEmpty()) {
        if (itemStack.get(itemStack.size() - 1) == itemType.ARRAY) {
            abStack.get(levelArray - 1).addItem(abvsStack.get(0));
            if (valueSeq != null && this.matched && levelArray == this.arrayMatchLevel) {
                this.literal = true;
                this.matched = false;
                writeElement(abvsStack.get(0));
            }
        } else if (itemStack.get(itemStack.size() - 1) == itemType.OBJECT) {
            obStack.get(levelObject - 1).addItem(spStack.get(levelObject - 1), abvsStack.get(0));
            if (valueSeq != null && this.matched && levelObject == this.objectMatchLevel) {
                this.literal = true;
                this.matched = false;
                writeElement(abvsStack.get(0));
            }
        }
    }
}

From source file:org.apache.vxquery.runtime.functions.cast.CastToBase64BinaryOperation.java

@Override
public void convertBase64Binary(XSBinaryPointable binaryp, DataOutput dOut)
        throws SystemException, IOException {
    dOut.write(ValueTag.XS_BASE64_BINARY_TAG);
    dOut.write(binaryp.getByteArray(), binaryp.getStartOffset(), binaryp.getLength());
}

From source file:org.apache.vxquery.runtime.functions.cast.CastToBase64BinaryOperation.java

@Override
public void convertHexBinary(XSBinaryPointable binaryp, DataOutput dOut) throws SystemException, IOException {
    dOut.write(ValueTag.XS_BASE64_BINARY_TAG);
    dOut.write(binaryp.getByteArray(), binaryp.getStartOffset(), binaryp.getLength());
}

From source file:org.apache.vxquery.runtime.functions.cast.CastToBase64BinaryOperation.java

@Override
public void convertString(UTF8StringPointable stringp, DataOutput dOut) throws SystemException, IOException {
    baaos.reset();/*from   ww  w  . jav  a 2 s .c  o  m*/
    Base64OutputStream b64os = new Base64OutputStream(baaos, false);
    b64os.write(stringp.getByteArray(), stringp.getStartOffset() + 2, stringp.getLength() - 2);

    dOut.write(ValueTag.XS_BASE64_BINARY_TAG);
    dOut.write((byte) ((baaos.size() >>> 8) & 0xFF));
    dOut.write((byte) ((baaos.size() >>> 0) & 0xFF));
    dOut.write(baaos.getByteArray(), 0, baaos.size());
}

From source file:org.apache.vxquery.runtime.functions.cast.CastToStringOperation.java

@Override
public void convertAnyURI(UTF8StringPointable stringp, DataOutput dOut) throws SystemException, IOException {
    dOut.write(returnTag);
    dOut.write(stringp.getByteArray(), stringp.getStartOffset(), stringp.getLength());
}

From source file:org.apache.vxquery.runtime.functions.cast.CastToStringOperation.java

@Override
public void convertBase64Binary(XSBinaryPointable binaryp, DataOutput dOut)
        throws SystemException, IOException {
    baaos.reset();//from ww  w  .j av  a 2 s.c om
    @SuppressWarnings("resource")
    Base64OutputStream b64os = new Base64OutputStream(baaos, true);
    b64os.write(binaryp.getByteArray(), binaryp.getStartOffset() + 2, binaryp.getLength() - 2);

    dOut.write(returnTag);
    dOut.write((byte) ((baaos.size() >>> 8) & 0xFF));
    dOut.write((byte) ((baaos.size() >>> 0) & 0xFF));
    dOut.write(baaos.getByteArray(), 0, baaos.size());
}

From source file:org.apache.vxquery.runtime.functions.cast.CastToStringOperation.java

@Override
public void convertDouble(DoublePointable doublep, DataOutput dOut) throws SystemException, IOException {
    abvsInner.reset();/* w  w  w  .  ja va  2  s  .  c  o  m*/
    double value = doublep.getDouble();

    if (Double.isInfinite(value)) {
        if (value == Double.NEGATIVE_INFINITY) {
            FunctionHelper.writeCharSequence("-", dOutInner);
        }
        FunctionHelper.writeCharSequence("INF", dOutInner);
        sendStringDataOutput(dOut);
    } else if (Double.isNaN(value)) {
        FunctionHelper.writeCharSequence("NaN", dOutInner);
        sendStringDataOutput(dOut);
    } else if (value == -0.0 || value == 0.0) {
        long bits = Double.doubleToLongBits(value);
        boolean negative = ((bits >> 63) == 0) ? false : true;

        if (negative) {
            FunctionHelper.writeChar('-', dOutInner);
        }
        FunctionHelper.writeCharSequence("0", dOutInner);
        sendStringDataOutput(dOut);
    } else if (Math.abs(value) >= 0.000001 && Math.abs(value) <= 10000000) {
        //the jdk (toString function) does not output number in desired format when 
        //a number is between one and ten million, so we take care of this 
        //case separately here.
        CastToDecimalOperation castToDecimal = new CastToDecimalOperation();
        castToDecimal.convertDouble(doublep, dOutInner);
        XSDecimalPointable decp = (XSDecimalPointable) XSDecimalPointable.FACTORY.createPointable();
        decp.set(abvsInner.getByteArray(), abvsInner.getStartOffset() + 1,
                XSDecimalPointable.TYPE_TRAITS.getFixedLength());
        if (Math.abs(value) <= 1000000) {
            convertDecimal(decp, dOut);
        } else {
            decimalToScientificNotn(decp, dOut);
        }
    } else {
        dOut.write(returnTag);
        dOut.writeUTF(Double.toString(value));
    }
}

From source file:org.apache.vxquery.runtime.functions.cast.CastToStringOperation.java

public void convertDoubleCanonical(DoublePointable doublep, DataOutput dOut)
        throws SystemException, IOException {
    abvsInner.reset();/*from  w  ww .  j ava2s .co  m*/
    double value = doublep.getDouble();

    dOut.write(returnTag);
    dOut.writeUTF(Double.toString(value));
    return;
}

From source file:org.apache.vxquery.runtime.functions.cast.CastToStringOperation.java

public void convertFloatCanonical(FloatPointable floatp, DataOutput dOut) throws SystemException, IOException {
    abvsInner.reset();//  www .ja  v  a 2  s .c  o  m
    float value = floatp.getFloat();

    if (Float.isInfinite(value)) {
        if (value == Float.NEGATIVE_INFINITY) {
            FunctionHelper.writeCharSequence("-", dOutInner);
        }
        FunctionHelper.writeCharSequence("INF", dOutInner);
    } else if (Float.isNaN(value)) {
        FunctionHelper.writeCharSequence("NaN", dOutInner);
    } else {
        dOut.write(returnTag);
        dOut.writeUTF(Float.toString(value));
        return;
    }
    sendStringDataOutput(dOut);
}