Tag Archives: Oracle JDK

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 final 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(final String obj, final String m, final 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(final String toDisplay) {
    return null;
  }

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

  @Override
  public String getProgram(final 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(final Reader reader) {
    return null;
  }

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

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

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

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

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

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

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

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

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

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

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

  @Override
  public void setContext(final ScriptContext context) {
  }

}

META-INF/services/javax.script.ScriptEngineFactory

SampleScriptEngineFactory

A sample is available here.

Advertisements

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 final class ScriptingSample {

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

    final 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"
  }

}

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 final class ScriptingSample {

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

    for (final ScriptEngineFactory factory : manager.getEngineFactories())

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

  }

}

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 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();
  }

}

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 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();
  }

}

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 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);
  }

}

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 final class JdkClient {

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

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

    final 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 final class JdkServer {

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

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

    while (true)
      server.send(packet);

  }

}

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 final class JdkClient {

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

    final InputStream in = client.getInputStream();

    final 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 final class JdkServer {

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

    Socket client = server.accept();

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

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

      final Thread t = new Thread(new Runnable() {

        public void run() {

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

            if (out != null) {

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

            }

          }

        }

      });

      t.start();

      client = server.accept();
    }

    server.close();
  }

}