diff options
Diffstat (limited to 'src/org/apache/http/util')
-rw-r--r-- | src/org/apache/http/util/ByteArrayBuffer.java | 162 | ||||
-rw-r--r-- | src/org/apache/http/util/CharArrayBuffer.java | 264 | ||||
-rw-r--r-- | src/org/apache/http/util/EncodingUtils.java | 185 | ||||
-rw-r--r-- | src/org/apache/http/util/EntityUtils.java | 149 | ||||
-rw-r--r-- | src/org/apache/http/util/ExceptionUtils.java | 85 | ||||
-rw-r--r-- | src/org/apache/http/util/LangUtils.java | 88 | ||||
-rw-r--r-- | src/org/apache/http/util/VersionInfo.java | 317 | ||||
-rw-r--r-- | src/org/apache/http/util/package.html | 45 |
8 files changed, 1295 insertions, 0 deletions
diff --git a/src/org/apache/http/util/ByteArrayBuffer.java b/src/org/apache/http/util/ByteArrayBuffer.java new file mode 100644 index 0000000..01d6577 --- /dev/null +++ b/src/org/apache/http/util/ByteArrayBuffer.java @@ -0,0 +1,162 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/ByteArrayBuffer.java $ + * $Revision: 496070 $ + * $Date: 2007-01-14 04:18:34 -0800 (Sun, 14 Jan 2007) $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package org.apache.http.util; + +/** + * A resizable byte array. + * + * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 496070 $ + * + * @since 4.0 + */ +public final class ByteArrayBuffer { + + private byte[] buffer; + private int len; + + public ByteArrayBuffer(int capacity) { + super(); + if (capacity < 0) { + throw new IllegalArgumentException("Buffer capacity may not be negative"); + } + this.buffer = new byte[capacity]; + } + + private void expand(int newlen) { + byte newbuffer[] = new byte[Math.max(this.buffer.length << 1, newlen)]; + System.arraycopy(this.buffer, 0, newbuffer, 0, this.len); + this.buffer = newbuffer; + } + + public void append(final byte[] b, int off, int len) { + if (b == null) { + return; + } + if ((off < 0) || (off > b.length) || (len < 0) || + ((off + len) < 0) || ((off + len) > b.length)) { + throw new IndexOutOfBoundsException(); + } + if (len == 0) { + return; + } + int newlen = this.len + len; + if (newlen > this.buffer.length) { + expand(newlen); + } + System.arraycopy(b, off, this.buffer, this.len, len); + this.len = newlen; + } + + public void append(int b) { + int newlen = this.len + 1; + if (newlen > this.buffer.length) { + expand(newlen); + } + this.buffer[this.len] = (byte)b; + this.len = newlen; + } + + public void append(final char[] b, int off, int len) { + if (b == null) { + return; + } + if ((off < 0) || (off > b.length) || (len < 0) || + ((off + len) < 0) || ((off + len) > b.length)) { + throw new IndexOutOfBoundsException(); + } + if (len == 0) { + return; + } + int oldlen = this.len; + int newlen = oldlen + len; + if (newlen > this.buffer.length) { + expand(newlen); + } + for (int i1 = off, i2 = oldlen; i2 < newlen; i1++, i2++) { + this.buffer[i2] = (byte) b[i1]; + } + this.len = newlen; + } + + public void append(final CharArrayBuffer b, int off, int len) { + if (b == null) { + return; + } + append(b.buffer(), off, len); + } + + public void clear() { + this.len = 0; + } + + public byte[] toByteArray() { + byte[] b = new byte[this.len]; + if (this.len > 0) { + System.arraycopy(this.buffer, 0, b, 0, this.len); + } + return b; + } + + public int byteAt(int i) { + return this.buffer[i]; + } + + public int capacity() { + return this.buffer.length; + } + + public int length() { + return this.len; + } + + public byte[] buffer() { + return this.buffer; + } + + public void setLength(int len) { + if (len < 0 || len > this.buffer.length) { + throw new IndexOutOfBoundsException(); + } + this.len = len; + } + + public boolean isEmpty() { + return this.len == 0; + } + + public boolean isFull() { + return this.len == this.buffer.length; + } + +} diff --git a/src/org/apache/http/util/CharArrayBuffer.java b/src/org/apache/http/util/CharArrayBuffer.java new file mode 100644 index 0000000..b89f5d1 --- /dev/null +++ b/src/org/apache/http/util/CharArrayBuffer.java @@ -0,0 +1,264 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/CharArrayBuffer.java $ + * $Revision: 496070 $ + * $Date: 2007-01-14 04:18:34 -0800 (Sun, 14 Jan 2007) $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package org.apache.http.util; + +import org.apache.http.protocol.HTTP; + +/** + * A resizable char array. + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 496070 $ + * + * @since 4.0 + */ +public final class CharArrayBuffer { + + private char[] buffer; + private int len; + + public CharArrayBuffer(int capacity) { + super(); + if (capacity < 0) { + throw new IllegalArgumentException("Buffer capacity may not be negative"); + } + this.buffer = new char[capacity]; + } + + private void expand(int newlen) { + char newbuffer[] = new char[Math.max(this.buffer.length << 1, newlen)]; + System.arraycopy(this.buffer, 0, newbuffer, 0, this.len); + this.buffer = newbuffer; + } + + public void append(final char[] b, int off, int len) { + if (b == null) { + return; + } + if ((off < 0) || (off > b.length) || (len < 0) || + ((off + len) < 0) || ((off + len) > b.length)) { + throw new IndexOutOfBoundsException(); + } + if (len == 0) { + return; + } + int newlen = this.len + len; + if (newlen > this.buffer.length) { + expand(newlen); + } + System.arraycopy(b, off, this.buffer, this.len, len); + this.len = newlen; + } + + public void append(String str) { + if (str == null) { + str = "null"; + } + int strlen = str.length(); + int newlen = this.len + strlen; + if (newlen > this.buffer.length) { + expand(newlen); + } + str.getChars(0, strlen, this.buffer, this.len); + this.len = newlen; + } + + public void append(final CharArrayBuffer b, int off, int len) { + if (b == null) { + return; + } + append(b.buffer, off, len); + } + + public void append(final CharArrayBuffer b) { + if (b == null) { + return; + } + append(b.buffer,0, b.len); + } + + public void append(char ch) { + int newlen = this.len + 1; + if (newlen > this.buffer.length) { + expand(newlen); + } + this.buffer[this.len] = ch; + this.len = newlen; + } + + public void append(final byte[] b, int off, int len) { + if (b == null) { + return; + } + if ((off < 0) || (off > b.length) || (len < 0) || + ((off + len) < 0) || ((off + len) > b.length)) { + throw new IndexOutOfBoundsException(); + } + if (len == 0) { + return; + } + int oldlen = this.len; + int newlen = oldlen + len; + if (newlen > this.buffer.length) { + expand(newlen); + } + for (int i1 = off, i2 = oldlen; i2 < newlen; i1++, i2++) { + int ch = b[i1]; + if (ch < 0) { + ch = 256 + ch; + } + this.buffer[i2] = (char) ch; + } + this.len = newlen; + } + + public void append(final ByteArrayBuffer b, int off, int len) { + if (b == null) { + return; + } + append(b.buffer(), off, len); + } + + public void append(final Object obj) { + append(String.valueOf(obj)); + } + + public void clear() { + this.len = 0; + } + + public char[] toCharArray() { + char[] b = new char[this.len]; + if (this.len > 0) { + System.arraycopy(this.buffer, 0, b, 0, this.len); + } + return b; + } + + public char charAt(int i) { + return this.buffer[i]; + } + + public char[] buffer() { + return this.buffer; + } + + public int capacity() { + return this.buffer.length; + } + + public int length() { + return this.len; + } + + public void ensureCapacity(int required) { + int available = this.buffer.length - this.len; + if (required > available) { + expand(this.len + required); + } + } + + public void setLength(int len) { + if (len < 0 || len > this.buffer.length) { + throw new IndexOutOfBoundsException(); + } + this.len = len; + } + + public boolean isEmpty() { + return this.len == 0; + } + + public boolean isFull() { + return this.len == this.buffer.length; + } + + public int indexOf(int ch, int beginIndex, int endIndex) { + if (beginIndex < 0) { + beginIndex = 0; + } + if (endIndex > this.len) { + endIndex = this.len; + } + if (beginIndex > endIndex) { + return -1; + } + for (int i = beginIndex; i < endIndex; i++) { + if (this.buffer[i] == ch) { + return i; + } + } + return -1; + } + + public int indexOf(int ch) { + return indexOf(ch, 0, this.len); + } + + public String substring(int beginIndex, int endIndex) { + if (beginIndex < 0) { + throw new IndexOutOfBoundsException(); + } + if (endIndex > this.len) { + throw new IndexOutOfBoundsException(); + } + if (beginIndex > endIndex) { + throw new IndexOutOfBoundsException(); + } + return new String(this.buffer, beginIndex, endIndex - beginIndex); + } + + public String substringTrimmed(int beginIndex, int endIndex) { + if (beginIndex < 0) { + throw new IndexOutOfBoundsException(); + } + if (endIndex > this.len) { + throw new IndexOutOfBoundsException(); + } + if (beginIndex > endIndex) { + throw new IndexOutOfBoundsException(); + } + while (beginIndex < endIndex && HTTP.isWhitespace(this.buffer[beginIndex])) { + beginIndex++; + } + while (endIndex > beginIndex && HTTP.isWhitespace(this.buffer[endIndex - 1])) { + endIndex--; + } + return new String(this.buffer, beginIndex, endIndex - beginIndex); + } + + public String toString() { + return new String(this.buffer, 0, this.len); + } + +} diff --git a/src/org/apache/http/util/EncodingUtils.java b/src/org/apache/http/util/EncodingUtils.java new file mode 100644 index 0000000..a1b3f44 --- /dev/null +++ b/src/org/apache/http/util/EncodingUtils.java @@ -0,0 +1,185 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/EncodingUtils.java $ + * $Revision: 503413 $ + * $Date: 2007-02-04 06:22:14 -0800 (Sun, 04 Feb 2007) $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +package org.apache.http.util; + +import java.io.UnsupportedEncodingException; + +import org.apache.http.protocol.HTTP; + +/** + * The home for utility methods that handle various encoding tasks. + * + * @author Michael Becke + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @since 4.0 + */ +public final class EncodingUtils { + + /** + * Converts the byte array of HTTP content characters to a string. If + * the specified charset is not supported, default system encoding + * is used. + * + * @param data the byte array to be encoded + * @param offset the index of the first byte to encode + * @param length the number of bytes to encode + * @param charset the desired character encoding + * @return The result of the conversion. + */ + public static String getString( + final byte[] data, + int offset, + int length, + String charset + ) { + + if (data == null) { + throw new IllegalArgumentException("Parameter may not be null"); + } + + if (charset == null || charset.length() == 0) { + throw new IllegalArgumentException("charset may not be null or empty"); + } + + try { + return new String(data, offset, length, charset); + } catch (UnsupportedEncodingException e) { + return new String(data, offset, length); + } + } + + + /** + * Converts the byte array of HTTP content characters to a string. If + * the specified charset is not supported, default system encoding + * is used. + * + * @param data the byte array to be encoded + * @param charset the desired character encoding + * @return The result of the conversion. + */ + public static String getString(final byte[] data, final String charset) { + if (data == null) { + throw new IllegalArgumentException("Parameter may not be null"); + } + return getString(data, 0, data.length, charset); + } + + /** + * Converts the specified string to a byte array. If the charset is not supported the + * default system charset is used. + * + * @param data the string to be encoded + * @param charset the desired character encoding + * @return The resulting byte array. + */ + public static byte[] getBytes(final String data, final String charset) { + + if (data == null) { + throw new IllegalArgumentException("data may not be null"); + } + + if (charset == null || charset.length() == 0) { + throw new IllegalArgumentException("charset may not be null or empty"); + } + + try { + return data.getBytes(charset); + } catch (UnsupportedEncodingException e) { + return data.getBytes(); + } + } + + /** + * Converts the specified string to byte array of ASCII characters. + * + * @param data the string to be encoded + * @return The string as a byte array. + */ + public static byte[] getAsciiBytes(final String data) { + + if (data == null) { + throw new IllegalArgumentException("Parameter may not be null"); + } + + try { + return data.getBytes(HTTP.US_ASCII); + } catch (UnsupportedEncodingException e) { + throw new Error("HttpClient requires ASCII support"); + } + } + + /** + * Converts the byte array of ASCII characters to a string. This method is + * to be used when decoding content of HTTP elements (such as response + * headers) + * + * @param data the byte array to be encoded + * @param offset the index of the first byte to encode + * @param length the number of bytes to encode + * @return The string representation of the byte array + */ + public static String getAsciiString(final byte[] data, int offset, int length) { + + if (data == null) { + throw new IllegalArgumentException("Parameter may not be null"); + } + + try { + return new String(data, offset, length, HTTP.US_ASCII); + } catch (UnsupportedEncodingException e) { + throw new Error("HttpClient requires ASCII support"); + } + } + + /** + * Converts the byte array of ASCII characters to a string. This method is + * to be used when decoding content of HTTP elements (such as response + * headers) + * + * @param data the byte array to be encoded + * @return The string representation of the byte array + */ + public static String getAsciiString(final byte[] data) { + if (data == null) { + throw new IllegalArgumentException("Parameter may not be null"); + } + return getAsciiString(data, 0, data.length); + } + + /** + * This class should not be instantiated. + */ + private EncodingUtils() { + } + +} diff --git a/src/org/apache/http/util/EntityUtils.java b/src/org/apache/http/util/EntityUtils.java new file mode 100644 index 0000000..f9a7cf3 --- /dev/null +++ b/src/org/apache/http/util/EntityUtils.java @@ -0,0 +1,149 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/EntityUtils.java $ + * $Revision: 569637 $ + * $Date: 2007-08-25 00:36:59 -0700 (Sat, 25 Aug 2007) $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package org.apache.http.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; + +import org.apache.http.HeaderElement; +import org.apache.http.HttpEntity; +import org.apache.http.NameValuePair; +import org.apache.http.ParseException; +import org.apache.http.protocol.HTTP; + +/** + * Static helpers for dealing with {@link HttpEntity entities}. + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 569637 $ + * + * @since 4.0 + */ +public final class EntityUtils { + + /** Disabled default constructor. */ + private EntityUtils() { + } + + public static byte[] toByteArray(final HttpEntity entity) throws IOException { + if (entity == null) { + throw new IllegalArgumentException("HTTP entity may not be null"); + } + InputStream instream = entity.getContent(); + if (instream == null) { + return new byte[] {}; + } + if (entity.getContentLength() > Integer.MAX_VALUE) { + throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); + } + int i = (int)entity.getContentLength(); + if (i < 0) { + i = 4096; + } + ByteArrayBuffer buffer = new ByteArrayBuffer(i); + try { + byte[] tmp = new byte[4096]; + int l; + while((l = instream.read(tmp)) != -1) { + buffer.append(tmp, 0, l); + } + } finally { + instream.close(); + } + return buffer.toByteArray(); + } + + public static String getContentCharSet(final HttpEntity entity) + throws ParseException { + + if (entity == null) { + throw new IllegalArgumentException("HTTP entity may not be null"); + } + String charset = null; + if (entity.getContentType() != null) { + HeaderElement values[] = entity.getContentType().getElements(); + if (values.length > 0) { + NameValuePair param = values[0].getParameterByName("charset"); + if (param != null) { + charset = param.getValue(); + } + } + } + return charset; + } + + public static String toString( + final HttpEntity entity, final String defaultCharset) throws IOException, ParseException { + if (entity == null) { + throw new IllegalArgumentException("HTTP entity may not be null"); + } + InputStream instream = entity.getContent(); + if (instream == null) { + return ""; + } + if (entity.getContentLength() > Integer.MAX_VALUE) { + throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); + } + int i = (int)entity.getContentLength(); + if (i < 0) { + i = 4096; + } + String charset = getContentCharSet(entity); + if (charset == null) { + charset = defaultCharset; + } + if (charset == null) { + charset = HTTP.DEFAULT_CONTENT_CHARSET; + } + Reader reader = new InputStreamReader(instream, charset); + CharArrayBuffer buffer = new CharArrayBuffer(i); + try { + char[] tmp = new char[1024]; + int l; + while((l = reader.read(tmp)) != -1) { + buffer.append(tmp, 0, l); + } + } finally { + reader.close(); + } + return buffer.toString(); + } + + public static String toString(final HttpEntity entity) + throws IOException, ParseException { + return toString(entity, null); + } + +} diff --git a/src/org/apache/http/util/ExceptionUtils.java b/src/org/apache/http/util/ExceptionUtils.java new file mode 100644 index 0000000..c7fdccd --- /dev/null +++ b/src/org/apache/http/util/ExceptionUtils.java @@ -0,0 +1,85 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/ExceptionUtils.java $ + * $Revision: 613003 $ + * $Date: 2008-01-17 15:00:42 -0800 (Thu, 17 Jan 2008) $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +package org.apache.http.util; + +import java.lang.reflect.Method; + +/** + * The home for utility methods that handle various exception-related tasks. + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * @author <a href="mailto:laura@lwerner.org">Laura Werner</a> + * + * @since 4.0 + */ +public final class ExceptionUtils { + + /** A reference to Throwable's initCause method, or null if it's not there in this JVM */ + static private final Method INIT_CAUSE_METHOD = getInitCauseMethod(); + + /** + * Returns a <code>Method<code> allowing access to + * {@link Throwable#initCause(Throwable) initCause} method of {@link Throwable}, + * or <code>null</code> if the method + * does not exist. + * + * @return A <code>Method<code> for <code>Throwable.initCause</code>, or + * <code>null</code> if unavailable. + */ + static private Method getInitCauseMethod() { + try { + Class[] paramsClasses = new Class[] { Throwable.class }; + return Throwable.class.getMethod("initCause", paramsClasses); + } catch (NoSuchMethodException e) { + return null; + } + } + + /** + * If we're running on JDK 1.4 or later, initialize the cause for the given throwable. + * + * @param throwable The throwable. + * @param cause The cause of the throwable. + */ + public static void initCause(Throwable throwable, Throwable cause) { + if (INIT_CAUSE_METHOD != null) { + try { + INIT_CAUSE_METHOD.invoke(throwable, new Object[] { cause }); + } catch (Exception e) { + // Well, with no logging, the only option is to munch the exception + } + } + } + + private ExceptionUtils() { + } + +} diff --git a/src/org/apache/http/util/LangUtils.java b/src/org/apache/http/util/LangUtils.java new file mode 100644 index 0000000..a1dee8f --- /dev/null +++ b/src/org/apache/http/util/LangUtils.java @@ -0,0 +1,88 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/LangUtils.java $ + * $Revision: 503413 $ + * $Date: 2007-02-04 06:22:14 -0800 (Sun, 04 Feb 2007) $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package org.apache.http.util; + +/** + * A set of utility methods to help produce consistent + * {@link Object#equals equals} and {@link Object#hashCode hashCode} methods. + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @since 4.0 + */ +public final class LangUtils { + + public static final int HASH_SEED = 17; + public static final int HASH_OFFSET = 37; + + /** Disabled default constructor. */ + private LangUtils() { + } + + public static int hashCode(final int seed, final int hashcode) { + return seed * HASH_OFFSET + hashcode; + } + + public static int hashCode(final int seed, final boolean b) { + return hashCode(seed, b ? 1 : 0); + } + + public static int hashCode(final int seed, final Object obj) { + return hashCode(seed, obj != null ? obj.hashCode() : 0); + } + + public static boolean equals(final Object obj1, final Object obj2) { + return obj1 == null ? obj2 == null : obj1.equals(obj2); + } + + public static boolean equals(final Object[] a1, final Object[] a2) { + if (a1 == null) { + if (a2 == null) { + return true; + } else { + return false; + } + } else { + if (a2 != null && a1.length == a2.length) { + for (int i = 0; i < a1.length; i++) { + if (!equals(a1[i], a2[i])) { + return false; + } + } + return true; + } else { + return false; + } + } + } + +} diff --git a/src/org/apache/http/util/VersionInfo.java b/src/org/apache/http/util/VersionInfo.java new file mode 100644 index 0000000..0c95594 --- /dev/null +++ b/src/org/apache/http/util/VersionInfo.java @@ -0,0 +1,317 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/VersionInfo.java $ + * $Revision: 554888 $ + * $Date: 2007-07-10 02:46:36 -0700 (Tue, 10 Jul 2007) $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package org.apache.http.util; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; +import java.util.Properties; +import java.util.ArrayList; + + +/** + * Provides access to version information for HTTP components. + * Instances of this class provide version information for a single module + * or informal unit, as explained + * <a href="http://wiki.apache.org/jakarta-httpclient/HttpComponents">here</a>. + * Static methods are used to extract version information from property + * files that are automatically packaged with HTTP component release JARs. + * <br/> + * All available version information is provided in strings, where + * the string format is informal and subject to change without notice. + * Version information is provided for debugging output and interpretation + * by humans, not for automated processing in applications. + * + * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> + * @author and others + */ +public class VersionInfo { + + /** A string constant for unavailable information. */ + public final static String UNAVAILABLE = "UNAVAILABLE"; + + /** The filename of the version information files. */ + public final static String VERSION_PROPERTY_FILE = "version.properties"; + + // the property names + public final static String PROPERTY_MODULE = "info.module"; + public final static String PROPERTY_RELEASE = "info.release"; + public final static String PROPERTY_TIMESTAMP = "info.timestamp"; + + + /** The package that contains the version information. */ + private final String infoPackage; + + /** The module from the version info. */ + private final String infoModule; + + /** The release from the version info. */ + private final String infoRelease; + + /** The timestamp from the version info. */ + private final String infoTimestamp; + + /** The classloader from which the version info was obtained. */ + private final String infoClassloader; + + + /** + * Instantiates version information. + * + * @param pckg the package + * @param module the module, or <code>null</code> + * @param release the release, or <code>null</code> + * @param time the build time, or <code>null</code> + * @param clsldr the class loader, or <code>null</code> + */ + protected VersionInfo(String pckg, String module, + String release, String time, String clsldr) { + if (pckg == null) { + throw new IllegalArgumentException + ("Package identifier must not be null."); + } + + infoPackage = pckg; + infoModule = (module != null) ? module : UNAVAILABLE; + infoRelease = (release != null) ? release : UNAVAILABLE; + infoTimestamp = (time != null) ? time : UNAVAILABLE; + infoClassloader = (clsldr != null) ? clsldr : UNAVAILABLE; + } + + + /** + * Obtains the package name. + * The package name identifies the module or informal unit. + * + * @return the package name, never <code>null</code> + */ + public final String getPackage() { + return infoPackage; + } + + /** + * Obtains the name of the versioned module or informal unit. + * This data is read from the version information for the package. + * + * @return the module name, never <code>null</code> + */ + public final String getModule() { + return infoModule; + } + + /** + * Obtains the release of the versioned module or informal unit. + * This data is read from the version information for the package. + * + * @return the release version, never <code>null</code> + */ + public final String getRelease() { + return infoRelease; + } + + /** + * Obtains the timestamp of the versioned module or informal unit. + * This data is read from the version information for the package. + * + * @return the timestamp, never <code>null</code> + */ + public final String getTimestamp() { + return infoTimestamp; + } + + /** + * Obtains the classloader used to read the version information. + * This is just the <code>toString</code> output of the classloader, + * since the version information should not keep a reference to + * the classloader itself. That could prevent garbage collection. + * + * @return the classloader description, never <code>null</code> + */ + public final String getClassloader() { + return infoClassloader; + } + + + /** + * Provides the version information in human-readable format. + * + * @return a string holding this version information + */ + public String toString() { + StringBuffer sb = new StringBuffer + (20 + infoPackage.length() + infoModule.length() + + infoRelease.length() + infoTimestamp.length() + + infoClassloader.length()); + + sb.append("VersionInfo(") + .append(infoPackage).append(':').append(infoModule); + + // If version info is missing, a single "UNAVAILABLE" for the module + // is sufficient. Everything else just clutters the output. + if (!UNAVAILABLE.equals(infoRelease)) + sb.append(':').append(infoRelease); + if (!UNAVAILABLE.equals(infoTimestamp)) + sb.append(':').append(infoTimestamp); + + sb.append(')'); + + if (!UNAVAILABLE.equals(infoClassloader)) + sb.append('@').append(infoClassloader); + + return sb.toString(); + } + + + /** + * Loads version information for a list of packages. + * + * @param pckgs the packages for which to load version info + * @param clsldr the classloader to load from, or + * <code>null</code> for the thread context classloader + * + * @return the version information for all packages found, + * never <code>null</code> + */ + public final static VersionInfo[] loadVersionInfo(String[] pckgs, + ClassLoader clsldr) { + if (pckgs == null) { + throw new IllegalArgumentException + ("Package identifier list must not be null."); + } + + ArrayList vil = new ArrayList(pckgs.length); + for (int i=0; i<pckgs.length; i++) { + VersionInfo vi = loadVersionInfo(pckgs[i], clsldr); + if (vi != null) + vil.add(vi); + } + + return (VersionInfo[]) vil.toArray(new VersionInfo[vil.size()]); + } + + + /** + * Loads version information for a package. + * + * @param pckg the package for which to load version information, + * for example "org.apache.http". + * The package name should NOT end with a dot. + * @param clsldr the classloader to load from, or + * <code>null</code> for the thread context classloader + * + * @return the version information for the argument package, or + * <code>null</code> if not available + */ + public final static VersionInfo loadVersionInfo(final String pckg, + ClassLoader clsldr) { + if (pckg == null) { + throw new IllegalArgumentException + ("Package identifier must not be null."); + } + + if (clsldr == null) + clsldr = Thread.currentThread().getContextClassLoader(); + + Properties vip = null; // version info properties, if available + try { + // org.apache.http becomes + // org/apache/http/version.properties + InputStream is = clsldr.getResourceAsStream + (pckg.replace('.', '/') + "/" + VERSION_PROPERTY_FILE); + if (is != null) { + try { + Properties props = new Properties(); + props.load(is); + vip = props; + } finally { + is.close(); + } + } + } catch (IOException ex) { + // shamelessly munch this exception + } + + VersionInfo result = null; + if (vip != null) + result = fromMap(pckg, vip, clsldr); + + return result; + } + + + /** + * Instantiates version information from properties. + * + * @param pckg the package for the version information + * @param info the map from string keys to string values, + * for example {@link java.util.Properties} + * @param clsldr the classloader, or <code>null</code> + * + * @return the version information + */ + protected final static VersionInfo fromMap(String pckg, Map info, + ClassLoader clsldr) { + if (pckg == null) { + throw new IllegalArgumentException + ("Package identifier must not be null."); + } + + String module = null; + String release = null; + String timestamp = null; + + if (info != null) { + module = (String) info.get(PROPERTY_MODULE); + if ((module != null) && (module.length() < 1)) + module = null; + + release = (String) info.get(PROPERTY_RELEASE); + if ((release != null) && ((release.length() < 1) || + (release.equals("${pom.version}")))) + release = null; + + timestamp = (String) info.get(PROPERTY_TIMESTAMP); + if ((timestamp != null) && + ((timestamp.length() < 1) || + (timestamp.equals("${mvn.timestamp}"))) + ) + timestamp = null; + } // if info + + String clsldrstr = null; + if (clsldr != null) + clsldrstr = clsldr.toString(); + + return new VersionInfo(pckg, module, release, timestamp, clsldrstr); + } + +} // class VersionInfo diff --git a/src/org/apache/http/util/package.html b/src/org/apache/http/util/package.html new file mode 100644 index 0000000..19d97b3 --- /dev/null +++ b/src/org/apache/http/util/package.html @@ -0,0 +1,45 @@ +<html> +<head> +<!-- +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/package.html $ + * $Revision: 496070 $ + * $Date: 2007-01-14 04:18:34 -0800 (Sun, 14 Jan 2007) $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +--> +</head> +<body> +Mostly utility classes with static helper methods for various purposes. + +The helper classes for resizable +{@link org.apache.http.util.ByteArrayBuffer byte} and +{@link org.apache.http.util.CharArrayBuffer char} arrays +do not fall into this category. + +</body> +</html> |