Example usage for java.io DataInput readUTF

List of usage examples for java.io DataInput readUTF

Introduction

In this page you can find the example usage for java.io DataInput readUTF.

Prototype

String readUTF() throws IOException;

Source Link

Document

Reads in a string that has been encoded using a modified UTF-8 format.

Usage

From source file:org.jactr.io.antlr3.serialization.Serializer.java

static public CommonTree read(DataInput input) throws IOException {
    int nodeType = input.readInt();
    String nodeText = input.readUTF();

    int tokenType = input.readInt();
    String tokenText = input.readUTF();
    int tokenStart = input.readInt();
    int tokenStop = input.readInt();
    int tokenLine = input.readInt();
    int tokenLineOffset = input.readInt();

    CommonToken token = new CommonToken(tokenType, tokenText);
    token.setStartIndex(tokenStart);/*w w  w .  j  a v  a 2s. c  om*/
    token.setStopIndex(tokenStop);
    token.setLine(tokenLine);
    token.setCharPositionInLine(tokenLineOffset);

    DetailedCommonTree node = (DetailedCommonTree) _adaptor.create(nodeType, token, nodeText);
    node.setTokenStartIndex(input.readInt());
    node.setTokenStopIndex(input.readInt());

    node.setStartOffset(input.readInt());
    node.setEndOffset(input.readInt());

    String url = input.readUTF();
    if (url.length() != 0)
        try {
            node.setSource(new URL(url));
        } catch (Exception e) {

        }

    int children = input.readInt();
    for (int i = 0; i < children; i++)
        node.addChild(read(input));
    return node;
}

From source file:org.kiji.schema.mapreduce.KijiDelete.java

/**
 * During deserialization, read attributes that may have been NULL while serializing.
 *
 * @param in Interface to input stream.//  w  w  w. java  2 s . com
 * @param type The class to read.
 * @return An object of "type" or null.
 * @throws IOException if an I/O error occurs.
 */
private Object readOptionalValue(DataInput in, Class<?> type) throws IOException {
    String input = in.readUTF();
    if (input.equals("NULL")) {
        return null;
    }
    if (KijiDeleteOperation.class == type) {
        return KijiDeleteOperation.valueOf(input);
    }
    if (Long.class == type) {
        return new Long(input);
    }
    return input;
}

From source file:org.kiji.schema.mapreduce.KijiIncrement.java

/** {@inheritDoc} */
@Override/*  ww w.j a v a  2s.  c  o  m*/
public void readFields(DataInput in) throws IOException {
    // EntityId.
    final byte[] bytes = new byte[in.readInt()];
    in.readFully(bytes);
    mEntityId = new RawEntityId(bytes);

    // Family/Qualifier/Timestamp.
    mFamily = in.readUTF();
    mQualifier = in.readUTF();
    mAmount = in.readLong();
}

From source file:org.kiji.schema.mapreduce.KijiPut.java

/** {@inheritDoc} */
@Override//from   w  ww . j a  v  a2 s  .c  o  m
public void readFields(DataInput in) throws IOException {
    // EntityId.
    final byte[] bytes = new byte[in.readInt()];
    in.readFully(bytes);
    mEntityId = new HBaseEntityId(bytes);

    // Family/Qualifier/Timestamp.
    mFamily = in.readUTF();
    mQualifier = in.readUTF();
    mTimestamp = in.readLong();

    // Avro.
    final Schema schema = new Schema.Parser().parse(in.readUTF());
    final KijiCellDecoderFactory decoderFactory = new SpecificCellDecoderFactory(null);
    final KijiCellDecoder<?> decoder = decoderFactory.create(schema, KijiCellFormat.NONE);
    final byte[] cellData = new byte[in.readInt()];
    in.readFully(cellData);
    mCell = decoder.decode(cellData, null);
}

From source file:org.lilyproject.repository.impl.hbase.LilyFieldSingleColumnValueFilter.java

public void readFields(final DataInput in) throws IOException {
    this.columnFamily = Bytes.readByteArray(in);
    if (this.columnFamily.length == 0) {
        this.columnFamily = null;
    }//from   w w w  .j a va 2  s  . co  m
    this.columnQualifier = Bytes.readByteArray(in);
    if (this.columnQualifier.length == 0) {
        this.columnQualifier = null;
    }
    this.compareOp = CompareFilter.CompareOp.valueOf(in.readUTF());
    this.comparator = (WritableByteArrayComparable) HbaseObjectWritable.readObject(in, null);
    this.foundColumn = in.readBoolean();
    this.matchedColumn = in.readBoolean();
    this.filterIfMissing = in.readBoolean();
    this.latestVersionOnly = in.readBoolean();
}

From source file:org.mitre.la.mapred.io.DenseVectorWritable.java

/**
 * Deserialize the fields of this object from <code>in</code>.
 *
 * @param in <code>DataInput</code> to deseriablize this object from.
 * @throws IOException//from   w ww.  j a va 2  s .  c o  m
 */
@Override
public void readFields(DataInput in) throws IOException {
    in.readByte();
    String label = in.readUTF();
    int length = in.readInt();
    double[] v = new double[length];
    for (int i = 0; i < length; i++) {
        v[i] = in.readDouble();
    }
    this.dv = new DenseVector(label, v);
}

From source file:org.netbeans.nbbuild.VerifyClassLinkageForIISI.java

public static Set<String> dependencies(byte[] data) throws IOException {
    Set<String> result = new TreeSet<String>();
    DataInput input = new DataInputStream(new ByteArrayInputStream(data));
    skip(input, 8); // magic, minor_version, major_version
    int size = input.readUnsignedShort() - 1; // constantPoolCount
    String[] utf8Strings = new String[size];
    boolean[] isClassName = new boolean[size];
    boolean[] isDescriptor = new boolean[size];
    for (int i = 0; i < size; i++) {
        byte tag = input.readByte();
        switch (tag) {
        case 1: // CONSTANT_Utf8
            utf8Strings[i] = input.readUTF();
            break;
        case 7: // CONSTANT_Class
            int index = input.readUnsignedShort() - 1;
            if (index >= size) {
                throw new IOException("@" + i + ": CONSTANT_Class_info.name_index " + index
                        + " too big for size of pool " + size);
            }//from  w  w w .j a v  a  2  s.  c om
            //log("Class reference at " + index, Project.MSG_DEBUG);
            isClassName[index] = true;
            break;
        case 3: // CONSTANT_Integer
        case 4: // CONSTANT_Float
        case 9: // CONSTANT_Fieldref
        case 10: // CONSTANT_Methodref
        case 11: // CONSTANT_InterfaceMethodref
            skip(input, 4);
            break;
        case 12: // CONSTANT_NameAndType
            skip(input, 2);
            index = input.readUnsignedShort() - 1;
            if (index >= size || index < 0) {
                throw new IOException("@" + i + ": CONSTANT_NameAndType_info.descriptor_index " + index
                        + " too big for size of pool " + size);
            }
            isDescriptor[index] = true;
            break;
        case 8: // CONSTANT_String
            skip(input, 2);
            break;
        case 5: // CONSTANT_Long
        case 6: // CONSTANT_Double
            skip(input, 8);
            i++; // weirdness in spec
            break;
        default:
            throw new IOException("Unrecognized constant pool tag " + tag + " at index " + i
                    + "; running UTF-8 strings: " + Arrays.asList(utf8Strings));
        }
    }
    //task.log("UTF-8 strings: " + Arrays.asList(utf8Strings), Project.MSG_DEBUG);
    for (int i = 0; i < size; i++) {
        String s = utf8Strings[i];
        final Set<String> classNameSet = extractClassName(s);

        if (s != null) {
            if (CollectionUtils.isNotEmpty(classNameSet)) {
                result.addAll(classNameSet);
            }

        }

    }
    return result;
}

From source file:org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepositoryTest.java

@Test
public void sessionFromData() throws Exception {
    final long expectedCreationTime = 1L;
    final long expectedLastAccessedTime = 2L;

    final int expectedMaxInactiveIntervalInSeconds = (int) TimeUnit.HOURS.toSeconds(6);

    final String expectedPrincipalName = "jblum";

    DataInput mockDataInput = mock(DataInput.class);

    given(mockDataInput.readUTF()).willReturn("2").willReturn(expectedPrincipalName);
    given(mockDataInput.readLong()).willReturn(expectedCreationTime).willReturn(expectedLastAccessedTime);
    given(mockDataInput.readInt()).willReturn(expectedMaxInactiveIntervalInSeconds);

    @SuppressWarnings("serial")
    AbstractGemFireOperationsSessionRepository.GemFireSession session = new AbstractGemFireOperationsSessionRepository.GemFireSession(
            "1") {
        @Override//w w  w .  j a  v a2 s  .  c  o  m
        @SuppressWarnings("unchecked")
        <T> T readObject(DataInput in) throws ClassNotFoundException, IOException {
            assertThat(in).isNotNull();

            AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes sessionAttributes = new AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes();

            sessionAttributes.setAttribute("attrOne", "testOne");
            sessionAttributes.setAttribute("attrTwo", "testTwo");

            return (T) sessionAttributes;
        }
    };

    session.fromData(mockDataInput);

    Set<String> expectedAttributeNames = asSet("attrOne", "attrTwo",
            FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME);

    assertThat(session.getId()).isEqualTo("2");
    assertThat(session.getCreationTime()).isEqualTo(expectedCreationTime);
    assertThat(session.getLastAccessedTime()).isEqualTo(expectedLastAccessedTime);
    assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo(expectedMaxInactiveIntervalInSeconds);
    assertThat(session.getPrincipalName()).isEqualTo(expectedPrincipalName);
    assertThat(session.getAttributeNames().size()).isEqualTo(3);
    assertThat(session.getAttributeNames().containsAll(expectedAttributeNames)).isTrue();
    assertThat(String.valueOf(session.getAttribute("attrOne"))).isEqualTo("testOne");
    assertThat(String.valueOf(session.getAttribute("attrTwo"))).isEqualTo("testTwo");
    assertThat(String.valueOf(session.getAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME)))
            .isEqualTo(expectedPrincipalName);

    verify(mockDataInput, times(2)).readUTF();
    verify(mockDataInput, times(2)).readLong();
    verify(mockDataInput, times(2)).readInt();
}

From source file:org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepositoryTest.java

@Test
public void sessionAttributesFromData() throws Exception {
    final DataInput mockDataInput = mock(DataInput.class);

    given(mockDataInput.readInt()).willReturn(2);
    given(mockDataInput.readUTF()).willReturn("attrOne").willReturn("attrTwo");

    @SuppressWarnings("serial")
    AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes sessionAttributes = new AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes() {
        private int count = 0;

        @Override/*w ww  . ja  va 2 s.  c o  m*/
        @SuppressWarnings("unchecked")
        <T> T readObject(DataInput in) throws ClassNotFoundException, IOException {
            assertThat(in).isSameAs(mockDataInput);
            return (T) Arrays.asList("testOne", "testTwo").get(count++);
        }
    };

    assertThat(sessionAttributes.getAttributeNames().isEmpty()).isTrue();

    sessionAttributes.fromData(mockDataInput);

    assertThat(sessionAttributes.getAttributeNames().size()).isEqualTo(2);
    assertThat(sessionAttributes.getAttributeNames().containsAll(asSet("attrOne", "attrTwo"))).isTrue();
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrOne"))).isEqualTo("testOne");
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrTwo"))).isEqualTo("testTwo");

    verify(mockDataInput, times(1)).readInt();
    verify(mockDataInput, times(2)).readUTF();
}

From source file:org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepositoryTest.java

@Test
public void sessionAttributesFromDelta() throws Exception {
    final DataInput mockDataInput = mock(DataInput.class);

    given(mockDataInput.readInt()).willReturn(2);
    given(mockDataInput.readUTF()).willReturn("attrOne").willReturn("attrTwo");

    @SuppressWarnings("serial")
    AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes sessionAttributes = new AbstractGemFireOperationsSessionRepository.GemFireSessionAttributes() {
        private int count = 0;

        @Override/*from w w  w  .  j  a v a 2 s.  co m*/
        @SuppressWarnings("unchecked")
        <T> T readObject(DataInput in) throws ClassNotFoundException, IOException {
            assertThat(in).isSameAs(mockDataInput);
            return (T) Arrays.asList("testOne", "testTwo", "testThree").get(count++);
        }
    };

    sessionAttributes.setAttribute("attrOne", "one");
    sessionAttributes.setAttribute("attrTwo", "two");

    assertThat(sessionAttributes.getAttributeNames().size()).isEqualTo(2);
    assertThat(sessionAttributes.getAttributeNames().containsAll(asSet("attrOne", "attrTwo"))).isTrue();
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrOne"))).isEqualTo("one");
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrTwo"))).isEqualTo("two");
    assertThat(sessionAttributes.hasDelta()).isTrue();

    sessionAttributes.fromDelta(mockDataInput);

    assertThat(sessionAttributes.getAttributeNames().size()).isEqualTo(2);
    assertThat(sessionAttributes.getAttributeNames().containsAll(asSet("attrOne", "attrTwo"))).isTrue();
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrOne"))).isEqualTo("testOne");
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrTwo"))).isEqualTo("testTwo");
    assertThat(sessionAttributes.hasDelta()).isFalse();

    verify(mockDataInput, times(1)).readInt();
    verify(mockDataInput, times(2)).readUTF();
    reset(mockDataInput);

    given(mockDataInput.readInt()).willReturn(1);
    given(mockDataInput.readUTF()).willReturn("attrTwo");

    sessionAttributes.setAttribute("attrOne", "one");
    sessionAttributes.setAttribute("attrTwo", "two");

    assertThat(sessionAttributes.getAttributeNames().size()).isEqualTo(2);
    assertThat(sessionAttributes.getAttributeNames().containsAll(asSet("attrOne", "attrTwo"))).isTrue();
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrOne"))).isEqualTo("one");
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrTwo"))).isEqualTo("two");
    assertThat(sessionAttributes.hasDelta()).isTrue();

    sessionAttributes.fromDelta(mockDataInput);

    assertThat(sessionAttributes.getAttributeNames().size()).isEqualTo(2);
    assertThat(sessionAttributes.getAttributeNames().containsAll(asSet("attrOne", "attrTwo"))).isTrue();
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrOne"))).isEqualTo("one");
    assertThat(String.valueOf(sessionAttributes.getAttribute("attrTwo"))).isEqualTo("testThree");
    assertThat(sessionAttributes.hasDelta()).isTrue();

    verify(mockDataInput, times(1)).readInt();
    verify(mockDataInput, times(1)).readUTF();
}