Package org.lilyproject.bytes.api

Examples of org.lilyproject.bytes.api.ByteArray


        }
        return value;
    }

    private ByteArray generateByteArray() {
        ByteArray value = null;
        // Default
        if (properties == null) {
            byte[] bytes = new byte[random.nextInt(100)];
            random.nextBytes(bytes);
            value = new ByteArray(bytes);
        } else {
            int length = JsonUtil.getInt(properties, "length", 100);
            byte[] bytes = new byte[length];
            random.nextBytes(bytes);
            value = new ByteArray(bytes);
        }
        return value;
    }
View Full Code Here


                        .value("intfield", 55)
                        .value("longfield", 999999999999L)
                        .value("booleanfield", Boolean.TRUE)
                        .value("floatfield", 33.33f)
                        .value("doublefield", 66.66d)
                        .value("binaryfield", new ByteArray("foo".getBytes()))
                        .build());

        record.setMetadata(new QName("ns", "field2"),
                new MetadataBuilder()
                        .value("stringfield", "another_string")
                        .build());

        ObjectNode recordNode = RecordWriter.INSTANCE.toJson(record, null, repository);

        // go through ser/deser
        String recordJson = JsonFormat.serializeAsString(recordNode);
        recordNode = (ObjectNode)JsonFormat.deserializeNonStd(recordJson);

        // Do some structural validations of the json
        ObjectNode metadataNode = (ObjectNode)recordNode.get("metadata");
        assertNotNull(metadataNode);
        assertNull(recordNode.get("metadataToDelete"));

        String prefix = recordNode.get("namespaces").get("ns").getTextValue();

        assertEquals(2, metadataNode.size());

        assertTrue(metadataNode.get(prefix + "$field1").get("stringfield").isTextual());
        assertEquals("string", metadataNode.get(prefix + "$field1").get("stringfield").getTextValue());

        assertTrue(metadataNode.get(prefix + "$field1").get("intfield").isInt());
        assertEquals(55, metadataNode.get(prefix + "$field1").get("intfield").getIntValue());

        assertTrue(metadataNode.get(prefix + "$field1").get("longfield").isLong());
        assertEquals(999999999999L, metadataNode.get(prefix + "$field1").get("longfield").getLongValue());

        assertTrue(metadataNode.get(prefix + "$field1").get("booleanfield").isBoolean());
        assertEquals(Boolean.TRUE, metadataNode.get(prefix + "$field1").get("booleanfield").getBooleanValue());

        // in JSON, no distinction between floats & doubles
        assertTrue(metadataNode.get(prefix + "$field1").get("floatfield").isFloatingPointNumber());
        assertEquals(33.33d, metadataNode.get(prefix + "$field1").get("floatfield").getDoubleValue(), 0.001);

        assertTrue(metadataNode.get(prefix + "$field1").get("doublefield").isFloatingPointNumber());
        assertEquals(33.33d, metadataNode.get(prefix + "$field1").get("floatfield").getDoubleValue(), 0.001);

        assertTrue(metadataNode.get(prefix + "$field1").get("binaryfield").isObject());
        ObjectNode binaryNode = (ObjectNode)metadataNode.get(prefix + "$field1").get("binaryfield");
        assertEquals("binary", binaryNode.get("type").getTextValue());
        assertArrayEquals("foo".getBytes(), binaryNode.get("value").getBinaryValue());

        assertTrue(metadataNode.get(prefix + "$field2").get("stringfield").isTextual());
        assertEquals("another_string", metadataNode.get(prefix + "$field2").get("stringfield").getTextValue());

        // Now parse json again to API objects
        record = RecordReader.INSTANCE.fromJson(recordNode, repository);
        assertEquals(2, record.getMetadataMap().size());

        Metadata metadata = record.getMetadata(new QName("ns", "field1"));
        assertEquals("string", metadata.get("stringfield"));
        assertEquals(55, metadata.getInt("intfield", null).intValue());
        assertEquals(999999999999L, metadata.getLong("longfield", null).longValue());
        assertEquals(Boolean.TRUE, metadata.getBoolean("booleanfield", null));
        assertEquals(33.33f, metadata.getFloat("floatfield", null), 0.001);
        assertEquals(66.66d, metadata.getDouble("doublefield", null), 0.001);
        assertEquals(new ByteArray("foo".getBytes()), metadata.getBytes("binaryfield"));

        metadata = record.getMetadata(new QName("ns", "field2"));
        assertEquals(1, metadata.getMap().size());
        assertEquals(0, metadata.getFieldsToDelete().size());
    }
View Full Code Here

        }

        @Override
        public Object deserialize(DataInput dataInput) {
            int size = dataInput.readVInt();
            return new ByteArray(dataInput.readBytes(size));
        }
View Full Code Here

                "field1"), Scope.NON_VERSIONED);
        RecordType recordType = typeManager.recordTypeBuilder().defaultNamespace("testByteArrayValueType")
                .name("recordType1")
                .field(byteArrayValueType.getId(), false).create();
        Record record = repository.recordBuilder().defaultNamespace("testByteArrayValueType").recordType("recordType1")
                .field("field1", new ByteArray(Bytes.toBytes("some bytes"))).create();
        Record readRecord = repository.read(record.getId());
        ByteArray readValue = readRecord.getField(new QName("testByteArrayValueType", "field1"));
        assertEquals("some bytes", Bytes.toString(readValue.getBytesUnsafe()));
    }
View Full Code Here

        testValues.add(Pair.create(new Integer(1), new Integer(2)));
        testValues.add(Pair.create(new Long(1), new Long(2)));
        testValues.add(Pair.create(new Float(1f), new Float(2f)));
        testValues.add(Pair.create(new Double(1d), new Double(2d)));
        testValues.add(Pair.create(Boolean.TRUE, Boolean.FALSE));
        testValues.add(Pair.create(new ByteArray("A".getBytes()), new ByteArray("B".getBytes())));

        for (Pair testValue : testValues) {
            String ctx = "testing " + testValue.getV1() + " of type " + testValue.getV1().getClass().getName();

            Record record = repository.newRecord();
View Full Code Here

            throws JsonFormatException, RepositoryException, InterruptedException {
        if (!handle.node.isTextual()) {
            throw new JsonFormatException("Expected base64 encoded value for property '" + handle.prop + "'");
        }
        try {
            return new ByteArray(handle.node.getBinaryValue());
        } catch (IOException e) {
            throw new JsonFormatException("Could not read base64 value for property '" + handle.prop + "'", e);
        }
    }
View Full Code Here

                        throw new JsonFormatException("Invalid binary value for metadata field '"
                                + name + "' of record field " + recordField);
                    }

                    try {
                        builder.value(name, new ByteArray(binaryValue.getBinaryValue()));
                    } catch (IOException e) {
                        throw new JsonFormatException("Invalid binary value for metadata field '"
                                + name + "' of record field " + recordField);
                    }
                } else if (type.equals("datetime")) {
View Full Code Here

        runValueTypeTests("uriRecordTypeId", "URI", URI.create("http://foo.com/bar"), URI.create("file://foo/com/bar.txt"), URI.create("https://site/index.html"));
    }

    @Test
    public void testByteArrayType() throws Exception {
        runValueTypeTests("byteArrayRecordTypeId", "BYTEARRAY", new ByteArray(Bytes.toBytes("bytes1")), new ByteArray(
                Bytes.toBytes("bytes2")), new ByteArray(Bytes.toBytes("bbb")));
    }
View Full Code Here

        return ByteArray.wrap(dataInput.readBytes(length));
    }

    @Override
    public void write(Object value, DataOutput dataOutput, IdentityRecordStack parentRecords) {
        ByteArray byteArray = (ByteArray) value;
        dataOutput.writeInt(byteArray.length());
        // We can use getBytesUnsafe here since we know that in this use case
        // the
        // dataOutput will be a DataOutputImpl which copies the byte[] when
        // writing it.
        dataOutput.writeBytes(byteArray.getBytesUnsafe());
    }
View Full Code Here

                .value("int", 5)
                .value("long", 99999999999L)
                .value("float", 3.33f)
                .value("double", 6.66d)
                .value("boolean", Boolean.TRUE)
                .value("bytes", new ByteArray("foobar".getBytes()))
                .value("date", time)
                .build();

        DataOutput output = new DataOutputImpl();
        MetadataSerDeser.write(metadata, output);
View Full Code Here

TOP

Related Classes of org.lilyproject.bytes.api.ByteArray

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.