This commit is contained in:
wentch
2016-01-06 14:08:04 +08:00
parent e94b5706ea
commit 876dfcc8fc
19 changed files with 571 additions and 162 deletions

View File

@@ -41,7 +41,7 @@ public final class EnMember<W extends Writer, T, F> implements Comparable<EnMemb
public boolean write(final W out, final boolean comma, final T obj) { public boolean write(final W out, final boolean comma, final T obj) {
F value = attribute.get(obj); F value = attribute.get(obj);
if (value == null) return comma; if (value == null) return comma;
if (out.isTiny()) { if (out.tiny()) {
if (istring) { if (istring) {
if (((CharSequence) value).length() == 0) return comma; if (((CharSequence) value).length() == 0) return comma;
} else if (isbool) { } else if (isbool) {

View File

@@ -124,8 +124,9 @@ public abstract class Factory<R extends Reader, W extends Writer> {
return convert; return convert;
} }
public void setTiny(boolean tiny) { public Factory tiny(boolean tiny) {
this.tiny = tiny; this.tiny = tiny;
return this;
} }
public ConvertColumnEntry findRef(AccessibleObject field) { public ConvertColumnEntry findRef(AccessibleObject field) {

View File

@@ -21,7 +21,7 @@ public interface Writer {
* *
* @return 是否简化 * @return 是否简化
*/ */
public boolean isTiny(); public boolean tiny();
/** /**
* 输出null值 * 输出null值

View File

@@ -14,7 +14,7 @@ import org.redkale.util.*;
* *
* @author zhangjx * @author zhangjx
*/ */
public final class BsonByteBufferReader extends BsonReader { public class BsonByteBufferReader extends BsonReader {
private ByteBuffer[] buffers; private ByteBuffer[] buffers;
@@ -24,7 +24,7 @@ public final class BsonByteBufferReader extends BsonReader {
protected BsonByteBufferReader(ByteBuffer... buffers) { protected BsonByteBufferReader(ByteBuffer... buffers) {
this.buffers = buffers; this.buffers = buffers;
this.currentBuffer = buffers[currentIndex]; if (buffers != null && buffers.length > 0) this.currentBuffer = buffers[currentIndex];
} }
@Override @Override
@@ -47,7 +47,7 @@ public final class BsonByteBufferReader extends BsonReader {
* @return 数组长度或 SIGN_NULL * @return 数组长度或 SIGN_NULL
*/ */
@Override @Override
public int readArrayB() { public final int readArrayB() {
short bt = readShort(); short bt = readShort();
if (bt == Reader.SIGN_NULL) return bt; if (bt == Reader.SIGN_NULL) return bt;
short lt = readShort(); short lt = readShort();
@@ -56,7 +56,7 @@ public final class BsonByteBufferReader extends BsonReader {
//------------------------------------------------------------ //------------------------------------------------------------
@Override @Override
public boolean readBoolean() { public final boolean readBoolean() {
return readByte() == 1; return readByte() == 1;
} }
@@ -76,41 +76,49 @@ public final class BsonByteBufferReader extends BsonReader {
} }
@Override @Override
public char readChar() { public final char readChar() {
int remain = this.currentBuffer.remaining(); if (this.currentBuffer != null) {
if (remain >= 2) { int remain = this.currentBuffer.remaining();
this.position += 2; if (remain >= 2) {
return this.currentBuffer.getChar(); this.position += 2;
return this.currentBuffer.getChar();
}
} }
return (char) ((0xff00 & (readByte() << 8)) | (0xff & readByte())); return (char) ((0xff00 & (readByte() << 8)) | (0xff & readByte()));
} }
@Override @Override
public short readShort() { public final short readShort() {
int remain = this.currentBuffer.remaining(); if (this.currentBuffer != null) {
if (remain >= 2) { int remain = this.currentBuffer.remaining();
this.position += 2; if (remain >= 2) {
return this.currentBuffer.getShort(); this.position += 2;
return this.currentBuffer.getShort();
}
} }
return (short) ((0xff00 & (readByte() << 8)) | (0xff & readByte())); return (short) ((0xff00 & (readByte() << 8)) | (0xff & readByte()));
} }
@Override @Override
public int readInt() { public final int readInt() {
int remain = this.currentBuffer.remaining(); if (this.currentBuffer != null) {
if (remain >= 4) { int remain = this.currentBuffer.remaining();
this.position += 4; if (remain >= 4) {
return this.currentBuffer.getInt(); this.position += 4;
return this.currentBuffer.getInt();
}
} }
return ((readByte() & 0xff) << 24) | ((readByte() & 0xff) << 16) | ((readByte() & 0xff) << 8) | (readByte() & 0xff); return ((readByte() & 0xff) << 24) | ((readByte() & 0xff) << 16) | ((readByte() & 0xff) << 8) | (readByte() & 0xff);
} }
@Override @Override
public long readLong() { public final long readLong() {
int remain = this.currentBuffer.remaining(); if (this.currentBuffer != null) {
if (remain >= 8) { int remain = this.currentBuffer.remaining();
this.position += 8; if (remain >= 8) {
return this.currentBuffer.getLong(); this.position += 8;
return this.currentBuffer.getLong();
}
} }
return ((((long) readByte() & 0xff) << 56) return ((((long) readByte() & 0xff) << 56)
| (((long) readByte() & 0xff) << 48) | (((long) readByte() & 0xff) << 48)
@@ -128,7 +136,7 @@ public final class BsonByteBufferReader extends BsonReader {
return bs; return bs;
} }
protected void read(final byte[] bs, final int pos) { private void read(final byte[] bs, final int pos) {
int remain = this.currentBuffer.remaining(); int remain = this.currentBuffer.remaining();
if (remain < 1) { if (remain < 1) {
this.currentBuffer = this.buffers[++this.currentIndex]; this.currentBuffer = this.buffers[++this.currentIndex];
@@ -148,14 +156,14 @@ public final class BsonByteBufferReader extends BsonReader {
} }
@Override @Override
public String readSmallString() { public final String readSmallString() {
int len = 0xff & readByte(); int len = 0xff & readByte();
if (len == 0) return ""; if (len == 0) return "";
return new String(read(len)); return new String(read(len));
} }
@Override @Override
public String readString() { public final String readString() {
int len = readInt(); int len = readInt();
if (len == SIGN_NULL) return null; if (len == SIGN_NULL) return null;
if (len == 0) return ""; if (len == 0) return "";

View File

@@ -10,10 +10,12 @@ import java.util.function.*;
/** /**
* *
* <p> 详情见: http://www.redkale.org * <p>
* 详情见: http://www.redkale.org
*
* @author zhangjx * @author zhangjx
*/ */
public final class BsonByteBufferWriter extends BsonWriter { public class BsonByteBufferWriter extends BsonWriter {
private final Supplier<ByteBuffer> supplier; private final Supplier<ByteBuffer> supplier;
@@ -21,8 +23,9 @@ public final class BsonByteBufferWriter extends BsonWriter {
private int index; private int index;
protected BsonByteBufferWriter(Supplier<ByteBuffer> supplier) { protected BsonByteBufferWriter(boolean tiny, Supplier<ByteBuffer> supplier) {
super((byte[]) null); super((byte[]) null);
this.tiny = tiny;
this.supplier = supplier; this.supplier = supplier;
} }
@@ -56,7 +59,7 @@ public final class BsonByteBufferWriter extends BsonWriter {
} }
@Override @Override
public BsonByteBufferWriter setTiny(boolean tiny) { public BsonByteBufferWriter tiny(boolean tiny) {
this.tiny = tiny; this.tiny = tiny;
return this; return this;
} }

View File

@@ -5,6 +5,7 @@
*/ */
package org.redkale.convert.bson; package org.redkale.convert.bson;
import java.io.*;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.nio.*; import java.nio.*;
import java.util.function.*; import java.util.function.*;
@@ -49,16 +50,18 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
this.tiny = tiny; this.tiny = tiny;
} }
public BsonByteBufferWriter pollBsonWriter(final Supplier<ByteBuffer> supplier) { @Override
return new BsonByteBufferWriter(supplier).setTiny(tiny); public BsonFactory getFactory() {
return (BsonFactory) factory;
} }
public BsonWriter pollBsonWriter() { //------------------------------ reader -----------------------------------------------------------
return writerPool.get().setTiny(tiny); public BsonReader pollBsonReader(final ByteBuffer... buffers) {
return new BsonByteBufferReader(buffers);
} }
public void offerBsonWriter(BsonWriter out) { public BsonReader pollBsonReader(final InputStream in) {
if (out != null) writerPool.offer(out); return new BsonStreamReader(in);
} }
public BsonReader pollBsonReader() { public BsonReader pollBsonReader() {
@@ -69,6 +72,24 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
if (in != null) readerPool.offer(in); if (in != null) readerPool.offer(in);
} }
//------------------------------ writer -----------------------------------------------------------
public BsonByteBufferWriter pollBsonWriter(final Supplier<ByteBuffer> supplier) {
return new BsonByteBufferWriter(tiny, supplier);
}
public BsonWriter pollBsonWriter(final OutputStream out) {
return new BsonStreamWriter(tiny, out);
}
public BsonWriter pollBsonWriter() {
return writerPool.get().tiny(tiny);
}
public void offerBsonWriter(BsonWriter out) {
if (out != null) writerPool.offer(out);
}
//------------------------------ convertFrom -----------------------------------------------------------
public <T> T convertFrom(final Type type, final byte[] bytes) { public <T> T convertFrom(final Type type, final byte[] bytes) {
if (bytes == null) return null; if (bytes == null) return null;
return convertFrom(type, bytes, 0, bytes.length); return convertFrom(type, bytes, 0, bytes.length);
@@ -89,6 +110,11 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
return (T) factory.loadDecoder(type).convertFrom(new BsonByteBufferReader(buffers)); return (T) factory.loadDecoder(type).convertFrom(new BsonByteBufferReader(buffers));
} }
public <T> T convertFrom(final Type type, final InputStream in) {
if (type == null || in == null) return null;
return (T) factory.loadDecoder(type).convertFrom(new BsonStreamReader(in));
}
public <T> T convertFrom(final BsonReader in, final Type type) { public <T> T convertFrom(final BsonReader in, final Type type) {
if (type == null) return null; if (type == null) return null;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -96,42 +122,27 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
return rs; return rs;
} }
//------------------------------ convertTo -----------------------------------------------------------
public byte[] convertTo(Object value) {
if (value == null) {
final BsonWriter out = writerPool.get().tiny(tiny);
out.writeNull();
byte[] result = out.toArray();
writerPool.offer(out);
return result;
}
return convertTo(value.getClass(), value);
}
public byte[] convertTo(final Type type, Object value) { public byte[] convertTo(final Type type, Object value) {
if (type == null) return null; if (type == null) return null;
final BsonWriter out = writerPool.get().setTiny(tiny); final BsonWriter out = writerPool.get().tiny(tiny);
factory.loadEncoder(type).convertTo(out, value); factory.loadEncoder(type).convertTo(out, value);
byte[] result = out.toArray(); byte[] result = out.toArray();
writerPool.offer(out); writerPool.offer(out);
return result; return result;
} }
public void convertTo(final BsonWriter out, final Type type, Object value) {
if (type == null) return;
factory.loadEncoder(type).convertTo(out, value);
}
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Type type, Object value) {
if (supplier == null || type == null) return null;
BsonByteBufferWriter out = new BsonByteBufferWriter(supplier);
if (value == null) {
out.writeNull();
} else {
factory.loadEncoder(type).convertTo(out, value);
}
return out.toBuffers();
}
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, Object value) {
if (supplier == null) return null;
BsonByteBufferWriter out = new BsonByteBufferWriter(supplier);
if (value == null) {
out.writeNull();
} else {
factory.loadEncoder(value.getClass()).convertTo(out, value);
}
return out.toBuffers();
}
public void convertTo(final BsonWriter out, Object value) { public void convertTo(final BsonWriter out, Object value) {
if (value == null) { if (value == null) {
out.writeNull(); out.writeNull();
@@ -140,26 +151,60 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
} }
} }
public byte[] convertTo(Object value) { public void convertTo(final BsonWriter out, final Type type, Object value) {
if (value == null) { if (type == null) return;
final BsonWriter out = writerPool.get().setTiny(tiny); factory.loadEncoder(type).convertTo(out, value);
out.writeNull();
byte[] result = out.toArray();
writerPool.offer(out);
return result;
}
return convertTo(value.getClass(), value);
} }
public BsonWriter convertToWriter(final Type type, Object value) { public void convertTo(final OutputStream out, Object value) {
if (type == null) return null; if (value == null) {
final BsonWriter out = writerPool.get().setTiny(tiny); new BsonStreamWriter(tiny, out).writeNull();
factory.loadEncoder(type).convertTo(out, value); } else {
return out; factory.loadEncoder(value.getClass()).convertTo(new BsonStreamWriter(tiny, out), value);
}
}
public void convertTo(final OutputStream out, final Type type, Object value) {
if (type == null) return;
if (value == null) {
new BsonStreamWriter(tiny, out).writeNull();
} else {
factory.loadEncoder(type).convertTo(new BsonStreamWriter(tiny, out), value);
}
}
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Type type, Object value) {
if (supplier == null || type == null) return null;
BsonByteBufferWriter out = new BsonByteBufferWriter(tiny, supplier);
if (value == null) {
out.writeNull();
} else {
factory.loadEncoder(type).convertTo(out, value);
}
return out.toBuffers();
}
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, Object value) {
if (supplier == null) return null;
BsonByteBufferWriter out = new BsonByteBufferWriter(tiny, supplier);
if (value == null) {
out.writeNull();
} else {
factory.loadEncoder(value.getClass()).convertTo(out, value);
}
return out.toBuffers();
} }
public BsonWriter convertToWriter(Object value) { public BsonWriter convertToWriter(Object value) {
if (value == null) return null; if (value == null) return null;
return convertToWriter(value.getClass(), value); return convertToWriter(value.getClass(), value);
} }
public BsonWriter convertToWriter(final Type type, Object value) {
if (type == null) return null;
final BsonWriter out = writerPool.get().tiny(tiny);
factory.loadEncoder(type).convertTo(out, value);
return out;
}
} }

View File

@@ -10,7 +10,9 @@ import org.redkale.convert.*;
/** /**
* *
* <p> 详情见: http://www.redkale.org * <p>
* 详情见: http://www.redkale.org
*
* @author zhangjx * @author zhangjx
*/ */
public final class BsonFactory extends Factory<BsonReader, BsonWriter> { public final class BsonFactory extends Factory<BsonReader, BsonWriter> {
@@ -30,6 +32,12 @@ public final class BsonFactory extends Factory<BsonReader, BsonWriter> {
super(parent, tiny); super(parent, tiny);
} }
@Override
public BsonFactory tiny(boolean tiny) {
this.tiny = tiny;
return this;
}
public static BsonFactory root() { public static BsonFactory root() {
return instance; return instance;
} }

View File

@@ -0,0 +1,60 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.convert.bson;
import java.io.*;
import org.redkale.convert.*;
/**
*
* @author zhangjx
*/
class BsonStreamReader extends BsonByteBufferReader {
private InputStream in;
private byte currByte;
protected BsonStreamReader(InputStream in) {
this.in = in;
}
@Override
protected boolean recycle() {
super.recycle(); // this.position 初始化值为-1
this.in = null;
this.currByte = 0;
return false;
}
@Override
public byte readByte() {
try {
byte b = (currByte = (byte) in.read());
this.position++;
return b;
} catch (IOException e) {
throw new ConvertException(e);
}
}
@Override
protected byte currentByte() {
return currByte;
}
@Override
protected byte[] read(final int len) {
byte[] bs = new byte[len];
try {
in.read(bs);
this.position += len;
} catch (IOException e) {
throw new ConvertException(e);
}
return bs;
}
}

View File

@@ -0,0 +1,48 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.convert.bson;
import java.io.*;
import org.redkale.convert.*;
/**
*
* @author zhangjx
*/
class BsonStreamWriter extends BsonByteBufferWriter {
private OutputStream out;
protected BsonStreamWriter(boolean tiny, OutputStream out) {
super(tiny, null);
this.out = out;
}
@Override
protected boolean recycle() {
super.recycle();
this.out = null;
return false;
}
@Override
public void writeTo(final byte[] chs, final int start, final int len) {
try {
out.write(chs, start, len);
} catch (IOException e) {
throw new ConvertException(e);
}
}
@Override
public void writeTo(final byte ch) {
try {
out.write((byte) ch);
} catch (IOException e) {
throw new ConvertException(e);
}
}
}

View File

@@ -65,11 +65,11 @@ public class BsonWriter implements Writer {
} }
@Override @Override
public final boolean isTiny() { public final boolean tiny() {
return tiny; return tiny;
} }
public BsonWriter setTiny(boolean tiny) { public BsonWriter tiny(boolean tiny) {
this.tiny = tiny; this.tiny = tiny;
return this; return this;
} }

View File

@@ -27,7 +27,7 @@ public class JsonByteBufferReader extends JsonReader {
protected JsonByteBufferReader(ByteBuffer... buffers) { protected JsonByteBufferReader(ByteBuffer... buffers) {
this.buffers = buffers; this.buffers = buffers;
this.currentBuffer = buffers[currentIndex]; if (buffers != null && buffers.length > 0) this.currentBuffer = buffers[currentIndex];
} }
@Override @Override
@@ -66,8 +66,10 @@ public class JsonByteBufferReader extends JsonReader {
this.currentChar = 0; this.currentChar = 0;
return ch; return ch;
} }
int remain = this.currentBuffer.remaining(); if (this.currentBuffer != null) {
if (remain == 0 && this.currentIndex + 1 >= this.buffers.length) return 0; int remain = this.currentBuffer.remaining();
if (remain == 0 && this.currentIndex + 1 >= this.buffers.length) return 0;
}
byte b1 = nextByte(); byte b1 = nextByte();
if (b1 >= 0) {// 1 byte, 7 bits: 0xxxxxxx if (b1 >= 0) {// 1 byte, 7 bits: 0xxxxxxx
return (char) b1; return (char) b1;
@@ -268,7 +270,7 @@ public class JsonByteBufferReader extends JsonReader {
int value = 0; int value = 0;
final boolean negative = firstchar == '-'; final boolean negative = firstchar == '-';
if (!negative) { if (!negative) {
if (firstchar < '0' || firstchar > '9') throw new NumberFormatException("illegal escape(" + firstchar + ") (position = " + position + ")"); if (firstchar < '0' || firstchar > '9') throw new ConvertException("illegal escape(" + firstchar + ") (position = " + position + ")");
value = firstchar - '0'; value = firstchar - '0';
} }
for (;;) { for (;;) {
@@ -281,7 +283,7 @@ public class JsonByteBufferReader extends JsonReader {
backChar(ch); backChar(ch);
break; break;
} else { } else {
throw new NumberFormatException("illegal escape(" + ch + ") (position = " + position + ")"); throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
} }
} }
return negative ? -value : value; return negative ? -value : value;
@@ -302,7 +304,7 @@ public class JsonByteBufferReader extends JsonReader {
long value = 0; long value = 0;
final boolean negative = firstchar == '-'; final boolean negative = firstchar == '-';
if (!negative) { if (!negative) {
if (firstchar < '0' || firstchar > '9') throw new NumberFormatException("illegal escape(" + firstchar + ") (position = " + position + ")"); if (firstchar < '0' || firstchar > '9') throw new ConvertException("illegal escape(" + firstchar + ") (position = " + position + ")");
value = firstchar - '0'; value = firstchar - '0';
} }
for (;;) { for (;;) {
@@ -315,7 +317,7 @@ public class JsonByteBufferReader extends JsonReader {
backChar(ch); backChar(ch);
break; break;
} else { } else {
throw new NumberFormatException("illegal escape(" + ch + ") (position = " + position + ")"); throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")");
} }
} }
return negative ? -value : value; return negative ? -value : value;

View File

@@ -9,18 +9,21 @@ import java.nio.*;
import java.nio.charset.*; import java.nio.charset.*;
import java.util.*; import java.util.*;
import java.util.function.*; import java.util.function.*;
import org.redkale.convert.*;
import org.redkale.util.*; import org.redkale.util.*;
/** /**
* *
* <p> 详情见: http://www.redkale.org * <p>
* 详情见: http://www.redkale.org
*
* @author zhangjx * @author zhangjx
*/ */
public final class JsonByteBufferWriter extends JsonWriter { public class JsonByteBufferWriter extends JsonWriter {
private static final Charset UTF8 = Charset.forName("UTF-8"); protected static final Charset UTF8 = Charset.forName("UTF-8");
private final Charset charset; protected Charset charset;
private final Supplier<ByteBuffer> supplier; private final Supplier<ByteBuffer> supplier;
@@ -28,17 +31,18 @@ public final class JsonByteBufferWriter extends JsonWriter {
private int index; private int index;
protected JsonByteBufferWriter(Supplier<ByteBuffer> supplier) { protected JsonByteBufferWriter(boolean tiny, Supplier<ByteBuffer> supplier) {
this(null, supplier); this(tiny, null, supplier);
} }
protected JsonByteBufferWriter(Charset charset, Supplier<ByteBuffer> supplier) { protected JsonByteBufferWriter(boolean tiny, Charset charset, Supplier<ByteBuffer> supplier) {
this.tiny = tiny;
this.charset = UTF8.equals(charset) ? null : charset; this.charset = UTF8.equals(charset) ? null : charset;
this.supplier = supplier; this.supplier = supplier;
} }
@Override @Override
public JsonByteBufferWriter setTiny(boolean tiny) { public JsonByteBufferWriter tiny(boolean tiny) {
this.tiny = tiny; this.tiny = tiny;
return this; return this;
} }
@@ -46,6 +50,7 @@ public final class JsonByteBufferWriter extends JsonWriter {
@Override @Override
protected boolean recycle() { protected boolean recycle() {
this.index = 0; this.index = 0;
this.charset = null;
this.buffers = null; this.buffers = null;
return false; return false;
} }
@@ -101,13 +106,13 @@ public final class JsonByteBufferWriter extends JsonWriter {
@Override @Override
public void writeTo(final char ch) { public void writeTo(final char ch) {
if (ch > Byte.MAX_VALUE) throw new RuntimeException("writeTo char(int.value = " + (int) ch + ") must be less 127"); if (ch > Byte.MAX_VALUE) throw new ConvertException("writeTo char(int.value = " + (int) ch + ") must be less 127");
expand(1); expand(1);
this.buffers[index].put((byte) ch); this.buffers[index].put((byte) ch);
} }
@Override @Override
public final void writeTo(final char[] chs, final int start, final int len) { public void writeTo(final char[] chs, final int start, final int len) {
writeTo(-1, false, chs, start, len); writeTo(-1, false, chs, start, len);
} }
@@ -239,7 +244,7 @@ public final class JsonByteBufferWriter extends JsonWriter {
* @param value String值 * @param value String值
*/ */
@Override @Override
public final void writeTo(final boolean quote, final String value) { public void writeTo(final boolean quote, final String value) {
char[] chs = Utility.charArray(value); char[] chs = Utility.charArray(value);
writeTo(-1, quote, chs, 0, chs.length); writeTo(-1, quote, chs, 0, chs.length);
} }
@@ -330,18 +335,6 @@ public final class JsonByteBufferWriter extends JsonWriter {
writeTo(expandsize, true, cs, 0, sb.length()); writeTo(expandsize, true, cs, 0, sb.length());
} }
@Override
public final void writeField(boolean comma, Attribute attribute) {
if (comma) writeTo(',');
writeTo(true, attribute.field());
writeTo(':');
}
@Override
public final void writeSmallString(String value) {
writeTo(false, value);
}
@Override @Override
public String toString() { public String toString() {
return Objects.toString(this); return Objects.toString(this);

View File

@@ -5,6 +5,7 @@
*/ */
package org.redkale.convert.json; package org.redkale.convert.json;
import java.io.*;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.nio.*; import java.nio.*;
import java.nio.charset.*; import java.nio.charset.*;
@@ -14,7 +15,9 @@ import org.redkale.util.*;
/** /**
* *
* <p> 详情见: http://www.redkale.org * <p>
* 详情见: http://www.redkale.org
*
* @author zhangjx * @author zhangjx
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -34,20 +37,18 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
this.tiny = tiny; this.tiny = tiny;
} }
public JsonByteBufferWriter pollJsonWriter(final Supplier<ByteBuffer> supplier) { @Override
return new JsonByteBufferWriter(supplier).setTiny(tiny); public JsonFactory getFactory() {
return (JsonFactory) factory;
} }
public JsonByteBufferWriter pollJsonWriter(final Charset charset, final Supplier<ByteBuffer> supplier) { //------------------------------ reader -----------------------------------------------------------
return new JsonByteBufferWriter(charset, supplier).setTiny(tiny); public JsonReader pollJsonReader(final ByteBuffer... buffers) {
return new JsonByteBufferReader(buffers);
} }
public JsonWriter pollJsonWriter() { public JsonReader pollJsonReader(final InputStream in) {
return writerPool.get().setTiny(tiny); return new JsonStreamReader(in);
}
public void offerJsonWriter(JsonWriter out) {
if (out != null) writerPool.offer(out);
} }
public JsonReader pollJsonReader() { public JsonReader pollJsonReader() {
@@ -58,11 +59,28 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
if (in != null) readerPool.offer(in); if (in != null) readerPool.offer(in);
} }
@Override //------------------------------ writer -----------------------------------------------------------
public JsonFactory getFactory() { public JsonByteBufferWriter pollJsonWriter(final Supplier<ByteBuffer> supplier) {
return (JsonFactory) factory; return new JsonByteBufferWriter(tiny, supplier);
} }
public JsonWriter pollJsonWriter(final OutputStream out) {
return new JsonStreamWriter(tiny, out);
}
public JsonWriter pollJsonWriter(final Charset charset, final OutputStream out) {
return new JsonStreamWriter(tiny, charset, out);
}
public JsonWriter pollJsonWriter() {
return writerPool.get().tiny(tiny);
}
public void offerJsonWriter(JsonWriter out) {
if (out != null) writerPool.offer(out);
}
//------------------------------ convertFrom -----------------------------------------------------------
public <T> T convertFrom(final Type type, final String text) { public <T> T convertFrom(final Type type, final String text) {
if (text == null) return null; if (text == null) return null;
return convertFrom(type, Utility.charArray(text)); return convertFrom(type, Utility.charArray(text));
@@ -87,19 +105,40 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
return (T) factory.loadDecoder(type).convertFrom(new JsonByteBufferReader(buffers)); return (T) factory.loadDecoder(type).convertFrom(new JsonByteBufferReader(buffers));
} }
public <T> T convertFrom(final Type type, final InputStream in) {
if (type == null || in == null) return null;
return (T) factory.loadDecoder(type).convertFrom(new JsonStreamReader(in));
}
public <T> T convertFrom(final JsonReader in, final Type type) {
if (type == null) return null;
@SuppressWarnings("unchecked")
T rs = (T) factory.loadDecoder(type).convertFrom(in);
return rs;
}
//------------------------------ convertTo -----------------------------------------------------------
public String convertTo(Object value) {
if (value == null) return "null";
return convertTo(value.getClass(), value);
}
public String convertTo(final Type type, Object value) { public String convertTo(final Type type, Object value) {
if (type == null) return null; if (type == null) return null;
if (value == null) return "null"; if (value == null) return "null";
final JsonWriter out = writerPool.get().setTiny(tiny); final JsonWriter out = writerPool.get().tiny(tiny);
factory.loadEncoder(type).convertTo(out, value); factory.loadEncoder(type).convertTo(out, value);
String result = out.toString(); String result = out.toString();
writerPool.offer(out); writerPool.offer(out);
return result; return result;
} }
public String convertTo(Object value) { public void convertTo(final JsonWriter out, Object value) {
if (value == null) return "null"; if (value == null) {
return convertTo(value.getClass(), value); out.writeNull();
} else {
factory.loadEncoder(value.getClass()).convertTo(out, value);
}
} }
public void convertTo(final JsonWriter out, final Type type, Object value) { public void convertTo(final JsonWriter out, final Type type, Object value) {
@@ -111,21 +150,37 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
} }
} }
public void convertTo(final JsonWriter out, Object value) { public void convertTo(final OutputStream out, Object value) {
if (value == null) {
new JsonStreamWriter(tiny, out).writeNull();
} else {
factory.loadEncoder(value.getClass()).convertTo(new JsonStreamWriter(tiny, out), value);
}
}
public void convertTo(final OutputStream out, final Type type, Object value) {
if (type == null) return;
if (value == null) {
new JsonStreamWriter(tiny, out).writeNull();
} else {
factory.loadEncoder(type).convertTo(new JsonStreamWriter(tiny, out), value);
}
}
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, Object value) {
if (supplier == null) return null;
JsonByteBufferWriter out = new JsonByteBufferWriter(tiny, null, supplier);
if (value == null) { if (value == null) {
out.writeNull(); out.writeNull();
} else { } else {
factory.loadEncoder(value.getClass()).convertTo(out, value); factory.loadEncoder(value.getClass()).convertTo(out, value);
} }
return out.toBuffers();
} }
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Type type, Object value) { public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, final Type type, Object value) {
return convertTo(null, supplier, type, value);
}
public ByteBuffer[] convertTo(final Charset charset, final Supplier<ByteBuffer> supplier, final Type type, Object value) {
if (supplier == null || type == null) return null; if (supplier == null || type == null) return null;
JsonByteBufferWriter out = new JsonByteBufferWriter(charset, supplier); JsonByteBufferWriter out = new JsonByteBufferWriter(tiny, null, supplier);
if (value == null) { if (value == null) {
out.writeNull(); out.writeNull();
} else { } else {
@@ -134,18 +189,15 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
return out.toBuffers(); return out.toBuffers();
} }
public ByteBuffer[] convertTo(final Supplier<ByteBuffer> supplier, Object value) { public JsonWriter convertToWriter(Object value) {
return convertTo(null, supplier, value); if (value == null) return null;
return convertToWriter(value.getClass(), value);
} }
public ByteBuffer[] convertTo(final Charset charset, final Supplier<ByteBuffer> supplier, Object value) { public JsonWriter convertToWriter(final Type type, Object value) {
if (supplier == null) return null; if (type == null) return null;
JsonByteBufferWriter out = new JsonByteBufferWriter(charset, supplier); final JsonWriter out = writerPool.get().tiny(tiny);
if (value == null) { factory.loadEncoder(type).convertTo(out, value);
out.writeNull(); return out;
} else {
factory.loadEncoder(value.getClass()).convertTo(out, value);
}
return out.toBuffers();
} }
} }

View File

@@ -14,7 +14,9 @@ import org.redkale.util.*;
/** /**
* *
* <p> 详情见: http://www.redkale.org * <p>
* 详情见: http://www.redkale.org
*
* @author zhangjx * @author zhangjx
*/ */
public final class JsonFactory extends Factory<JsonReader, JsonWriter> { public final class JsonFactory extends Factory<JsonReader, JsonWriter> {
@@ -32,6 +34,12 @@ public final class JsonFactory extends Factory<JsonReader, JsonWriter> {
super(parent, tiny); super(parent, tiny);
} }
@Override
public JsonFactory tiny(boolean tiny) {
this.tiny = tiny;
return this;
}
public static JsonFactory root() { public static JsonFactory root() {
return instance; return instance;
} }

View File

@@ -350,7 +350,7 @@ public class JsonReader implements Reader {
int value = 0; int value = 0;
final boolean negative = firstchar == '-'; final boolean negative = firstchar == '-';
if (!negative) { if (!negative) {
if (firstchar < '0' || firstchar > '9') throw new NumberFormatException("illegal escape(" + firstchar + ") (position = " + currpos + ") in (" + new String(this.text) + ")"); if (firstchar < '0' || firstchar > '9') throw new ConvertException("illegal escape(" + firstchar + ") (position = " + currpos + ") in (" + new String(this.text) + ")");
value = firstchar - '0'; value = firstchar - '0';
} }
for (;;) { for (;;) {
@@ -362,7 +362,7 @@ public class JsonReader implements Reader {
} else if (ch == ',' || ch == '}' || ch == ']' || ch <= ' ' || ch == ':') { } else if (ch == ',' || ch == '}' || ch == ']' || ch <= ' ' || ch == ':') {
break; break;
} else { } else {
throw new NumberFormatException("illegal escape(" + ch + ") (position = " + currpos + ") in (" + new String(this.text) + ")"); throw new ConvertException("illegal escape(" + ch + ") (position = " + currpos + ") in (" + new String(this.text) + ")");
} }
} }
this.position = currpos - 1; this.position = currpos - 1;
@@ -396,7 +396,7 @@ public class JsonReader implements Reader {
long value = 0; long value = 0;
final boolean negative = firstchar == '-'; final boolean negative = firstchar == '-';
if (!negative) { if (!negative) {
if (firstchar < '0' || firstchar > '9') throw new NumberFormatException("illegal escape(" + firstchar + ") (position = " + currpos + ") in (" + new String(this.text) + ")"); if (firstchar < '0' || firstchar > '9') throw new ConvertException("illegal escape(" + firstchar + ") (position = " + currpos + ") in (" + new String(this.text) + ")");
value = firstchar - '0'; value = firstchar - '0';
} }
for (;;) { for (;;) {
@@ -408,7 +408,7 @@ public class JsonReader implements Reader {
} else if (ch == ',' || ch == '}' || ch == ']' || ch <= ' ' || ch == ':') { } else if (ch == ',' || ch == '}' || ch == ']' || ch <= ' ' || ch == ':') {
break; break;
} else { } else {
throw new NumberFormatException("illegal escape(" + ch + ") (position = " + currpos + ") but '" + ch + "' in (" + new String(this.text) + ")"); throw new ConvertException("illegal escape(" + ch + ") (position = " + currpos + ") but '" + ch + "' in (" + new String(this.text) + ")");
} }
} }
this.position = currpos - 1; this.position = currpos - 1;

View File

@@ -0,0 +1,40 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.convert.json;
import java.io.*;
import org.redkale.convert.*;
/**
*
* @author zhangjx
*/
class JsonStreamReader extends JsonByteBufferReader {
private InputStream in;
protected JsonStreamReader(InputStream in) {
this.in = in;
}
@Override
protected boolean recycle() {
super.recycle(); // this.position 初始化值为-1
this.in = null;
return false;
}
@Override
protected byte nextByte() {
try {
byte b = (byte) in.read();
this.position++;
return b;
} catch (IOException e) {
throw new ConvertException(e);
}
}
}

View File

@@ -0,0 +1,141 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.convert.json;
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
import org.redkale.convert.*;
import org.redkale.util.*;
/**
*
* @author zhangjx
*/
class JsonStreamWriter extends JsonByteBufferWriter {
private OutputStream out;
protected JsonStreamWriter(boolean tiny, OutputStream out) {
this(tiny, null, out);
}
protected JsonStreamWriter(boolean tiny, Charset charset, OutputStream out) {
super(tiny, charset, null);
this.out = out;
}
@Override
protected boolean recycle() {
super.recycle();
this.out = null;
return false;
}
@Override
public void writeTo(final char ch) {
if (ch > Byte.MAX_VALUE) throw new ConvertException("writeTo char(int.value = " + (int) ch + ") must be less 127");
try {
out.write((byte) ch);
} catch (IOException e) {
throw new ConvertException(e);
}
}
@Override
public void writeTo(final char[] chs, final int start, final int len) {
writeTo(false, chs, start, len);
}
private void writeTo(final boolean quote, final char[] chs, final int start, final int len) {
try {
if (quote) out.write('"');
if (charset == null) { //UTF-8
final int limit = start + len;
for (int i = start; i < limit; i++) {
char c = chs[i];
if (c < 0x80) {
out.write((byte) c);
} else if (c < 0x800) {
out.write((byte) (0xc0 | (c >> 6)));
out.write((byte) (0x80 | (c & 0x3f)));
} else {
out.write((byte) (0xe0 | ((c >> 12))));
out.write((byte) (0x80 | ((c >> 6) & 0x3f)));
out.write((byte) (0x80 | (c & 0x3f)));
}
}
} else {
ByteBuffer bb = charset.encode(CharBuffer.wrap(chs, start, len));
out.write(bb.array());
}
if (quote) out.write('"');
} catch (IOException e) {
throw new ConvertException(e);
}
}
/**
* <b>注意:</b> 该String值不能为null且不会进行转义 只用于不含需要转义字符的字符串例如enum、double、BigInteger转换的String
*
* @param quote 是否写入双引号
* @param value String值
*/
@Override
public void writeTo(final boolean quote, final String value) {
char[] chs = Utility.charArray(value);
writeTo(quote, chs, 0, chs.length);
}
@Override
public void writeString(String value) {
if (value == null) {
writeNull();
return;
}
final char[] chs = Utility.charArray(value);
int len = 0;
for (char ch : chs) {
switch (ch) {
case '\n': len += 2;
break;
case '\r': len += 2;
break;
case '\t': len += 2;
break;
case '\\': len += 2;
break;
case '"': len += 2;
break;
default: len++;
break;
}
}
if (len == chs.length) {
writeTo(true, chs, 0, len);
return;
}
StringBuilder sb = new StringBuilder(len);
for (char ch : chs) {
switch (ch) {
case '\n': sb.append("\\n");
break;
case '\r': sb.append("\\r");
break;
case '\t': sb.append("\\t");
break;
case '\\': sb.append("\\\\");
break;
case '"': sb.append("\\\"");
break;
default: sb.append(ch);
break;
}
}
char[] cs = Utility.charArray(sb);
writeTo(true, cs, 0, sb.length());
}
}

View File

@@ -58,11 +58,11 @@ public class JsonWriter implements Writer {
} }
@Override @Override
public boolean isTiny() { public boolean tiny() {
return tiny; return tiny;
} }
public JsonWriter setTiny(boolean tiny) { public JsonWriter tiny(boolean tiny) {
this.tiny = tiny; this.tiny = tiny;
return this; return this;
} }
@@ -159,14 +159,14 @@ public class JsonWriter implements Writer {
} }
@Override @Override
public void writeField(boolean comma, Attribute attribute) { public final void writeField(boolean comma, Attribute attribute) {
if (comma) writeTo(','); if (comma) writeTo(',');
writeTo(true, attribute.field()); writeTo(true, attribute.field());
writeTo(':'); writeTo(':');
} }
@Override @Override
public void writeSmallString(String value) { public final void writeSmallString(String value) {
writeTo(true, value); writeTo(true, value);
} }

View File

@@ -189,17 +189,17 @@ public class HttpResponse<R extends HttpRequest> extends Response<R> {
public void finishJson(Object obj) { public void finishJson(Object obj) {
this.contentType = "text/plain; charset=utf-8"; this.contentType = "text/plain; charset=utf-8";
finish(request.getJsonConvert().convertTo(context.getCharset(), context.getBufferSupplier(), obj)); finish(request.getJsonConvert().convertTo(context.getBufferSupplier(), obj));
} }
public void finishJson(Type type, Object obj) { public void finishJson(Type type, Object obj) {
this.contentType = "text/plain; charset=utf-8"; this.contentType = "text/plain; charset=utf-8";
finish(request.getJsonConvert().convertTo(context.getCharset(), context.getBufferSupplier(), type, obj)); finish(request.getJsonConvert().convertTo(context.getBufferSupplier(), type, obj));
} }
public void finishJson(Object... objs) { public void finishJson(Object... objs) {
this.contentType = "text/plain; charset=utf-8"; this.contentType = "text/plain; charset=utf-8";
finish(request.getJsonConvert().convertTo(context.getCharset(), context.getBufferSupplier(), objs)); finish(request.getJsonConvert().convertTo(context.getBufferSupplier(), objs));
} }
public void finish(String obj) { public void finish(String obj) {