001 /** 002 * 003 * Copyright 2004 Protique Ltd 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 **/ 018 019 package org.activemq.io.util; 020 021 /** 022 * Simple holder for a an array of Bytes - used instead of a ByteBuffer to avoid unecessary System.array() copies 023 * 024 * @version $Revision: 1.1.1.1 $ 025 */ 026 public class ByteArray { 027 private byte[] buf; 028 private int offset; 029 private int length; 030 031 /** 032 * Construct an empty ByteArray 033 */ 034 public ByteArray() { 035 } 036 037 /** 038 * Create a byte array 039 * 040 * @param buf 041 */ 042 public ByteArray(byte[] buf) { 043 this(buf, 0, buf.length); 044 } 045 046 /** 047 * Create a ByteArray 048 * 049 * @param buf 050 * @param offset 051 * @param length 052 */ 053 public ByteArray(byte[] buf, int offset, int length) { 054 this.buf = buf; 055 this.offset = offset; 056 this.length = length; 057 } 058 059 /** 060 * clear the values held by this ByteArray 061 */ 062 public void clear() { 063 buf = null; 064 offset = 0; 065 length = 0; 066 } 067 068 /** 069 * reset values 070 * 071 * @param buf 072 */ 073 public void reset(byte[] buf) { 074 if (buf != null) { 075 reset(buf, 0, buf.length); 076 } 077 else { 078 clear(); 079 } 080 } 081 082 /** 083 * reset values 084 * 085 * @param buf 086 * @param offset 087 * @param length 088 */ 089 public void reset(byte[] buf, int offset, int length) { 090 this.buf = buf; 091 this.offset = offset; 092 this.length = length; 093 } 094 095 /** 096 * @return Returns the buf. 097 */ 098 public byte[] getBuf() { 099 return buf; 100 } 101 102 /** 103 * @param buf The buf to set. 104 */ 105 public void setBuf(byte[] buf) { 106 this.buf = buf; 107 } 108 109 /** 110 * @return Returns the length. 111 */ 112 public int getLength() { 113 return length; 114 } 115 116 /** 117 * @param length The length to set. 118 */ 119 public void setLength(int length) { 120 this.length = length; 121 } 122 123 /** 124 * @return Returns the offset. 125 */ 126 public int getOffset() { 127 return offset; 128 } 129 130 /** 131 * @param offset The offset to set. 132 */ 133 public void setOffset(int offset) { 134 this.offset = offset; 135 } 136 137 /** 138 * return the byte at the position 139 * @param position 140 * @return 141 */ 142 public byte get(int position){ 143 return buf[offset + position]; 144 } 145 146 /** 147 * make a copy 148 * 149 * @return a copy of it's self 150 */ 151 public ByteArray copy() { 152 ByteArray result = new ByteArray(); 153 if (buf != null) { 154 byte[] data = new byte[length]; 155 System.arraycopy(buf, offset, data, 0, length); 156 result.reset(data); 157 } 158 return result; 159 } 160 }