Software

How to Create a Script Engine with the Java Scripting API

SampleScriptEngineFactory.java

import java.util.ArrayList;
import java.util.List;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;

public class SampleScriptEngineFactory implements ScriptEngineFactory {

  @Override
  public String getEngineName() {
    return null;
  }

  @Override
  public String getEngineVersion() {
    return null;
  }

  @Override
  public List<String> getExtensions() {
    return new ArrayList<String>();
  }

  @Override
  public String getLanguageName() {
    return null;
  }

  @Override
  public String getLanguageVersion() {
    return null;
  }

  @Override
  public String getMethodCallSyntax(String obj, String m, String... args) {
    return null;
  }

  @Override
  public List<String> getMimeTypes() {
    return new ArrayList<String>();
  }

  @Override
  public List<String> getNames() {
    return new ArrayList<String>();
  }

  @Override
  public String getOutputStatement(String toDisplay) {
    return null;
  }

  @Override
  public Object getParameter(String key) {
    return null;
  }

  @Override
  public String getProgram(String... statements) {
    return null;
  }

  @Override
  public ScriptEngine getScriptEngine() {
    return null;
  }

}

SampleScriptEngine.java

import java.io.Reader;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;

public class SampleScriptEngine implements ScriptEngine {

  @Override
  public Bindings createBindings() {
    return null;
  }

  @Override
  public Object eval(Reader reader) {
    return null;
  }

  @Override
  public Object eval(Reader reader, Bindings n) {
    return null;
  }

  @Override
  public Object eval(Reader reader, ScriptContext context) {
    return null;
  }

  @Override
  public Object eval(String script) {
    return null;
  }

  @Override
  public Object eval(String script, Bindings n) {
    return null;
  }

  @Override
  public Object eval(String script, ScriptContext context) {
    return null;
  }

  @Override
  public Object get(String key) {
    return null;
  }

  @Override
  public Bindings getBindings(int scope) {
    return null;
  }

  @Override
  public ScriptContext getContext() {
    return null;
  }

  @Override
  public ScriptEngineFactory getFactory() {
    return null;
  }

  @Override
  public void put(String key, Object value) {
  }

  @Override
  public void setBindings(Bindings bindings, int scope) {
  }

  @Override
  public void setContext(ScriptContext context) {
  }

}

META-INF/services/javax.script.ScriptEngineFactory

SampleScriptEngineFactory

A sample is available here.

Software

How to Call a Script with the Java Scripting API

import static java.lang.System.out;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class ScriptingSample {

  public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();

    ScriptEngine engine = manager.getEngineByName("Sample");

    engine.put("x",  1); // same as engine.getBindings(ScriptContext.ENGINE_SCOPE).put("key", "value")

    out.println(engine.eval("x + 1")); // prints "2"
  }

}
Software

How to List Script Engine Factories with the Java Scripting API

import static java.lang.System.out;

import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;

public class ScriptingSample {

  public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();

    for (ScriptEngineFactory factory : manager.getEngineFactories())

      for (String name : factory.getNames())
        out.println(name);

  }

}
Software

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

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 class JdkSample {

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

    FileChannel fc = f.getChannel();

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

    ByteArrayOutputStream baos = new ByteArrayOutputStream(20);

    ObjectOutputStream oos = new ObjectOutputStream(baos);

    oos.writeObject("Simples Assim");

    map.put(baos.toByteArray());

    oos.close();

    f.close();
  }

}
Software

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

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 class JdkSample {

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

    FileChannel fc = f.getChannel();

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

    byte[] buf = new byte[20];

    map.get(buf);

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

    out.println(ois.readObject());

    ois.close();

    f.close();
  }

}
Software

How to Write an Object to Direct Memory with Oracle JDK

W/ ByteBuffer

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

public class JdkSample {

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

    ByteArrayOutputStream baos = new ByteArrayOutputStream(20);

    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 class JdkSample {

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

    ByteArrayOutputStream baos = new ByteArrayOutputStream(20);

    ObjectOutputStream oos = new ObjectOutputStream(baos);

    oos.writeObject("Simples Assim");

    byte[] buf = baos.toByteArray();

    oos.close();

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

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

    unsafe.freeMemory(address);
  }

}
Software

How to Create an Multicast Client/Server with Oracle JDK

Client

import static java.lang.System.out;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class JdkClient {

  public static void main(String[] args) throws Exception {
    MulticastSocket client = new MulticastSocket(45588);

    client.joinGroup(InetAddress.getByName("224.0.1.0")); // 224.0.0.XXX is reserved

    DatagramPacket packet = new DatagramPacket(new byte[13], 13);

    client.receive(packet);

    out.write(packet.getData());

    client.close();
  }

}

Server

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class JdkServer {

  public static void main(String[] args) throws Exception {
    MulticastSocket server = new MulticastSocket();

    DatagramPacket packet = new DatagramPacket("Simples Assim".getBytes(), 13, InetAddress.getByName("224.0.1.0"), 45588);

    while (true)
      server.send(packet);

  }

}
Software

How to Create an IP Unicast Client/Server with Oracle JDK

Client

import static java.lang.System.out;

import java.io.InputStream;
import java.net.Socket;

public class JdkClient {

  public static void main(String[] args) throws Exception {
    Socket client = new Socket("localhost", 8080);

    InputStream in = client.getInputStream();

    byte[] msg = new byte[13];

    in.read(msg);

    out.println(new String(msg));

    in.close();

    client.close();
  }

}

Server

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class JdkServer {

  public static void main(String[] args) throws Exception {
    ServerSocket server = new ServerSocket(8080);

    Socket client = server.accept();

    while (client != null) {
      OutputStream out = client.getOutputStream();

      byte[] msg = "Simples Assim".getBytes();

      Thread t = new Thread(new Runnable() {

        public void run() {

          try {
            out.write(msg);
          } catch (IOException e) {
            ...
          } finally {

            if (out != null) {

              try {
                out.close();
              } catch (IOException e) {
                e.printStackTrace();
              }

            }

          }

        }

      });

      t.start();

      client = server.accept();
    }

    server.close();
  }

}