예제 #1
0
  private void runBooleanTest(Output write) throws IOException {
    for (int i = 0; i < 100; i++) {
      write.writeBoolean(true);
      write.writeBoolean(false);
    }

    Input read = new Input(write.toBytes());
    for (int i = 0; i < 100; i++) {
      assertEquals(true, read.readBoolean());
      assertEquals(false, read.readBoolean());
    }
  }
예제 #2
0
 public boolean readBoolean() throws EOFException {
   try {
     return input.readBoolean();
   } catch (KryoException e) {
     throw maybeEndOfStream(e);
   }
 }
예제 #3
0
  private Vertex readVertex(
      final Direction directionRequested,
      final Function<DetachedVertex, Vertex> vertexMaker,
      final Function<DetachedEdge, Edge> edgeMaker,
      final Input input)
      throws IOException {
    if (null != directionRequested && null == edgeMaker)
      throw new IllegalArgumentException(
          "If a directionRequested is specified then an edgeAdder function should also be specified");

    this.headerReader.read(kryo, input);

    final DetachedVertex detachedVertex = (DetachedVertex) kryo.readClassAndObject(input);
    final Vertex v = vertexMaker.apply(detachedVertex);

    final boolean streamContainsEdgesInSomeDirection = input.readBoolean();
    if (!streamContainsEdgesInSomeDirection && directionRequested != null)
      throw new IllegalStateException(
          String.format(
              "The direction %s was requested but no attempt was made to serialize edges into this stream",
              directionRequested));

    // if there are edges in the stream and the direction is not present then the rest of the stream
    // is
    // simply ignored
    if (directionRequested != null) {
      final Direction directionsInStream = kryo.readObject(input, Direction.class);
      if (directionsInStream != Direction.BOTH && directionsInStream != directionRequested)
        throw new IllegalStateException(
            String.format(
                "Stream contains %s edges, but requesting %s",
                directionsInStream, directionRequested));

      final Direction firstDirection = kryo.readObject(input, Direction.class);
      if (firstDirection == Direction.OUT
          && (directionRequested == Direction.BOTH || directionRequested == Direction.OUT))
        readEdges(input, edgeMaker);
      else {
        // requested direction in, but BOTH must be serialized so skip this.  the
        // illegalstateexception
        // prior to this IF should  have caught a problem where IN is not supported at all
        if (firstDirection == Direction.OUT && directionRequested == Direction.IN) skipEdges(input);
      }

      if (directionRequested == Direction.BOTH || directionRequested == Direction.IN) {
        // if the first direction was OUT then it was either read or skipped.  in that case, the
        // marker
        // of the stream is currently ready to read the IN direction. otherwise it's in the perfect
        // place
        // to start reading edges
        if (firstDirection == Direction.OUT) kryo.readObject(input, Direction.class);

        readEdges(input, edgeMaker);
      }
    }

    return v;
  }
예제 #4
0
 private void skipEdges(final Input input) {
   if (input.readBoolean()) {
     Object next = kryo.readClassAndObject(input);
     while (!next.equals(EdgeTerminator.INSTANCE)) {
       // next edge to skip or the terminator
       next = kryo.readClassAndObject(input);
     }
   }
 }
예제 #5
0
 private void readEdges(final Input input, final Function<DetachedEdge, Edge> edgeMaker) {
   if (input.readBoolean()) {
     Object next = kryo.readClassAndObject(input);
     while (!next.equals(EdgeTerminator.INSTANCE)) {
       final DetachedEdge detachedEdge = (DetachedEdge) next;
       edgeMaker.apply(detachedEdge);
       next = kryo.readClassAndObject(input);
     }
   }
 }
예제 #6
0
 public Calendar read(Kryo kryo, Input input, Class<Calendar> type) {
   Calendar result = Calendar.getInstance(timeZoneSerializer.read(kryo, input, TimeZone.class));
   result.setTimeInMillis(input.readLong(true));
   result.setLenient(input.readBoolean());
   result.setFirstDayOfWeek(input.readInt(true));
   result.setMinimalDaysInFirstWeek(input.readInt(true));
   long gregorianChange = input.readLong(false);
   if (gregorianChange != DEFAULT_GREGORIAN_CUTOVER)
     if (result instanceof GregorianCalendar)
       ((GregorianCalendar) result).setGregorianChange(new Date(gregorianChange));
   return result;
 }
예제 #7
0
    private void doRead(
        long classId,
        long testId,
        boolean allClassOutput,
        TestOutputEvent.Destination destination,
        java.io.Writer writer) {
      if (dataFile == null) {
        return;
      }

      Index targetIndex = index.children.get(classId);
      if (targetIndex != null && testId != 0) {
        targetIndex = targetIndex.children.get(testId);
      }

      if (targetIndex == null) {
        return;
      }

      boolean stdout = destination == TestOutputEvent.Destination.StdOut;
      Region region = stdout ? targetIndex.stdOut : targetIndex.stdErr;

      if (region.start < 0) {
        return;
      }

      boolean ignoreClassLevel = !allClassOutput && testId != 0;
      boolean ignoreTestLevel = !allClassOutput && testId == 0;

      try {
        dataFile.seek(region.start);

        while (dataFile.getFilePointer() <= region.stop) {
          dataFile.read(recordHeaderBuffer);
          Input input = new Input(recordHeaderBuffer);
          boolean readStdout = input.readBoolean();
          long readClassId = input.readLong();
          long readTestId = input.readLong();
          int readLength = input.readInt();
          input.close();

          boolean isClassLevel = readTestId == 0;

          if (stdout != readStdout || classId != readClassId) {
            dataFile.skipBytes(readLength);
            continue;
          }

          if (ignoreClassLevel && isClassLevel) {
            dataFile.skipBytes(readLength);
            continue;
          }

          if (ignoreTestLevel && !isClassLevel) {
            dataFile.skipBytes(readLength);
            continue;
          }

          if (testId == 0 || testId == readTestId) {
            byte[] stringBytes = new byte[readLength];
            dataFile.read(stringBytes);
            String message;
            try {
              message = new String(stringBytes, messageStorageCharset.name());
            } catch (UnsupportedEncodingException e) {
              // shouldn't happen
              throw UncheckedException.throwAsUncheckedException(e);
            }

            writer.write(message);
          } else {
            dataFile.skipBytes(readLength);
            continue;
          }
        }
      } catch (IOException e1) {
        throw new UncheckedIOException(e1);
      }
    }
예제 #8
0
  @Override
  public void readGraph(final InputStream inputStream, final Graph graphToWriteTo)
      throws IOException {
    this.counter.set(0);
    final Input input = new Input(inputStream);
    this.headerReader.read(kryo, input);

    final BatchGraph graph;
    try {
      // will throw an exception if not constructed properly
      graph =
          BatchGraph.build(graphToWriteTo)
              .vertexIdKey(vertexIdKey)
              .edgeIdKey(edgeIdKey)
              .bufferSize(batchSize)
              .create();
    } catch (Exception ex) {
      throw new IOException("Could not instantiate BatchGraph wrapper", ex);
    }

    try (final Output output = new Output(new FileOutputStream(tempFile))) {
      final boolean supportedMemory = input.readBoolean();
      if (supportedMemory) {
        // if the graph that serialized the data supported sideEffects then the sideEffects needs to
        // be read
        // to advance the reader forward.  if the graph being read into doesn't support the
        // sideEffects
        // then we just setting the data to sideEffects.
        final Map<String, Object> memMap =
            (Map<String, Object>) kryo.readObject(input, HashMap.class);
        if (graphToWriteTo.features().graph().variables().supportsVariables()) {
          final Graph.Variables variables = graphToWriteTo.variables();
          memMap.forEach(variables::set);
        }
      }

      final boolean hasSomeVertices = input.readBoolean();
      if (hasSomeVertices) {
        while (!input.eof()) {
          final List<Object> vertexArgs = new ArrayList<>();
          final DetachedVertex current = (DetachedVertex) kryo.readClassAndObject(input);
          appendToArgList(vertexArgs, T.id, current.id());
          appendToArgList(vertexArgs, T.label, current.label());

          final Vertex v = graph.addVertex(vertexArgs.toArray());
          current
              .iterators()
              .propertyIterator()
              .forEachRemaining(p -> createVertexProperty(graphToWriteTo, v, p, false));
          // current.iterators().hiddenPropertyIterator().forEachRemaining(p ->
          // createVertexProperty(graphToWriteTo, v, p, true));

          // the gio file should have been written with a direction specified
          final boolean hasDirectionSpecified = input.readBoolean();
          final Direction directionInStream = kryo.readObject(input, Direction.class);
          final Direction directionOfEdgeBatch = kryo.readObject(input, Direction.class);

          // graph serialization requires that a direction be specified in the stream and that the
          // direction of the edges be OUT
          if (!hasDirectionSpecified
              || directionInStream != Direction.OUT
              || directionOfEdgeBatch != Direction.OUT)
            throw new IllegalStateException(
                String.format(
                    "Stream must specify edge direction and that direction must be %s",
                    Direction.OUT));

          // if there are edges then read them to end and write to temp, otherwise read what should
          // be
          // the vertex terminator
          if (!input.readBoolean()) kryo.readClassAndObject(input);
          else readToEndOfEdgesAndWriteToTemp(input, output);
        }
      }
    } catch (Exception ex) {
      throw new IOException(ex);
    }
    // done writing to temp

    // start reading in the edges now from the temp file
    try (final Input edgeInput = new Input(new FileInputStream(tempFile))) {
      readFromTempEdges(edgeInput, graph);
      graph.tx().commit();
    } catch (Exception ex) {
      ex.printStackTrace();
      throw new IOException(ex);
    } finally {
      deleteTempFileSilently();
    }
  }
예제 #9
0
 public Boolean read(Kryo kryo, Input input, Class<Boolean> type) {
   return input.readBoolean();
 }