org.glassfish.scripting.jython.grizzly.WSGIGrizzlyWrapper Maven / Gradle / Ivy
package org.glassfish.scripting.jython.grizzly;
import com.sun.grizzly.tcp.http11.GrizzlyInputStream;
import org.python.core.PyString;
import java.io.IOException;
// Wrapper for a Grizzly input stream to handle the differences in reading semantics between Python and Java
public class WSGIGrizzlyWrapper {
GrizzlyInputStream myStream;
public static WSGIGrizzlyWrapper wrap(GrizzlyInputStream gis) {
return new WSGIGrizzlyWrapper(gis);
}
private WSGIGrizzlyWrapper(GrizzlyInputStream gis) {
myStream = gis;
}
// Python wants this to read and return all bytes from the stream
public byte[] read() throws IOException {
byte[] read = new byte[myStream.available()];
myStream.read(read);
return read;
}
/*
public byte[] read(int n) throws IOException {
System.out.println("Someone is reading from the wrapper!");
byte[] read = new byte[n];
myStream.read(read);
String theyGot = new String(read);
System.out.println("They will get " + theyGot);
return read;
}
*/
public PyString read(int n) throws IOException {
byte[] read = new byte[n];
myStream.read(read);
return new PyString(new String(read));
}
public int readinto(byte[] b) throws IOException {
return myStream.read(b);
}
}