Simples Assim

How to Write an Object to a Memory-Mapped File with Oracle JDK

leave a comment »

import static java.nio.channels.FileChannel.MapMode.READ_WRITE;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public final class JdkSample {

  public static void main(final String[] args) throws Exception {
    final RandomAccessFile f = new RandomAccessFile("sample.out", "rw");

    final FileChannel fc = f.getChannel();

    final ByteBuffer map = fc.map(READ_WRITE, 0, 20);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream(20);

    final ObjectOutputStream oos = new ObjectOutputStream(baos);

    oos.writeObject("Simples Assim");

    map.put(baos.toByteArray());

    oos.close();

    f.close();
  }

}
About these ads

Written by Fernando Ribeiro

June 18, 2014 at 4:31 pm

Posted in Software

Tagged with , ,

How to Read an Object from a Memory-Mapped File with Oracle JDK

leave a comment »

import static java.lang.System.out;
import static java.nio.channels.FileChannel.MapMode.READ_ONLY;

import java.io.ByteArrayInputStream;

import java.io.ObjectInputStream;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public final class JdkSample {

  public static void main(final String[] args) throws Exception {
    final RandomAccessFile f = new RandomAccessFile("sample.out", "r");

    final FileChannel fc = f.getChannel();

    final MappedByteBuffer map = chan.map(READ_ONLY, 0, 20);

    final byte[] buf = new byte[20];

    map.get(buf);

    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(buf));

    out.println(ois.readObject());

    ois.close();

    f.close();
  }

}

Written by Fernando Ribeiro

June 18, 2014 at 4:26 pm

Posted in Software

Tagged with , ,

How to Write an Object to Direct Memory with Oracle JDK

leave a comment »

W/ ByteBuffer

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;

public final class JdkSample {

  public static void main(final String[] args) throws Exception {
    final ByteBuffer buf = ByteBuffer.allocateDirect(20);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream(20);

    final ObjectOutputStream oos = new ObjectOutputStream(baos);

    oos.writeObject("Simples Assim");

    buf.put(baos.toByteArray());

    oos.close();
  }

}

W/ Unsafe

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;

import sun.misc.Unsafe;

public final class JdkSample {

  public static void main(final String[] args) throws Exception {
    final Unsafe unsafe = Unsafe.getUnsafe();

    final ByteArrayOutputStream baos = new ByteArrayOutputStream(20);

    final ObjectOutputStream oos = new ObjectOutputStream(baos);

    oos.writeObject("Simples Assim");

    final byte[] buf = baos.toByteArray();

    oos.close();

    final long address = unsafe.allocateMemory(buf.length);

    for (int i = 0; i < buf.length; i++)
      unsafe.putByte(address + i, buf[i]);

    unsafe.freeMemory(address);
  }

}

Written by Fernando Ribeiro

June 17, 2014 at 9:51 pm

Posted in Software

Tagged with , ,

Quotation by Larry Ellison

leave a comment »

When you innovate, you’ve got to be prepared for everyone telling you you’re nuts.

http://www.oracle.com/us/corporate/press/BoardofDirectors/ellison/index.html

Written by Fernando Ribeiro

June 4, 2014 at 9:45 pm

Posted in Business, Technology

Tagged with

Quotation by Leonardo da Vinci

leave a comment »

Simplicity is the ultimate sophistication.

http://en.wikipedia.org/wiki/Leonardo_da_vinci

Written by Fernando Ribeiro

May 31, 2014 at 10:58 pm

Posted in Uncategorized

Tagged with

Quotation by Aristotle

leave a comment »

The whole is greater than the sum of its parts.

http://en.wikipedia.org/wiki/Aristotle

Written by Fernando Ribeiro

May 31, 2014 at 10:57 pm

Posted in Uncategorized

Tagged with

Quotation by Joshua McKenty

leave a comment »

Simple is the only thing that does scale.

(from here)

https://www.linkedin.com/in/joshuamckenty

Written by Fernando Ribeiro

May 31, 2014 at 10:56 pm

Posted in Business, Software, Technology

Tagged with

Follow

Get every new post delivered to your Inbox.

Join 1,159 other followers