more optimizations on write side

This commit is contained in:
Alex Tkachman
2010-09-12 17:05:58 +02:00
parent aed824c94c
commit f9e7887e02
5 changed files with 186 additions and 109 deletions

View File

@@ -0,0 +1,106 @@
/*
* Copyright 2009-2010 MBTE Sweden AB.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package redis.clients.util;
import redis.clients.jedis.JedisException;
import java.io.*;
public class RedisInputStream extends FilterInputStream {
protected final byte buf[];
protected int count, limit;
public RedisInputStream(InputStream in, int size) {
super(in);
if (size <= 0) {
throw new IllegalArgumentException("Buffer size <= 0");
}
buf = new byte[size];
}
public RedisInputStream(InputStream in) {
this(in, 8192);
}
public byte readByte () throws IOException {
if(count == limit) {
fill ();
}
return buf[count++];
}
public String readLine() {
int b;
byte c;
StringBuilder sb = new StringBuilder();
try {
while (true) {
if(count == limit) {
fill ();
}
if(limit == -1)
break;
b = buf[count++];
if (b == '\r') {
if(count == limit) {
fill ();
}
if(limit == -1) {
sb.append((char) b);
break;
}
c = buf[count++];
if (c == '\n') {
break;
}
sb.append((char) b);
sb.append((char) c);
} else {
sb.append((char) b);
}
}
} catch (IOException e) {
throw new JedisException(e);
}
return sb.toString();
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
if(count == limit) {
fill ();
if(limit == -1)
return -1;
}
final int length = Math.min(limit - count, len);
System.arraycopy(buf, count, b, off, length);
count += length;
return length;
}
private void fill () throws IOException {
limit = in.read(buf);
count = 0;
}
}

View File

@@ -1,21 +1,15 @@
package redis.clients.util;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
/**
* The class implements a buffered output stream without synchronization
* There are also special operations like in-place string encoding
* There are also special operations like in-place string encoding.
* This stream fully ignore mark/reset and should not be used outside Jedis
*/
public final class RedisOutputStream extends FilterOutputStream {
protected final byte buf[];
protected final ByteBuffer outByteBuffer;
private final CharsetEncoder CHARSET_ENCODER = CHARSET.newEncoder();
protected int count;
public static final Charset CHARSET = Charset.forName("UTF-8");
@@ -30,13 +24,11 @@ public final class RedisOutputStream extends FilterOutputStream {
throw new IllegalArgumentException("Buffer size <= 0");
}
buf = new byte[size];
outByteBuffer = ByteBuffer.wrap(buf);
}
private void flushBuffer() throws IOException {
if (count > 0) {
out.write(buf, 0, count);
outByteBuffer.position(0);
count = 0;
}
}
@@ -233,4 +225,4 @@ public final class RedisOutputStream extends FilterOutputStream {
flushBuffer();
out.flush();
}
}
}