Esempio n. 1
0
 @Override
 public void write(Kryo kryo, Output out) {
   byte[] bytes = getBytes();
   out.writeInt(bytes.length);
   out.writeInt(this.numFields);
   out.write(bytes);
 }
Esempio n. 2
0
 @Override
 public void write(Kryo kryo, Output output) {
   output.writeInt(player.getId());
   output.writeInt(target.getId());
   output.writeLong(time);
   kryo.writeObject(output, meanOfDeath);
 }
Esempio n. 3
0
    @Override
    public void write(Kryo kryo, Output output) {
      output.writeInt(objectID, true);

      int methodClassID = kryo.getRegistration(method.getDeclaringClass()).getId();
      output.writeInt(methodClassID, true);

      CachedMethod[] cachedMethods = getMethods(kryo, method.getDeclaringClass());
      CachedMethod cachedMethod = null;
      for (int i = 0, n = cachedMethods.length; i < n; i++) {
        cachedMethod = cachedMethods[i];
        if (cachedMethod.method.equals(method)) {
          output.writeByte(i);
          break;
        }
      }

      for (int i = 0, n = cachedMethod.serializers.length; i < n; i++) {
        Serializer serializer = cachedMethod.serializers[i];
        if (serializer != null) {
          kryo.writeObjectOrNull(output, args[i], serializer);
        } else {
          kryo.writeClassAndObject(output, args[i]);
        }
      }

      output.writeByte(responseID);
    }
Esempio n. 4
0
  private void runShortTest(Output write) throws IOException {
    write.writeShort(0);
    write.writeShort(63);
    write.writeShort(64);
    write.writeShort(127);
    write.writeShort(128);
    write.writeShort(8192);
    write.writeShort(16384);
    write.writeShort(32767);
    write.writeShort(-63);
    write.writeShort(-64);
    write.writeShort(-127);
    write.writeShort(-128);
    write.writeShort(-8192);
    write.writeShort(-16384);
    write.writeShort(-32768);

    Input read = new Input(write.toBytes());
    assertEquals(0, read.readShort());
    assertEquals(63, read.readShort());
    assertEquals(64, read.readShort());
    assertEquals(127, read.readShort());
    assertEquals(128, read.readShort());
    assertEquals(8192, read.readShort());
    assertEquals(16384, read.readShort());
    assertEquals(32767, read.readShort());
    assertEquals(-63, read.readShort());
    assertEquals(-64, read.readShort());
    assertEquals(-127, read.readShort());
    assertEquals(-128, read.readShort());
    assertEquals(-8192, read.readShort());
    assertEquals(-16384, read.readShort());
    assertEquals(-32768, read.readShort());
  }
Esempio n. 5
0
    private void writeIndex() {
      Output indexOutput;
      try {
        indexOutput = new Output(new FileOutputStream(getIndexFile()));
      } catch (FileNotFoundException e) {
        throw new UncheckedIOException(e);
      }

      try {
        indexOutput.writeInt(index.size(), true);

        for (Map.Entry<Long, Map<Long, TestCaseRegion>> classEntry : index.entrySet()) {
          Long classId = classEntry.getKey();
          Map<Long, TestCaseRegion> regions = classEntry.getValue();

          indexOutput.writeLong(classId, true);
          indexOutput.writeInt(regions.size(), true);

          for (Map.Entry<Long, TestCaseRegion> testCaseEntry : regions.entrySet()) {
            long id = testCaseEntry.getKey();
            TestCaseRegion region = testCaseEntry.getValue();
            indexOutput.writeLong(id, true);
            indexOutput.writeLong(region.stdOutRegion.start);
            indexOutput.writeLong(region.stdOutRegion.stop);
            indexOutput.writeLong(region.stdErrRegion.start);
            indexOutput.writeLong(region.stdErrRegion.stop);
          }
        }
      } finally {
        indexOutput.close();
      }
    }
Esempio n. 6
0
  /**
   * Attempt to write the file, delete the backup and return true as atomically as possible. If any
   * exception occurs, delete the new file; next time we will restore from the backup.
   *
   * @param key table key
   * @param objectEntity table instance
   * @param originalFile file to write new data
   * @param backupFile backup file to be used if write is failed
   */
  private <E> void writeTableFile(
      String key, ObjectEntity<E> objectEntity, File originalFile, File backupFile) {
    try {
      FileOutputStream fileStream = new FileOutputStream(originalFile);

      final Output kryoOutput = new Output(fileStream);
      getKryo().writeObject(kryoOutput, objectEntity);
      kryoOutput.flush();
      fileStream.flush();
      sync(fileStream);
      kryoOutput.close(); // also close file stream

      // Writing was successful, delete the backup file if there is one.
      //noinspection ResultOfMethodCallIgnored
      backupFile.delete();
    } catch (IOException | KryoException e) {
      // Clean up an unsuccessfully written file
      if (originalFile.exists()) {
        if (!originalFile.delete()) {
          throw new PaperDbException("Couldn't clean up partially-written file " + originalFile, e);
        }
      }
      throw new PaperDbException(
          "Couldn't save table: "
              + key
              + ". "
              + "Backed up table will be used on next read attempt",
          e);
    }
  }
Esempio n. 7
0
 public void write(Kryo kryo, Output output, Enum object) {
   if (object == null) {
     output.writeVarInt(NULL, true);
     return;
   }
   output.writeVarInt(object.ordinal() + 1, true);
 }
Esempio n. 8
0
  public void testFlushRoundTrip() throws Exception {

    Kryo kryo = new Kryo();

    String s1 = "12345";

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ObjectOutputStream objOutput = new ObjectOutputStream(os);
    Output output = new Output(objOutput);

    kryo.writeClass(output, s1.getClass());
    kryo.writeObject(output, s1);
    output.flush();
    //		objOutput.flush();  // this layer wasn't flushed prior to this bugfix, add it for a
    // workaround

    byte[] b = os.toByteArray();
    System.out.println("size: " + b.length);

    ByteArrayInputStream in = new ByteArrayInputStream(b);
    ObjectInputStream objIn = new ObjectInputStream(in);
    Input input = new Input(objIn);

    Registration r = kryo.readClass(input);
    String s2 = kryo.readObject(input, r.getType());

    assertEquals(s1, s2);
  }
 @Override
 protected void writeObject(Kryo kryo, Output output, VideoChunk video) throws Exception {
   output.writeLong(video.getDuration());
   output.writeInt(video.getVideo().length);
   output.writeBytes(video.getVideo());
   output.writeString(video.getContainer());
 }
Esempio n. 10
0
 @Override
 public byte[] encode(Object obj) {
   Output output = null;
   output = new Output(1, 4096);
   kryo.writeObject(output, obj);
   byte[] data = output.toBytes();
   output.flush();
   return data;
 }
Esempio n. 11
0
 @Override
 public byte[] toBytes(Event event) {
   Kryo kryo = pool.borrow();
   try (Output out = new Output(new java.io.ByteArrayOutputStream())) {
     kryo.writeClassAndObject(out, event);
     return out.toBytes();
   } finally {
     pool.release(kryo);
   }
 }
 @Override
 public void write(Kryo kryo, Output output) {
   output.writeString(this.firstname);
   output.writeString(this.lastname);
   kryo.writeObjectOrNull(output, this.emailAddress, SerializableEmailAddress.class);
   output.writeInt(this.addresses.size());
   for (Address address : this.addresses) {
     kryo.writeObject(output, address);
   }
 }
Esempio n. 13
0
 @Override
 public CachedData encode(final Object obj) {
   final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
   try (final Output output = new Output(byteStream)) {
     kryo.writeClassAndObject(output, obj);
     output.flush();
     final byte[] bytes = byteStream.toByteArray();
     return new CachedData(0, bytes, bytes.length);
   }
 }
Esempio n. 14
0
  public void runStringTest(int length) throws IOException {
    Output write = new Output(1024, -1);
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < length; i++) buffer.append((char) i);

    String value = buffer.toString();
    write.writeString(value);
    write.writeString(value);
    Input read = new Input(write.toBytes());
    assertEquals(value, read.readString());
    assertEquals(value, read.readStringBuilder().toString());

    write.clear();
    write.writeString(buffer);
    write.writeString(buffer);
    read = new Input(write.toBytes());
    assertEquals(value, read.readStringBuilder().toString());
    assertEquals(value, read.readString());

    if (length <= 127) {
      write.clear();
      write.writeAscii(value);
      write.writeAscii(value);
      read = new Input(write.toBytes());
      assertEquals(value, read.readStringBuilder().toString());
      assertEquals(value, read.readString());
    }
  }
 private static byte[] serializeObjectToKryo(Serializable object) {
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   Output output = new Output(baos);
   Kryo kryo = borrowKryo();
   try {
     kryo.writeObject(output, object);
   } finally {
     releaseKryo(kryo);
   }
   output.close();
   return baos.toByteArray();
 }
Esempio n. 16
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());
    }
  }
Esempio n. 17
0
  public static byte[] serialize(Object object) throws SerializeException {
    if (object == null) {
      throw new SerializeException("指向object对象指针为空");
    }

    Kryo kryo = kryoThreadLocal.get();
    ByteArrayOutputStream stream = new ByteArrayOutputStream(20000);
    Output output = new Output(stream);
    kryo.writeObject(output, object);
    output.close();

    return stream.toByteArray();
  }
Esempio n. 18
0
  public void testCanReadInt() throws IOException {
    Output write = new Output(new ByteArrayOutputStream());

    Input read = new Input(write.toBytes());
    assertEquals(false, read.canReadInt());

    write.writeInt(400, true);

    read = new Input(write.toBytes());
    assertEquals(true, read.canReadInt());
    read.setLimit(read.limit() - 1);
    assertEquals(false, read.canReadInt());
  }
Esempio n. 19
0
 @Override
 public void write(Kryo kryo, Output output) {
   super.write(kryo, output);
   if (_has(VALUES)) {
     writeCollection(kryo, output, getValues(), true);
   }
   if (_has(NAMESPACE)) {
     output.writeString(getNameSpace());
   }
   if (_has(IDENTITY)) {
     output.writeString(getIdentity());
   }
 }
 @Test
 public void testSerDeserPerf2() throws Exception {
   Kryo kryo = new Kryo();
   String outputPath = FilenameUtils.concat(getTmpPath(), "file2.bin");
   Output output = new Output(new FileOutputStream(outputPath));
   for (int i = 0; i < 1000; i++) {
     kryo.writeObject(output, constructNewPE());
   }
   output.close();
   Input input = new Input(new FileInputStream(outputPath));
   NewPartitionedEvent someObject = kryo.readObject(input, NewPartitionedEvent.class);
   input.close();
   Assert.assertTrue(someObject.getData().length == 1);
 }
Esempio n. 21
0
 public void write(Kryo kryo, Output output, Object object) {
   Deflater deflater = new Deflater(compressionLevel, noHeaders);
   OutputChunked outputChunked = new OutputChunked(output, 256);
   DeflaterOutputStream deflaterStream = new DeflaterOutputStream(outputChunked, deflater);
   Output deflaterOutput = new Output(deflaterStream, 256);
   kryo.writeObject(deflaterOutput, object, serializer);
   deflaterOutput.flush();
   try {
     deflaterStream.finish();
   } catch (IOException ex) {
     throw new KryoException(ex);
   }
   outputChunked.endChunks();
 }
Esempio n. 22
0
  public byte[] serializeBatch(BatchTuple batch) {
    if (batch == null || batch.currBatchSize() == 0) return null;

    _kryoOut.clear();
    for (Tuple tuple : batch.getTuples()) {
      /*
       * byte structure:
       * 1st tuple: length + tuple bytes
       * 2nd tuple: length + tuple bytes
       * ......
       */
      int startPos = _kryoOut.position();

      // Set initial value of tuple length, which will be updated accordingly after serialization
      _kryoOut.writeInt(0);

      serializeTuple(_kryoOut, tuple);

      // Update the tuple length
      int endPos = _kryoOut.position();
      _kryoOut.setPosition(startPos);
      _kryoOut.writeInt(endPos - startPos - 4);
      _kryoOut.setPosition(endPos);
    }
    return _kryoOut.toBytes();
  }
Esempio n. 23
0
 public void savePage(
     OutputStream out, String title, UrlPageModel pageModel, SerializablePage page) {
   synchronized (serializeLock) {
     Output output = null;
     try {
       output = isHoneycomb() ? new KryoOutputHC(out) : new Output(out);
       output.writeString(title);
       kryo.writeObject(output, pageModel);
       kryo.writeObject(output, page);
     } finally {
       IOUtils.closeQuietly(output);
     }
   }
 }
Esempio n. 24
0
 private void writeTicksAndEvents(Output output) throws KryoException {
   int spritesNum = 100;
   String currEdge = "1107293662 1107288392";
   String nextEdge = "";
   Random rand = new Random(System.currentTimeMillis());
   for (int edges = 0; edges < 1000; edges++) {
     Set<String> outgoing = graphData.getEdgesOf(currEdge.split(" ")[1]);
     Object[] setArray = outgoing.toArray();
     nextEdge = (String) setArray[rand.nextInt(outgoing.size())];
     for (int i = 0; i < 10; i = i + 5) {
       eventWriter.writeTick(output, i);
       int percentage = (int) (((double) i / 10) * 100);
       int nextPer = (int) (((double) (i + 5) / 10) * 100);
       for (int j = 0; j < spritesNum; j++) {
         if (Math.random() > 0) {
           //                        System.out.println(currEdge);
           String[] split = currEdge.split(" ");
           eventWriter.writeEvent(
               output, new MoveEvent(j, split[0], split[1], percentage, nextPer));
         }
       }
     }
     currEdge = nextEdge;
   }
   output.close();
 }
Esempio n. 25
0
 public void write(Kryo kryo, Output output, BigDecimal object) {
   if (object == null) {
     output.writeVarInt(NULL, true);
     return;
   }
   BigDecimal value = (BigDecimal) object;
   // fast-path optimizations for BigDecimal constants
   if (value == BigDecimal.ZERO) {
     bigIntegerSerializer.write(kryo, output, BigInteger.ZERO);
     output.writeInt(0, false); // for backwards compatibility
     return;
   }
   // default behaviour
   bigIntegerSerializer.write(kryo, output, value.unscaledValue());
   output.writeInt(value.scale(), false);
 }
Esempio n. 26
0
    @Override
    public void write(Kryo kryo, Output output, VectorSet vectorSet) {
      output.writeString(vectorSet.key());
      kryo.writeObject(output, vectorSet.accumuFactor);
      kryo.writeObject(output, vectorSet.sparseFactor);

      TLongIntMap indexer = vectorSet.indexer;
      TLongIntIterator iter = indexer.iterator();
      kryo.writeObject(output, indexer.size());
      while (iter.hasNext()) {
        iter.advance();
        long vecid = iter.key();
        int start = iter.value();
        int length = vectorSet.length(vecid);
        kryo.writeObject(output, vecid);
        kryo.writeObject(output, start);
        kryo.writeObject(output, length);
      }

      int size = vectorSet.data.size();
      kryo.writeObject(output, size);
      for (int offset = 0; offset < size; offset++) {
        float val = vectorSet.data.get(offset);
        kryo.writeObject(output, val);
      }
    }
  private DatagramPacket[] messageToPackets(
      final InetSocketAddress remoteSocketAddress, final RPCMessage rpcMessage) {

    final MultiPacketOutputStream mpos =
        new MultiPacketOutputStream(RPCMessage.MAXIMUM_MSG_SIZE + RPCMessage.METADATA_SIZE);
    final Kryo kryo = KryoUtil.getKryo();
    kryo.reset();

    final Output output = new Output(mpos);

    kryo.writeObject(output, new RPCEnvelope(rpcMessage));
    output.close();
    mpos.close();

    return mpos.createPackets(remoteSocketAddress);
  }
Esempio n. 28
0
  /**
   * @@@ in the furture, it will skill serialize 'targetTask' through check some flag
   *
   * @see ITupleSerializer#serialize(int, Tuple)
   */
  private void serializeTuple(Output output, Tuple tuple) {
    try {
      if (tuple instanceof TupleExt) {
        output.writeInt(((TupleExt) tuple).getTargetTaskId());
        output.writeLong(((TupleExt) tuple).getCreationTimeStamp());
      }

      output.writeInt(tuple.getSourceTask(), true);
      output.writeInt(
          _ids.getStreamId(tuple.getSourceComponent(), tuple.getSourceStreamId()), true);
      tuple.getMessageId().serialize(output);
      _kryo.serializeInto(tuple.getValues(), output);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  /**
   * @param sopremoRecord
   * @return
   */
  private SopremoRecord serializeAndDeserialize(SopremoRecord sopremoRecord) throws IOException {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Output output = new Output(baos);
    kryo.writeObject(output, sopremoRecord);
    output.close();
    baos.close();

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    Input input = new Input(bais);
    SopremoRecord deserialized = kryo.readObject(input, SopremoRecord.class);
    deserialized.setLayout(sopremoRecord.getLayout());
    input.close();

    return deserialized;
  }
Esempio n. 30
0
    public void onOutput(long classId, long testId, TestOutputEvent outputEvent) {
      boolean stdout = outputEvent.getDestination() == TestOutputEvent.Destination.StdOut;
      mark(classId, testId, stdout);

      output.writeBoolean(stdout);
      output.writeLong(classId);
      output.writeLong(testId);

      byte[] bytes;
      try {
        bytes = outputEvent.getMessage().getBytes(messageStorageCharset.name());
      } catch (UnsupportedEncodingException e) {
        throw UncheckedException.throwAsUncheckedException(e);
      }
      output.writeInt(bytes.length);
      output.writeBytes(bytes);
    }