Android Open Source - tape Serialized Converter






From Project

Back to project page tape.

License

The source code is released under:

Apache License

If you think the Android project tape listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

// Copyright 2012 Square, Inc.
package com.squareup.tape;
//from ww w. j  av a  2  s.  co m
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

/**
 * Serialized object queue converter.
 *
 * <b>This class is not thread safe; instances should be kept thread-confined.</b>
 *
 * @param <T> Object type.
 */
public class SerializedConverter<T extends Serializable> implements FileObjectQueue.Converter<T> {
  /** Deserialize a stream to an object. */
  @SuppressWarnings("unchecked")
  private T deserialize(InputStream in) throws IOException {
    ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in, 1024));
    T entry;
    try {
      entry = (T) oin.readUnshared();
    } catch (ClassNotFoundException e) {
      // This can only happen if we make an incompatible change.
      throw new AssertionError(e);
    }
    return entry;
  }

  /** Deserialize bytes to an object. */
  @Override public T from(byte[] bytes) throws IOException {
    return deserialize(new ByteArrayInputStream(bytes));
  }

  /** Serializes o to bytes. */
   @Override public void toStream(T o, OutputStream bytes) throws IOException {
    ObjectOutputStream out = new ObjectOutputStream(bytes);
    out.writeUnshared(o);
    out.close();
  }
}




Java Source Code List

com.squareup.tape.FileException.java
com.squareup.tape.FileObjectQueue.java
com.squareup.tape.InMemoryObjectQueue.java
com.squareup.tape.ObjectQueue.java
com.squareup.tape.QueueFile.java
com.squareup.tape.SerializedConverter.java
com.squareup.tape.TaskInjector.java
com.squareup.tape.TaskQueue.java
com.squareup.tape.Task.java
com.squareup.tape.sample.GsonConverter.java
com.squareup.tape.sample.ImageUploadQueueSizeEvent.java
com.squareup.tape.sample.ImageUploadSuccessEvent.java
com.squareup.tape.sample.ImageUploadTaskQueue.java
com.squareup.tape.sample.ImageUploadTaskService.java
com.squareup.tape.sample.ImageUploadTask.java
com.squareup.tape.sample.SampleActivity.java
com.squareup.tape.sample.SampleApplication.java