001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.mina.util.byteaccess; 021 022import org.apache.mina.core.buffer.IoBuffer; 023 024/** 025 * Provides restricted, relative, read-only access to the bytes in a 026 * <code>CompositeByteArray</code>. Using this interface has the advantage 027 * that it can be automatically determined when a component 028 * <code>ByteArray</code> can no longer be read, and thus components can be 029 * automatically freed. This makes it easier to use pooling for underlying 030 * <code>ByteArray</code>s. 031 * 032 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 033 */ 034public class CompositeByteArrayRelativeReader extends CompositeByteArrayRelativeBase implements IoRelativeReader { 035 036 /** 037 * Whether or not to free component <code>CompositeByteArray</code>s when 038 * the cursor moves past them. 039 */ 040 private final boolean autoFree; 041 042 /** 043 * 044 * Creates a new instance of CompositeByteArrayRelativeReader. 045 * 046 * @param cba 047 * The backing ByteArray 048 * @param autoFree 049 * If data should be freed once it has been passed in the list 050 */ 051 public CompositeByteArrayRelativeReader(CompositeByteArray cba, boolean autoFree) { 052 super(cba); 053 this.autoFree = autoFree; 054 } 055 056 @Override 057 protected void cursorPassedFirstComponent() { 058 if (autoFree) { 059 cba.removeFirst().free(); 060 } 061 } 062 063 /** 064 * {@inheritDoc} 065 */ 066 public void skip(int length) { 067 cursor.skip(length); 068 } 069 070 /** 071 * {@inheritDoc} 072 */ 073 public ByteArray slice(int length) { 074 return cursor.slice(length); 075 } 076 077 /** 078 * @return the byte at the current position in the buffer 079 * 080 */ 081 public byte get() { 082 return cursor.get(); 083 } 084 085 /** 086 * places the data starting at current position into the 087 * supplied {@link IoBuffer} 088 */ 089 public void get(IoBuffer bb) { 090 cursor.get(bb); 091 } 092 093 /** 094 * {@inheritDoc} 095 */ 096 public short getShort() { 097 return cursor.getShort(); 098 } 099 100 /** 101 * {@inheritDoc} 102 */ 103 public int getInt() { 104 return cursor.getInt(); 105 } 106 107 /** 108 * {@inheritDoc} 109 */ 110 public long getLong() { 111 return cursor.getLong(); 112 } 113 114 /** 115 * {@inheritDoc} 116 */ 117 public float getFloat() { 118 return cursor.getFloat(); 119 } 120 121 /** 122 * {@inheritDoc} 123 */ 124 public double getDouble() { 125 return cursor.getDouble(); 126 } 127 128 /** 129 * {@inheritDoc} 130 */ 131 public char getChar() { 132 return cursor.getChar(); 133 } 134 135}