1 /* 2 * $Header: /u/cvs/Projects/EnhydraOrg/enhydra3x/Enhydra/modules/Tomcat/src/share/org/apache/tomcat/util/MessageString.java,v 1.2 2000/02/26 02:32:29 shawn Exp $ 3 * $Revision: 1.2 $ 4 * $Date: 2000/02/26 02:32:29 $ 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 developed 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 import javax.servlet.ServletOutputStream; 71 72 /** 73 * This class is used to represent a string in an HTTP message, 74 * and can either be an actual String or a subarray of bytes. 75 * 76 * @author dac@eng.sun.com 77 */ 78 79 public class MessageString extends MessageBytes { 80 /** 81 * The message String. 82 */ 83 protected String str; 84 85 /** 86 * Creates a new, uninitialized message string. 87 */ 88 public MessageString() { 89 } 90 91 /** 92 * Creates a new message string with the specified String object. 93 * @param s the String 94 */ 95 public MessageString(String s) { 96 str = s; 97 } 98 99 /** 100 * Creates a new message string with the specified bytes. 101 * @param b the bytes 102 * @param off the offset of the bytes 103 * @param len the length of the bytes 104 */ 105 public MessageString(byte[] b, int off, int len) { 106 super(b, off, len); 107 } 108 109 /** 110 * Resets the message string to an uninitialized state. 111 */ 112 public void reset() { 113 super.reset(); 114 str = null; 115 } 116 117 /** 118 * Sets the message string to the specified String. 119 * @param s the String 120 */ 121 public void setString(String s) { 122 super.reset(); 123 str = s; 124 } 125 126 /** 127 * Sets the message string to the specified bytes. 128 * @param b the bytes 129 * @param off the offset of the bytes 130 * @param len the length of the bytes 131 */ 132 public void setBytes(byte[] b, int off, int len) { 133 super.setBytes(b, off, len); 134 str = null; 135 } 136 137 /** 138 * Returns true if the message string is set. 139 */ 140 public boolean isSet() { 141 return str != null || super.isSet(); 142 } 143 144 /** 145 * Get the bytes of this message string in buf starting at buf_offset. 146 * @return the number of bytes added to buf. 147 */ 148 public int getBytes(byte buf[], 149 int buf_offset) 150 { 151 if (str != null) { 152 int len = str.length(); 153 // deprecated line (kept for reference) 154 //str.getBytes(0, len, buf, buf_offset); 155 byte[] strBytes = str.getBytes(); 156 System.arraycopy(strBytes, 0, buf, buf_offset, len); 157 return len; 158 } 159 else { 160 return super.getBytes(buf, buf_offset); 161 } 162 } 163 164 /** 165 * Returns the message string as a String object. 166 */ 167 public String toString() { 168 return str != null ? str : super.toString(); 169 } 170 171 /** 172 * Returns the message string parsed as an unsigned integer. 173 * @exception NumberFormatException if the integer format was invalid 174 */ 175 public int toInteger() throws NumberFormatException { 176 return str != null ? Integer.parseInt(str) : super.toInteger(); 177 } 178 179 /** 180 * Returns the message string parsed as a date. 181 * @param d the HttpDate object to use for parsing 182 * @exception IllegalArgumentException if the date format was invalid 183 */ 184 public long toDate(HttpDate d) throws IllegalArgumentException { 185 if (str != null) { 186 d.parse(str); 187 return d.getTime(); 188 } else { 189 return super.toDate(d); 190 } 191 } 192 193 /** 194 * Compares this message string to the specified String object. 195 * @param s the String to compare 196 * @return true if the comparison succeeded, false otherwise 197 */ 198 public boolean equals(String s) { 199 return str != null ? str.equals(s) : super.equals(s); 200 } 201 202 /** 203 * Compares this message string to the specified String object. Case is 204 * ignored in the comparison. 205 * @param s the String to compare 206 * @return true if the comparison succeeded, false otherwise 207 */ 208 public boolean equalsIgnoreCase(String s) { 209 return str != null ? str.equalsIgnoreCase(s) : super.equalsIgnoreCase(s); 210 } 211 212 /** 213 * Compares this message string to the specified subarray of bytes. 214 * @param b the bytes to compare 215 * @param off the start offset of the bytes 216 * @param len the length of the bytes 217 * @return true if the comparison succeeded, false otherwise 218 */ 219 public boolean equals(byte[] b, int off, int len) { 220 if (str != null) { 221 String s = str; 222 if (len != s.length()) { 223 return false; 224 } 225 for (int i = 0; i < len; i++) { 226 if (b[off++] != s.charAt(i)) { 227 return false; 228 } 229 } 230 return true; 231 } 232 return super.equals(b, off, len); 233 } 234 235 /** 236 * Compares this message string to the specified subarray of bytes. 237 * Case is ignored in the comparison. 238 * @param b the bytes to compare 239 * @param off the start offset of the bytes 240 * @param len the length of the bytes 241 * @return true if the comparison succeeded, false otherwise 242 */ 243 public boolean equalsIgnoreCase(byte[] b, int off, int len) { 244 if (str != null) { 245 String s = str; 246 if (len != s.length()) { 247 return false; 248 } 249 for (int i = 0; i < len; i++) { 250 if (toLower(b[off++]) != toLower(s.charAt(i))) { 251 return false; 252 } 253 } 254 return true; 255 } 256 return super.equalsIgnoreCase(b, off, len); 257 } 258 259 /** 260 * Returns true if the message string starts with the specified string. 261 * @param s the string 262 */ 263 public boolean startsWith(String s) { 264 return str != null ? str.startsWith(s) : super.startsWith(s); 265 } 266 267 /** 268 * Writes the message string to the specified servlet output stream. 269 * @param out the servlet output stream 270 * @exception IOException if an I/O error has occurred 271 */ 272 public void write(ServletOutputStream out) throws IOException { 273 if (str != null) { 274 out.print(str); 275 } else { 276 super.write(out); 277 } 278 } 279 280 /** 281 * Returns the length of the message string. 282 */ 283 public int length() { 284 return str != null ? str.length() : super.length(); 285 } 286 }