view src/org/eclipse/jetty/io/View.java @ 1036:b87f97f6418a

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 03 Nov 2016 21:42:41 -0600
parents eca26899c4bc
children a7319f14ba1e
line wrap: on
line source

//
//  ========================================================================
//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.io;

/**
 * A View on another buffer.  Allows operations that do not change the _content or
 * indexes of the backing buffer.
 * 
 * 
 * 
 */
public final class View extends AbstractBuffer
{
	private Buffer _buffer;

	/**
	 * @param buffer The <code>Buffer</code> on which we are presenting a <code>View</code>.
	 * @param mark The initial value of the {@link Buffer#markIndex mark index}
	 * @param get The initial value of the {@link Buffer#getIndex get index}
	 * @param put The initial value of the {@link Buffer#putIndex put index}
	 * @param access The access level - one of the constants from {@link Buffer}.
	 */
	public View(Buffer buffer, int mark, int get, int put,int access)
	{
		super(READWRITE);
		_buffer=buffer.buffer();
		setPutIndex(put);
		setGetIndex(get);
		setMarkIndex(mark);
		_access=access;
	}
	
	public View()
	{
		super(READWRITE);
	}
	
	/**
	 * Update view to buffer
	 */
	public void update(Buffer buffer)
	{
		_access = READWRITE;
		_buffer = buffer.buffer();
		setGetIndex(0);
		setPutIndex(buffer.putIndex());
		setGetIndex(buffer.getIndex());
		setMarkIndex(buffer.markIndex());
		_access = buffer.isReadOnly()?READONLY:READWRITE;
	}

	@Override
	public byte[] array()
	{
		return _buffer.array();
	}

	@Override
	public Buffer buffer()
	{
		return _buffer.buffer();
	}

	@Override
	public int capacity()
	{
		return _buffer.capacity();
	}

	@Override
	public void clear()
	{
		setMarkIndex(-1);
		setGetIndex(0);
		setPutIndex(_buffer.getIndex());
		setGetIndex(_buffer.getIndex());
	}

	@Override
	public void compact()
	{
		// TODO
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj)
	{
		return  this==obj ||((obj instanceof Buffer)&& obj.equals(this)) || super.equals(obj);
	}

	@Override
	public boolean isReadOnly()
	{
		return _buffer.isReadOnly();
	}

	@Override
	public byte peek(int index)
	{
		return _buffer.peek(index);
	}

	@Override
	public int peek(int index, byte[] b, int offset, int length)
	{
		return _buffer.peek(index,b,offset,length);
	}

	@Override
	public int poke(int index, Buffer src)
	{
		return _buffer.poke(index,src); 
	}

	@Override
	public void poke(int index, byte b)
	{
		_buffer.poke(index,b);
	}

	@Override
	public int poke(int index, byte[] b, int offset, int length)
	{
		return _buffer.poke(index,b,offset,length);
	}
	
	@Override
	public String toString()
	{
		if (_buffer==null)
			return "INVALID";
		return super.toString();
	}
}