HTTP streaming transport should allow to exchange binary message #78

flowersinthesand opened this Issue Feb 3, 2015 · 1 comment


None yet

1 participant


Following the decision from vibe-project/vibe-protocol#56 (comment), we need a library to handle Base64. Unfortunately, Java has built-in Base64 encoder/decoder as of 8. However, surprisingly I found out that Jackson supports Base64 through conversion from byte/string to string/byte.

ObjectMapper mapper = new ObjectMapper();
// Using byte[]
String encoded = mapper.convertValue(new byte[]{'h','e','l','l','o'}, String.class);
System.out.println(encoded); // aGVsbG8=
byte[] decoded = mapper.convertValue(encoded, byte[].class);
for (byte b : decoded) {
    System.out.print((char) b);
System.out.println(); // hello

// Using ByteBuffer
ByteBuffer bytes = ByteBuffer.wrap(new byte[]{'h','e','l','l','o'});
String encoded1 = mapper.convertValue(bytes, String.class);
System.out.println(encoded1); // aGVsbG8=
byte[] decoded1 = mapper.convertValue(encoded1, ByteBuffer.class).array();
for (byte b : decoded1) {
    System.out.print((char) b);
System.out.println(); // hello

As we already have used Jackson for JSON, no new dependency will be added.

@flowersinthesand flowersinthesand added this to the 3.0.0-Alpha15 milestone Feb 3, 2015

Test result along with vibe-project/vibe-protocol#56 (comment)

package test;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRegistration;
import javax.servlet.annotation.WebListener;

import org.atmosphere.cpr.ApplicationConfig;
import org.atmosphere.vibe.platform.action.Action;
import org.atmosphere.vibe.platform.bridge.atmosphere2.VibeAtmosphereServlet;
import org.atmosphere.vibe.transport.ServerTransport;
import org.atmosphere.vibe.transport.http.HttpTransportServer;

public class HttpStreamTransportTest implements ServletContextListener {
    public void contextInitialized(ServletContextEvent event) {
        ServletContext context = event.getServletContext();
        HttpTransportServer server = new HttpTransportServer().transportAction(new Action<ServerTransport>() {
            public void on(final ServerTransport transport) {
                transport.textAction(new Action<String>() {
                    public void on(String data) {
                        System.out.println("on text " + data);
                .binaryAction(new Action<ByteBuffer>() {
                    public void on(ByteBuffer data) {
                        System.out.println("on binary " + data + " " + new String(data.array(), Charset.forName("utf-8")));
        Servlet servlet = new VibeAtmosphereServlet().httpAction(server);
        ServletRegistration.Dynamic reg = context.addServlet(VibeAtmosphereServlet.class.getName(), servlet);
        reg.setInitParameter(ApplicationConfig.DISABLE_ATMOSPHEREINTERCEPTOR, Boolean.TRUE.toString());

    public void contextDestroyed(ServletContextEvent sce) {}

// opened
// on text hello
// on binary java.nio.HeapByteBuffer[pos=0 lim=5 cap=5] hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment