1 /* 2 * $Header: /u/cvs/Projects/EnhydraOrg/enhydra3x/Enhydra/modules/Tomcat/src/share/org/apache/tomcat/util/MessageBytes.java,v 1.2.2.1 2000/03/24 00:54:44 peterj Exp $ 3 * $Revision: 1.2.2.1 $ 4 * $Date: 2000/03/24 00:54:44 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developsd by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>. 59 * 60 * [Additional notices, if required by prior licensing conditions] 61 * 62 */ 63 64 65 package org.apache.tomcat.util; 66 67 import java.io.OutputStream; 68 import java.io.IOException; 69 70 /** 71 * This class is used to represent a subarray of bytes in an HTTP message. 72 * 73 * @author dac@eng.sun.com 74 * @author James Todd [gonzo@eng.sun.com] 75 */ 76 77 public class MessageBytes extends Ascii { 78 79 private StringManager sm = null; 80 // StringManager.getManager(Constants.Package); 81 82 private StringManager getSM() { 83 if (sm == null) { 84 sm = StringManager.getManager(Constants.Package); 85 } 86 return sm; 87 } 88 89 /** 90 * The message bytes. 91 */ 92 protected byte[] bytes; 93 94 /** 95 * The start offset of the bytes. 96 */ 97 protected int offset; 98 99 /** 100 * The length of the bytes. 101 */ 102 protected int length; 103 104 /** 105 * Creates a new, uninitialized MessageBytes object. 106 */ 107 public MessageBytes() { 108 } 109 110 /** 111 * Creates a new MessageBytes object with the specified bytes. 112 * @param b the bytes 113 * @param off the start offset of the bytes 114 * @param len the length of the bytes 115 */ 116 public MessageBytes(byte[] b, int off, int len) { 117 setBytes(b, off, len); 118 } 119 120 /** 121 * Resets the message bytes to an uninitialized state. 122 */ 123 public void reset() { 124 bytes = null; 125 } 126 127 /** 128 * Sets the message bytes to the specified subarray of bytes. 129 * @param b the ascii bytes 130 * @param off the start offset of the bytes 131 * @param len the length of the bytes 132 */ 133 public void setBytes(byte[] b, int off, int len) { 134 bytes = b; 135 offset = off; 136 length = len; 137 } 138 139 /** 140 * Returns the message bytes. 141 */ 142 public byte[] getBytes() { 143 return bytes; 144 } 145 146 /** 147 * Puts the message bytes in buf starting at buf_offset. 148 * @return the number of bytes added to buf. 149 */ 150 public int getBytes(byte buf[], 151 int buf_offset) 152 { 153 if (bytes != null) 154 System.arraycopy(bytes, offset, buf, buf_offset, length); 155 return length; 156 } 157 158 /** 159 * Returns the start offset of the bytes. 160 */ 161 public int getOffset() { 162 return offset; 163 } 164 165 /** 166 * Returns the length of the bytes. 167 */ 168 public int getLength() { 169 return length; 170 } 171 172 /** 173 * Returns true if the message bytes have been set. 174 */ 175 public boolean isSet() { 176 return bytes != null; 177 } 178 179 /** 180 * Returns the message bytes as a String object. 181 */ 182 public String toString() { 183 return bytes != null ? new String(bytes, offset, length) : null; 184 } 185 186 /** 187 * Returns the message bytes parsed as an unsigned integer. 188 * @exception NumberFormatException if the integer format was invalid 189 */ 190 public int toInteger() throws NumberFormatException { 191 return parseInt(bytes, offset, length); 192 } 193 194 /** 195 * Returns the message bytes parsed as a date. 196 * @param d the HttpDate object to use for parsing 197 * @exception IllegalArgumentException if the date format was invalid 198 */ 199 public long toDate(HttpDate d) throws IllegalArgumentException { 200 if (bytes != null) { 201 d.parse(bytes, offset, length); 202 return d.getTime(); 203 } else { 204 String msg = getSM().getString("messageBytes.iae", bytes); 205 206 throw new IllegalArgumentException(msg); 207 } 208 } 209 210 /** 211 * Compares the message bytes to the specified String object. 212 * @param s the String to compare 213 * @return true if the comparison succeeded, false otherwise 214 */ 215 public boolean equals(String s) { 216 byte[] b = bytes; 217 int len = length; 218 if (b == null || len != s.length()) { 219 return false; 220 } 221 int off = offset; 222 for (int i = 0; i < len; i++) { 223 if (b[off++] != s.charAt(i)) { 224 return false; 225 } 226 } 227 return true; 228 } 229 230 /** 231 * Compares the message bytes to the specified String object. Case is 232 * ignored in the comparison. 233 * @param s the String to compare 234 * @return true if the comparison succeeded, false otherwise 235 */ 236 public boolean equalsIgnoreCase(String s) { 237 byte[] b = bytes; 238 int len = length; 239 if (b == null || len != s.length()) { 240 return false; 241 } 242 int off = offset; 243 for (int i = 0; i < len; i++) { 244 if (toLower(b[off++]) != toLower((byte)s.charAt(i))) { 245 return false; 246 } 247 } 248 return true; 249 } 250 251 /** 252 * Compares the message bytes to the specified subarray of bytes. 253 * @param b the bytes to compare 254 * @param off the start offset of the bytes 255 * @param len the length of the bytes 256 * @return true if the comparison succeeded, false otherwise 257 */ 258 public boolean equals(byte[] b, int off, int len) { 259 byte[] b1 = bytes; 260 if (b1 == null || len != length) { 261 return false; 262 } 263 int off1 = offset; 264 while (len-- > 0) { 265 if (b[off++] != b1[off1++]) { 266 return false; 267 } 268 } 269 return true; 270 } 271 272 /** 273 * Compares the message bytes to the specified subarray of bytes. 274 * Case is ignored in the comparison. 275 * @param b the bytes to compare 276 * @param off the start offset of the bytes 277 * @param len the length of the bytes 278 * @return true if the comparison succeeded, false otherwise 279 */ 280 public boolean equalsIgnoreCase(byte[] b, int off, int len) { 281 byte[] b1 = bytes; 282 if (b1 == null || len != length) { 283 return false; 284 } 285 int off1 = offset; 286 while (len-- > 0) { 287 if (toLower(b[off++]) != toLower(b1[off1++])) { 288 return false; 289 } 290 } 291 return true; 292 } 293 294 /** 295 * Returns true if the message bytes starts with the specified string. 296 * @param s the string 297 */ 298 public boolean startsWith(String s) { 299 byte[] b = bytes; 300 int len = s.length(); 301 if (b == null || len > length) { 302 return false; 303 } 304 int off = offset; 305 for (int i = 0; i < len; i++) { 306 if (b[off++] != s.charAt(i)) { 307 return false; 308 } 309 } 310 return true; 311 } 312 313 /** 314 * Writes the message bytes to the specified output stream. 315 * @param out the output stream 316 * @exception IOException if an I/O error has occurred 317 */ 318 public void write(OutputStream out) throws IOException { 319 if (bytes != null) { 320 out.write(bytes, offset, length); 321 } 322 } 323 324 /** 325 * Returns the length of the message bytes. 326 */ 327 public int length() { 328 return bytes != null ? length : 0; 329 } 330 }