1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
package org.msgpack.jruby;
import java.nio.ByteBuffer;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyObject;
import org.jruby.RubyHash;
import org.jruby.RubyIO;
import org.jruby.RubyInteger;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.util.ByteList;
import org.jcodings.Encoding;
@JRubyClass(name="MessagePack::Buffer")
public class Buffer extends RubyObject {
private static final long serialVersionUID = 8441244627425629412L;
private transient IRubyObject io;
private transient ByteBuffer buffer;
private boolean writeMode;
private transient Encoding binaryEncoding;
private static final int CACHE_LINE_SIZE = 64;
private static final int ARRAY_HEADER_SIZE = 24;
public Buffer(Ruby runtime, RubyClass type) {
super(runtime, type);
}
static class BufferAllocator implements ObjectAllocator {
public IRubyObject allocate(Ruby runtime, RubyClass type) {
return new Buffer(runtime, type);
}
}
@JRubyMethod(name = "initialize", optional = 2)
public IRubyObject initialize(ThreadContext ctx, IRubyObject[] args) {
if (args.length > 0) {
IRubyObject io = args[0];
if (io.respondsTo("close") && (io.respondsTo("read") || (io.respondsTo("write") && io.respondsTo("flush")))) {
this.io = io;
}
}
this.buffer = ByteBuffer.allocate(CACHE_LINE_SIZE - ARRAY_HEADER_SIZE);
this.writeMode = true;
this.binaryEncoding = ctx.runtime.getEncodingService().getAscii8bitEncoding();
return this;
}
private void ensureRemainingCapacity(int c) {
if (!writeMode) {
buffer.compact();
writeMode = true;
}
if (buffer.remaining() < c) {
int newLength = Math.max(buffer.capacity() + (buffer.capacity() >> 1), buffer.capacity() + c);
newLength += CACHE_LINE_SIZE - ((ARRAY_HEADER_SIZE + newLength) % CACHE_LINE_SIZE);
buffer = ByteBuffer.allocate(newLength).put(buffer.array(), 0, buffer.position());
}
}
private void ensureReadMode() {
if (writeMode) {
buffer.flip();
writeMode = false;
}
}
private int rawSize() {
if (writeMode) {
return buffer.position();
} else {
return buffer.limit() - buffer.position();
}
}
@JRubyMethod(name = "clear")
public IRubyObject clear(ThreadContext ctx) {
if (!writeMode) {
buffer.compact();
writeMode = true;
}
buffer.clear();
return ctx.runtime.getNil();
}
@JRubyMethod(name = "size")
public IRubyObject size(ThreadContext ctx) {
return ctx.runtime.newFixnum(rawSize());
}
@JRubyMethod(name = "empty?")
public IRubyObject isEmpty(ThreadContext ctx) {
return rawSize() == 0 ? ctx.runtime.getTrue() : ctx.runtime.getFalse();
}
private IRubyObject bufferWrite(ThreadContext ctx, IRubyObject str) {
ByteList bytes = str.asString().getByteList();
int length = bytes.length();
ensureRemainingCapacity(length);
buffer.put(bytes.unsafeBytes(), bytes.begin(), length);
return ctx.runtime.newFixnum(length);
}
@JRubyMethod(name = "write", alias = {"<<"})
public IRubyObject write(ThreadContext ctx, IRubyObject str) {
if (io == null) {
return bufferWrite(ctx, str);
} else {
return io.callMethod(ctx, "write", str);
}
}
private void feed(ThreadContext ctx) {
if (io != null) {
bufferWrite(ctx, io.callMethod(ctx, "read"));
}
}
private IRubyObject readCommon(ThreadContext ctx, IRubyObject[] args, boolean raiseOnUnderflow) {
feed(ctx);
int length = rawSize();
if (args != null && args.length == 1) {
length = (int) args[0].convertToInteger().getLongValue();
}
if (raiseOnUnderflow && rawSize() < length) {
throw ctx.runtime.newEOFError();
}
int readLength = Math.min(length, rawSize());
if (readLength == 0 && length > 0) {
return ctx.runtime.getNil();
} else if (readLength == 0) {
return ctx.runtime.newString();
} else {
ensureReadMode();
byte[] bytes = new byte[readLength];
buffer.get(bytes);
ByteList byteList = new ByteList(bytes, binaryEncoding);
return ctx.runtime.newString(byteList);
}
}
@JRubyMethod(name = "read", optional = 1)
public IRubyObject read(ThreadContext ctx, IRubyObject[] args) {
return readCommon(ctx, args, false);
}
@JRubyMethod(name = "read_all", optional = 1)
public IRubyObject readAll(ThreadContext ctx, IRubyObject[] args) {
return readCommon(ctx, args, true);
}
private IRubyObject skipCommon(ThreadContext ctx, IRubyObject _length, boolean raiseOnUnderflow) {
feed(ctx);
int length = (int) _length.convertToInteger().getLongValue();
if (raiseOnUnderflow && rawSize() < length) {
throw ctx.runtime.newEOFError();
}
ensureReadMode();
int skipLength = Math.min(length, rawSize());
buffer.position(buffer.position() + skipLength);
return ctx.runtime.newFixnum(skipLength);
}
@JRubyMethod(name = "skip")
public IRubyObject skip(ThreadContext ctx, IRubyObject length) {
return skipCommon(ctx, length, false);
}
@JRubyMethod(name = "skip_all")
public IRubyObject skipAll(ThreadContext ctx, IRubyObject length) {
return skipCommon(ctx, length, true);
}
public boolean hasIo() {
return io != null;
}
@JRubyMethod(name = "to_s", alias = {"to_str"})
public IRubyObject toS(ThreadContext ctx) {
ensureReadMode();
int length = buffer.limit() - buffer.position();
ByteList str = new ByteList(buffer.array(), buffer.position(), length, binaryEncoding, true);
return ctx.runtime.newString(str);
}
@JRubyMethod(name = "to_a")
public IRubyObject toA(ThreadContext ctx) {
return ctx.runtime.newArray(toS(ctx));
}
@JRubyMethod(name = "io")
public IRubyObject getIo(ThreadContext ctx) {
return io == null ? ctx.runtime.getNil() : io;
}
@JRubyMethod(name = "flush")
public IRubyObject flush(ThreadContext ctx) {
if (io == null) {
return ctx.runtime.getNil();
} else {
return io.callMethod(ctx, "flush");
}
}
@JRubyMethod(name = "close")
public IRubyObject close(ThreadContext ctx) {
if (io == null) {
return ctx.runtime.getNil();
} else {
return io.callMethod(ctx, "close");
}
}
@JRubyMethod(name = "write_to")
public IRubyObject writeTo(ThreadContext ctx, IRubyObject io) {
return io.callMethod(ctx, "write", readCommon(ctx, null, false));
}
public ByteList getBytes() {
byte[] bytes = new byte[rawSize()];
buffer.get(bytes);
return new ByteList(bytes, binaryEncoding);
}
}
|