예제 #1
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);
    }
  }
예제 #2
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);
  }
예제 #3
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;
 }
예제 #4
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);
   }
 }
예제 #5
0
 // Required by Kryo serialization.
 public void write(Kryo kryo, Output out) {
   kryo.writeObject(out, intVal);
   kryo.writeObject(out, floatVal);
   kryo.writeObject(out, shortVal);
   kryo.writeObject(out, byteArr);
   kryo.writeObject(out, longArr);
   kryo.writeObject(out, dblArr);
   kryo.writeObject(out, str);
   out.flush();
 }
예제 #6
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();
 }
예제 #7
0
  public void testSmallBuffers() throws Exception {
    ByteBuffer buf = ByteBuffer.allocate(1024);
    ByteBufferOutputStream byteBufferOutputStream = new ByteBufferOutputStream(buf);
    Output testOutput = new Output(byteBufferOutputStream);
    testOutput.writeBytes(new byte[512]);
    testOutput.writeBytes(new byte[512]);
    testOutput.flush();

    ByteBufferInputStream testInputs = new ByteBufferInputStream();
    buf.flip();
    testInputs.setByteBuffer(buf);
    Input input = new Input(testInputs, 512);
    byte[] toRead = new byte[512];
    input.readBytes(toRead);

    input.readBytes(toRead);
  }
예제 #8
0
  public void testOutputStream() throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    Output output = new Output(buffer, 2);
    output.writeBytes(new byte[] {11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26});
    output.writeBytes(new byte[] {31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46});
    output.writeBytes(new byte[] {51, 52, 53, 54, 55, 56, 57, 58});
    output.writeBytes(new byte[] {61, 62, 63, 64, 65});
    output.flush();

    assertEquals(
        new byte[] { //
          11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, //
          31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, //
          51, 52, 53, 54, 55, 56, 57, 58, //
          61, 62, 63, 64, 65
        },
        buffer.toByteArray());
  }
  /** {@inheritDoc} */
  @SuppressWarnings("unchecked")
  public void write(Connection connection, OutputStream outputStream, Object object) {
    Output output = new Output(outputStream);
    IKryoProvider kryoProvider = serializerQueue.poll();

    // if nothing is available in queue don't wait, create new one
    if (null == kryoProvider) {
      kryoProvider = createKryoProvider();
    }

    try {
      Kryo kryo = kryoProvider.getKryo();
      kryo.getContext().put("connection", connection);
      kryo.writeClassAndObject(output, object);
      output.flush();
    } finally {
      serializerQueue.offer(kryoProvider);
    }
  }
  @Test
  @Parameters(method = "parameters")
  public void inputAndOutputAreSame(Gender gender, Range range) {
    Seeking inputElement = new Seeking(gender, range);
    Kryo kryo = new Kryo();
    kryo.register(Range.class, new RangeSerializer());

    SeekingSerializer serializer = new SeekingSerializer();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Output output = new Output(outputStream);
    serializer.write(kryo, output, inputElement);
    output.flush();

    Input input = new Input(new ByteArrayInputStream(outputStream.toByteArray()));
    Seeking outputElement = serializer.read(kryo, input, Seeking.class);

    Assert.assertEquals(inputElement.getAgeRange(), outputElement.getAgeRange());
    Assert.assertEquals(inputElement.getGender(), outputElement.getGender());
  }
예제 #11
0
  public void testWriteBytes() throws IOException {
    Output buffer = new Output(512);
    buffer.writeBytes(new byte[] {11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26});
    buffer.writeBytes(new byte[] {31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46});
    buffer.writeByte(51);
    buffer.writeBytes(new byte[] {52, 53, 54, 55, 56, 57, 58});
    buffer.writeByte(61);
    buffer.writeByte(62);
    buffer.writeByte(63);
    buffer.writeByte(64);
    buffer.writeByte(65);
    buffer.flush();

    assertEquals(
        new byte[] { //
          11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, //
          31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, //
          51, 52, 53, 54, 55, 56, 57, 58, //
          61, 62, 63, 64, 65
        },
        buffer.toBytes());
  }
 public void flush() {
   output.flush();
 }