Package net.alteiar.newversion.shared.chunk

Source Code of net.alteiar.newversion.shared.chunk.ChunkSendFactory

package net.alteiar.newversion.shared.chunk;

import java.io.ByteArrayOutputStream;

import net.alteiar.shared.IUniqueObject;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Output;

public class ChunkSendFactory {

  private final Kryo kryo;
  private final int chunkSize;

  public ChunkSendFactory(Kryo kryo) {
    this(kryo, 524288);
  }

  public ChunkSendFactory(Kryo kryo, int chunkSize) {
    this.kryo = kryo;
    this.chunkSize = chunkSize;
  }

  public int getChunkSize() {
    return chunkSize;
  }

  public synchronized ChunkObjectSend generateMessages(IUniqueObject bean) {
    ChunkObjectSend msg = null;
    try {
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      Output output = new Output(stream);
      kryo.writeObjectOrNull(output, bean, bean.getClass());
      output.close(); // Also calls output.flush()

      // Serialization done, get bytes
      byte[] buffer = stream.toByteArray();
      msg = new ChunkObjectSend(bean.getClass().getName(), bean.getId(),
          buffer, chunkSize);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(-1);
    }

    return msg;
  }
}
TOP

Related Classes of net.alteiar.newversion.shared.chunk.ChunkSendFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.