view src/org/eclipse/jetty/io/JBuffer.java @ 1064:a0abb16cf6e7

remove JBuffer.readFrom()
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 09 Nov 2016 03:09:37 -0700
parents 0157e92670f5
children 158d1e6ac17f
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.eclipse.jetty.util.TypeUtil;


public final class JBuffer {
	private static final Logger LOG = LoggerFactory.getLogger(JBuffer.class);

	private final ByteBuffer bb;

	public JBuffer(ByteBuffer bb) {
		this.bb = bb;
	}

	public int position() {
		return bb.position();
	}

	public void position(int i) {
		bb.position(i);
	}

	public int limit() {
		return bb.limit();
	}

	public void limit(int i) {
		bb.limit(i);
	}

	public byte[] array() {
		return bb.array();
	}

	public boolean hasArray() {
		return bb.hasArray();
	}

	public JBuffer duplicate() {
		return new JBuffer(bb.duplicate());
	}

	public int remaining() {
		return bb.remaining();
	}

	public boolean isReadOnly() {
		return bb.isReadOnly();
	}

	public boolean hasRemaining() {
		return bb.hasRemaining();
	}

	public byte get() {
		return bb.get();
	}

	public void get(byte[] bytes) {
		bb.get(bytes);
	}

	public void compact() {
		int n = bb.remaining();
		bb.compact();
		bb.position(0);
		bb.limit(n);
	}

	public int capacity() {
		return bb.capacity();
	}



	public ByteBuffer getByteBuffer() {
		return bb;
	}

	public void clear() {
		bb.position(0);
		bb.limit(0);
	}

	public int space() {
		return bb.capacity() - bb.limit();
	}


	public void get(byte[] b, int offset, int length) {
		bb.get(b,offset,length);
	}


	public int put(JBuffer src) {
		return put(src.asArray());
	}

	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);
	}

	private 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;
	}

	public int put(byte[] b) {
		return put(b,0,b.length);
	}

	public final int putIndex() {
		return bb.limit();
	}

	public void setPutIndex(int putIndex) {
		bb.limit(putIndex);
	}

	public void skip(int n) {
		if (remaining() < n) n = remaining();
		bb.position(bb.position() + n);
	}

	private final byte[] asArray() {
		byte[] bytes = new byte[remaining()];
		bb.duplicate().get(bytes);
		return bytes;
	}
/*
	@Override
	public String toString() {
//		return toString("ISO-8859-1");
//		Thread.dumpStack();
		throw new RuntimeException("toString");
	}
*/

	public byte get(int index) {
		return bb.get(index);
	}

}