Mercurial Hosting > luan
view src/org/eclipse/jetty/io/JBuffer.java @ 1047:1accf965d51a
remove other Buffer implementations
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 07 Nov 2016 22:51:09 -0700 |
parents | a8c92b0a08ed |
children | 2b769da7f67d |
line wrap: on
line source
// tmp class to implement Buffer until I can get rid of it package org.eclipse.jetty.io; import java.io.InputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.eclipse.jetty.io.nio.NIOBuffer; import org.eclipse.jetty.util.TypeUtil; public final class JBuffer implements NIOBuffer { private static final Logger LOG = LoggerFactory.getLogger(JBuffer.class); private final ByteBuffer bb; public JBuffer(ByteBuffer bb) { this.bb = bb; } @Override public byte[] array() { return bb.hasArray() ? bb.array() : null; } @Override public Buffer duplicate() { return new JBuffer(bb.duplicate()); } @Override public int remaining() { return bb.remaining(); } @Override public boolean isReadOnly() { return bb.isReadOnly(); } @Override public boolean hasRemaining() { return bb.hasRemaining(); } @Override public byte get() { return bb.get(); } @Override public void compact() { int n = bb.remaining(); bb.compact(); bb.position(0); bb.limit(n); } @Override public int capacity() { return bb.capacity(); } @Override public boolean isDirect() { return bb.isDirect(); } @Override public ByteBuffer getByteBuffer() { ByteBuffer dup = bb.duplicate(); dup.limit(dup.capacity()); return dup; } @Override public int getIndex() { return bb.position(); } @Override public void clear() { bb.position(0); bb.limit(0); } @Override public int space() { return bb.capacity() - bb.limit(); } @Override public Buffer buffer() { return this; } @Override public Buffer get(int length) { ByteBuffer dup = bb.duplicate(); int end = bb.position()+length; dup.limit(end); bb.position(end); return new JBuffer(dup); } @Override public int get(byte[] b, int offset, int length) { int remaining = bb.remaining(); if( remaining == 0 ) return -1; if( length > remaining ) length = remaining; bb.get(b,offset,length); return length; } @Override public int put(Buffer src) { return put(src.asArray()); } @Override public void put(byte b) { ByteBuffer dup = bb.duplicate(); dup.position(bb.limit()); dup.limit(bb.capacity()); dup.put(b); bb.limit(bb.limit()+1); } @Override public int put(byte[] b, int offset, int length) { ByteBuffer dup = bb.duplicate(); int put = bb.limit(); int capacity = bb.capacity(); dup.position(put); dup.limit(capacity); if( length > capacity - put ) length = capacity - put; dup.put(b,offset,length); bb.limit(put+length); return length; } @Override public int put(byte[] b) { return put(b,0,b.length); } @Override public final int putIndex() { return bb.limit(); } @Override public void setGetIndex(int getIndex) { bb.position(getIndex); } @Override public void setPutIndex(int putIndex) { bb.limit(putIndex); } @Override public int skip(int n) { if (remaining() < n) n = remaining(); bb.position(bb.position() + n); return n; } @Override public Buffer slice() { return duplicate(); } @Override public final Buffer sliceFrom(int index) { ByteBuffer dup = bb.duplicate(); dup.position(index); dup.limit(bb.position()-1); return new JBuffer(dup); } @Override public int readFrom(InputStream in,int max) throws IOException { ByteBuffer dup = bb.duplicate(); int put = bb.limit(); dup.limit( Math.min(put+max,bb.capacity()) ); dup.position(put); ReadableByteChannel chan = Channels.newChannel(in); int n = chan.read(dup); if( n > 0 ) bb.limit(put+n); return n; } public final byte[] asArray() { byte[] bytes = new byte[remaining()]; bb.duplicate().get(bytes); return bytes; } @Override public String toString() { return toString("ISO-8859-1"); } @Override public final String toString(int index, int length) { ByteBuffer dup = bb.duplicate(); dup.limit(index+length); dup.position(index); return new JBuffer(dup).toString(); } @Override public final String toString(String charset) { byte[] bytes = asArray(); try { return new String(bytes,charset); } catch(Exception e) { LOG.warn("",e); return new String(bytes); } } @Override public String toDetailString() { StringBuilder buf = new StringBuilder(); buf.append("["); buf.append(super.hashCode()); buf.append(","); buf.append(this.buffer().hashCode()); buf.append(",g="); buf.append(getIndex()); buf.append(",p="); buf.append(putIndex()); buf.append(",c="); buf.append(capacity()); buf.append("]={"); int count = 0; for (int i = getIndex(); i < putIndex(); i++) { byte b = peek(i); TypeUtil.toHex(b,buf); if (count++ == 50) { if (putIndex() - i > 20) { buf.append(" ... "); i = putIndex() - 20; } } } buf.append('}'); return buf.toString(); } private Buffer pokeBuffer(int index) { Buffer dup = duplicate(); dup.setPutIndex(index); return dup; } @Override public int poke(int index, byte b[], int offset, int length) { return pokeBuffer(index).put(b,offset,length); } @Override public void poke(int index, byte b) { pokeBuffer(index).put(b); } @Override public int poke(int index, Buffer src) { return pokeBuffer(index).put(src); } private Buffer peekBuffer(int index) { Buffer dup = duplicate(); dup.setGetIndex(index); dup.setPutIndex(dup.capacity()); return dup; } @Override public int peek(int index, byte[] b, int offset, int length) { return peekBuffer(index).get(b,offset,length); } @Override public byte peek(int index) { return bb.get(index); } @Override public byte peek() { return peek(bb.position()); } }