Package com.cetsoft.imcache.test

Source Code of com.cetsoft.imcache.test.OffHeapByteBufferTest

/*
* Copyright (C) 2014 Cetsoft, http://www.cetsoft.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author : Yusuf Aytas
* Date   : Sep 23, 2013
*/
package com.cetsoft.imcache.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import com.cetsoft.imcache.cache.offheap.bytebuffer.OffHeapByteBuffer;
import com.cetsoft.imcache.cache.offheap.bytebuffer.Pointer;

/**
* The Class OffHeapByteBufferTest.
*/
public class OffHeapByteBufferTest {

  /**
   * The main method.
   *
   * @param args the arguments
   */
  public static void main(String[] args) {
    int length = 5, x = 4, y = 3;
    OffHeapByteBuffer buffer = new OffHeapByteBuffer(0,1000);
    SimpleObject object = new SimpleObject(x, y);
    for (int i = 0; i < length; i++) {
      byte [] payload = serialize(object);
      Pointer pointer = buffer.store(payload);
      SimpleObject simpleObject = deserialize(buffer.retrieve(pointer));
      if(object.getX()!=simpleObject.getX()&&object.getY()!=simpleObject.getY()){
        System.err.println("Problem");
      }
    }
    Pointer pointer = buffer.update(new Pointer(115, buffer), new byte[200]);
    if(buffer.retrieve(pointer).length!=200){
      System.err.println("Problem");
    }
    buffer.retrieve(new Pointer(115, buffer));
  }

  /**
   * Serialize.
   *
   * @param object the object
   * @return the byte[]
   */
  private static byte[] serialize(Object object){
    byte[] objectBytes = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
      ObjectOutput out = new ObjectOutputStream(bos)
      out.writeObject(object);
      objectBytes = bos.toByteArray();
      out.close();
      bos.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return objectBytes;
  }
 
  /**
   * Deserialize.
   *
   * @param <C> the generic type
   * @param bytes the bytes
   * @return the c
   */
  @SuppressWarnings("unchecked")
  private static <C> C deserialize(byte [] bytes){
    Object object = null;
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    try {
      ObjectInput in = new ObjectInputStream(bis);
      object = in.readObject();
      bis.close();
      in.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    catch(ClassNotFoundException e){
      e.printStackTrace();
    }
    return (C)object;
  }
 
}
TOP

Related Classes of com.cetsoft.imcache.test.OffHeapByteBufferTest

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.