Merge last green changeset of mozilla-inbound to mozilla-central

This commit is contained in:
Ed Morley 2011-12-21 20:31:10 +00:00
Родитель 6477759d1c 3f28991c6c
Коммит 5a36c97a86
690 изменённых файлов: 85730 добавлений и 6 удалений

Просмотреть файл

@ -12,6 +12,8 @@
<uses-sdk android:minSdkVersion="5"
android:targetSdkVersion="11"/>
#include ../sync/manifests/SyncAndroidManifest_permissions.xml.in
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
@ -104,6 +106,8 @@
</intent-filter>
</activity>
#include ../sync/manifests/SyncAndroidManifest_activities.xml.in
#if MOZ_CRASHREPORTER
<activity android:name="CrashReporter"
android:label="@string/crash_reporter_title"
@ -144,6 +148,7 @@
android:authorities="@ANDROID_PACKAGE_NAME@.db.browser"
android:permission="@ANDROID_PACKAGE_NAME@.permissions.BROWSER_PROVIDER"/>
#include ../sync/manifests/SyncAndroidManifest_services.xml.in
</application>
<permission android:name="@ANDROID_PACKAGE_NAME@.permissions.BROWSER_PROVIDER"

Просмотреть файл

@ -20,6 +20,7 @@
#
# Contributor(s):
# Vladimir Vukicevic <vladimir@pobox.com>
# Richard Newman <rnewman@mozilla.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
@ -47,7 +48,15 @@ DIRS = locales
DIST_FILES = package-name.txt
SYNC_JAVA_FILES=$(shell cat $(topsrcdir)/mobile/android/sync/java-sources.mn | tr '\n' ' ';)
SYNC_PP_JAVA_FILES=$(shell cat $(topsrcdir)/mobile/android/sync/preprocess-sources.mn | tr '\n' ' ';)
SYNC_RES_DRAWABLE=$(shell cat $(topsrcdir)/mobile/android/sync/android-drawable-resources.mn | tr '\n' ' ';)
SYNC_RES_LAYOUT=$(shell cat $(topsrcdir)/mobile/android/sync/android-layout-resources.mn | tr '\n' ' ';)
SYNC_RES_VALUES=$(shell cat $(topsrcdir)/mobile/android/sync/android-values-resources.mn | tr '\n' ' ';)
SYNC_RES_XML=$(shell cat $(topsrcdir)/mobile/android/sync/android-xml-resources.mn | tr '\n' ' ';)
JAVAFILES = \
$(SYNC_JAVA_FILES) \
AboutHomeContent.java \
AlertNotification.java \
AutoCompletePopup.java \
@ -112,6 +121,7 @@ JAVAFILES = \
$(NULL)
PROCESSEDJAVAFILES = \
$(SYNC_PP_JAVA_FILES) \
App.java \
LauncherShortcuts.java \
NotificationHandler.java \
@ -177,6 +187,7 @@ DEFINES += -DMOZ_ANDROID_SHARED_ID="$(ANDROID_PACKAGE_NAME).sharedID"
endif
RES_LAYOUT = \
$(SYNC_RES_LAYOUT) \
res/layout/autocomplete_list_item.xml \
res/layout/awesomebar_header_row.xml \
res/layout/awesomebar_row.xml \
@ -211,6 +222,7 @@ RES_LAYOUT_V11 = \
$(NULL)
RES_VALUES = \
$(SYNC_RES_VALUES) \
res/values/defaults.xml \
res/values/arrays.xml \
res/values/colors.xml \
@ -223,7 +235,11 @@ RES_VALUES_V11 = \
res/values-v11/themes.xml \
$(NULL)
RES_XML = res/xml/preferences.xml
RES_XML = \
res/xml/preferences.xml \
res/xml/sync_syncadapter.xml \
$(SYNC_RES_XML) \
$(NULL)
RES_ANIM = \
res/anim/grow_fade_in.xml \
@ -448,7 +464,9 @@ MOZ_ANDROID_DRAWABLES += mobile/android/base/resources/drawable/crash_reporter.p
RES_LAYOUT += res/layout/crash_reporter.xml
endif
MOZ_ANDROID_DRAWABLES += mobile/android/base/resources/drawable/abouthome_bg_repeat.xml \
MOZ_ANDROID_DRAWABLES += \
$(SYNC_RES_DRAWABLE) \
mobile/android/base/resources/drawable/abouthome_bg_repeat.xml \
mobile/android/base/resources/drawable/abouthome_topsites_bg_repeat.xml \
mobile/android/base/resources/drawable/address_bar_bg.xml \
mobile/android/base/resources/drawable/address_bar_bg_shadow.xml \
@ -505,13 +523,23 @@ include $(topsrcdir)/config/android-common.mk
# Note that we're going to set up a dependency directly between embed_android.dex and the java files
# Instead of on the .class files, since more than one .class file might be produced per .java file
# Sync dependencies are provided in a single jar. Sync classes themselves are delivered as source,
# because Android resource classes must be compiled together in order to avoid overlapping resource
# indices.
classes.dex: $(JAVAFILES) $(PROCESSEDJAVAFILES) R.java
$(NSINSTALL) -D classes
$(JAVAC) $(JAVAC_FLAGS) -Xlint:unchecked -Xlint:deprecation -d classes $(addprefix $(srcdir)/,$(JAVAFILES)) $(PROCESSEDJAVAFILES) R.java
$(DX) --dex --output=$@ classes
# This needs to be preprocessed to match Fennec's browser authority.
# Right now this leaves the preprocessed file in base/resources. Doing a direct
# copy didn't seem to work. This needs fixing.
$(topsrcdir)/mobile/android/base/resources/xml/sync_syncadapter.xml:
$(PYTHON) $(topsrcdir)/config/Preprocessor.py \
$(AUTOMATION_PPARGS) $(DEFINES) $(ACDEFINES) $(topsrcdir)/mobile/android/base/resources/xml/sync_syncadapter.xml.in > $@
AndroidManifest.xml $(PROCESSEDJAVAFILES) package-name.txt: % : %.in
mkdir -p db
mkdir -p db sync/repositories/android
$(PYTHON) $(topsrcdir)/config/Preprocessor.py \
$(AUTOMATION_PPARGS) $(DEFINES) $(ACDEFINES) $< > $@
@ -538,6 +566,7 @@ $(RES_LAYOUT_V11): $(subst res/,$(srcdir)/resources/,$(RES_LAYOUT_V11))
$(NSINSTALL) $(srcdir)/resources/layout-v11/* res/layout-v11/
$(RES_VALUES): \
$(subst res/,$(srcdir)/resources/,$(SYNC_RES_VALUES)) \
$(srcdir)/resources/values/colors.xml \
$(srcdir)/resources/values/styles.xml \
$(srcdir)/resources/values/themes.xml \
@ -552,7 +581,7 @@ $(RES_VALUES_V11): $(subst res/,$(srcdir)/resources/,$(RES_VALUES_V11))
$(RES_XML): $(subst res/,$(srcdir)/resources/,$(RES_XML))
$(NSINSTALL) -D res/xml
$(NSINSTALL) $(srcdir)/resources/xml/* res/xml/
$(NSINSTALL) $(srcdir)/resources/xml/*.xml res/xml/
$(RES_ANIM): $(subst res/,$(srcdir)/resources/,$(RES_ANIM))
$(NSINSTALL) -D res/anim
@ -610,10 +639,10 @@ $(RES_MENU): $(subst res/,$(srcdir)/resources/,$(RES_MENU))
$(NSINSTALL) -D res/menu
$(NSINSTALL) $^ res/menu
R.java: $(MOZ_APP_ICON) $(RES_LAYOUT) $(RES_LAYOUT_V11) $(RES_DRAWABLE) $(RES_VALUES) $(RES_VALUES_V11) $(RES_XML) $(RES_ANIM) $(RES_DRAWABLE_NODPI) $(RES_DRAWABLE_MDPI_V8) $(RES_DRAWABLE_HDPI_V8) $(RES_DRAWABLE_MDPI_V9) $(RES_DRAWABLE_HDPI_V9) $(RES_DRAWABLE_MDPI_V11) $(RES_DRAWABLE_HDPI_V11) $(RES_DRAWABLE_XHDPI_V11) $(RES_DRAWABLE_LAND_MDPI_V14) $(RES_DRAWABLE_LAND_HDPI_V14) $(RES_DRAWABLE_LAND_XHDPI_V14) $(RES_COLOR) $(RES_MENU) res/drawable/icon.png res/drawable-hdpi/icon.png res/values/strings.xml AndroidManifest.xml
R.java: $(MOZ_APP_ICON) $(RES_LAYOUT) $(RES_LAYOUT_V11) $(RES_DRAWABLE) $(RES_VALUES) $(RES_VALUES_V11) $(RES_XML) $(RES_ANIM) $(RES_DRAWABLE_NODPI) $(RES_DRAWABLE_MDPI_V8) $(RES_DRAWABLE_HDPI_V8) $(RES_DRAWABLE_MDPI_V9) $(RES_DRAWABLE_HDPI_V9) $(RES_DRAWABLE_MDPI_V11) $(RES_DRAWABLE_HDPI_V11) $(RES_DRAWABLE_XHDPI_V11) $(RES_DRAWABLE_LAND_MDPI_V14) $(RES_DRAWABLE_LAND_HDPI_V14) $(RES_DRAWABLE_LAND_XHDPI_V14) $(RES_COLOR) $(RES_MENU) res/drawable/sync_icon.png res/drawable/icon.png res/drawable-hdpi/icon.png res/values/strings.xml AndroidManifest.xml
$(AAPT) package -f -M AndroidManifest.xml -I $(ANDROID_SDK)/android.jar -S res -J . --custom-package org.mozilla.gecko
gecko.ap_: AndroidManifest.xml res/drawable/icon.png res/drawable-hdpi/icon.png $(RES_LAYOUT) $(RES_LAYOUT_V11) $(RES_DRAWABLE) $(RES_VALUES) $(RES_VALUES_V11) $(RES_XML) $(RES_ANIM) $(RES_DRAWABLE_NODPI) $(RES_DRAWABLE_MDPI_V8) $(RES_DRAWABLE_HDPI_V8) $(RES_DRAWABLE_MDPI_V9) $(RES_DRAWABLE_HDPI_V9) $(RES_DRAWABLE_MDPI_V11) $(RES_DRAWABLE_HDPI_V11) $(RES_DRAWABLE_XHDPI_V11) $(RES_DRAWABLE_LAND_MDPI_V14) $(RES_DRAWABLE_LAND_HDPI_V14) $(RES_DRAWABLE_LAND_XHDPI_V14) $(RES_COLOR) $(RES_MENU) res/values/strings.xml FORCE
gecko.ap_: AndroidManifest.xml res/drawable/sync_icon.png res/drawable/icon.png res/drawable-hdpi/icon.png $(RES_LAYOUT) $(RES_LAYOUT_V11) $(RES_DRAWABLE) $(RES_VALUES) $(RES_VALUES_V11) $(RES_XML) $(RES_ANIM) $(RES_DRAWABLE_NODPI) $(RES_DRAWABLE_MDPI_V8) $(RES_DRAWABLE_HDPI_V8) $(RES_DRAWABLE_MDPI_V9) $(RES_DRAWABLE_HDPI_V9) $(RES_DRAWABLE_MDPI_V11) $(RES_DRAWABLE_HDPI_V11) $(RES_DRAWABLE_XHDPI_V11) $(RES_DRAWABLE_LAND_MDPI_V14) $(RES_DRAWABLE_LAND_HDPI_V14) $(RES_DRAWABLE_LAND_XHDPI_V14) $(RES_COLOR) $(RES_MENU) res/values/strings.xml FORCE
$(AAPT) package -f -M AndroidManifest.xml -I $(ANDROID_SDK)/android.jar -S res -F $@
libs:: classes.dex package-name.txt

Просмотреть файл

@ -0,0 +1,43 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec;
/**
* Defines common decoding methods for byte array decoders.
*
* @author Apache Software Foundation
* @version $Id: BinaryDecoder.java 1075406 2011-02-28 16:18:26Z ggregory $
*/
public interface BinaryDecoder extends Decoder {
/**
* Decodes a byte array and returns the results as a byte array.
*
* @param source A byte array which has been encoded with the
* appropriate encoder
*
* @return a byte array that contains decoded content
*
* @throws DecoderException A decoder exception is thrown
* if a Decoder encounters a failure condition during
* the decode process.
*/
byte[] decode(byte[] source) throws DecoderException;
}

Просмотреть файл

@ -0,0 +1,43 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec;
/**
* Defines common encoding methods for byte array encoders.
*
* @author Apache Software Foundation
* @version $Id: BinaryEncoder.java 1075406 2011-02-28 16:18:26Z ggregory $
*/
public interface BinaryEncoder extends Encoder {
/**
* Encodes a byte array and return the encoded data
* as a byte array.
*
* @param source Data to be encoded
*
* @return A byte array containing the encoded data
*
* @throws EncoderException thrown if the Encoder
* encounters a failure condition during the
* encoding process.
*/
byte[] encode(byte[] source) throws EncoderException;
}

Просмотреть файл

@ -0,0 +1,127 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec;
/**
* Character encoding names required of every implementation of the Java platform.
*
* From the Java documentation <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard
* charsets</a>:
* <p>
* <cite>Every implementation of the Java platform is required to support the following character encodings. Consult the
* release documentation for your implementation to see if any other encodings are supported. Consult the release
* documentation for your implementation to see if any other encodings are supported. </cite>
* </p>
*
* <ul>
* <li><code>US-ASCII</code><br/>
* Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</li>
* <li><code>ISO-8859-1</code><br/>
* ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</li>
* <li><code>UTF-8</code><br/>
* Eight-bit Unicode Transformation Format.</li>
* <li><code>UTF-16BE</code><br/>
* Sixteen-bit Unicode Transformation Format, big-endian byte order.</li>
* <li><code>UTF-16LE</code><br/>
* Sixteen-bit Unicode Transformation Format, little-endian byte order.</li>
* <li><code>UTF-16</code><br/>
* Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order
* accepted on input, big-endian used on output.)</li>
* </ul>
*
* This perhaps would best belong in the [lang] project. Even if a similar interface is defined in [lang], it is not
* forseen that [codec] would be made to depend on [lang].
*
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
* @author Apache Software Foundation
* @since 1.4
* @version $Id: CharEncoding.java 797857 2009-07-25 23:43:33Z ggregory $
*/
public class CharEncoding {
/**
* CharEncodingISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1. </p>
* <p>
* Every implementation of the Java platform is required to support this character encoding.
* </p>
*
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
*/
public static final String ISO_8859_1 = "ISO-8859-1";
/**
* <p>
* Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
* </p>
* <p>
* Every implementation of the Java platform is required to support this character encoding.
* </p>
*
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
*/
public static final String US_ASCII = "US-ASCII";
/**
* <p>
* Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
* (either order accepted on input, big-endian used on output)
* </p>
* <p>
* Every implementation of the Java platform is required to support this character encoding.
* </p>
*
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
*/
public static final String UTF_16 = "UTF-16";
/**
* <p>
* Sixteen-bit Unicode Transformation Format, big-endian byte order.
* </p>
* <p>
* Every implementation of the Java platform is required to support this character encoding.
* </p>
*
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
*/
public static final String UTF_16BE = "UTF-16BE";
/**
* <p>
* Sixteen-bit Unicode Transformation Format, little-endian byte order.
* </p>
* <p>
* Every implementation of the Java platform is required to support this character encoding.
* </p>
*
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
*/
public static final String UTF_16LE = "UTF-16LE";
/**
* <p>
* Eight-bit Unicode Transformation Format.
* </p>
* <p>
* Every implementation of the Java platform is required to support this character encoding.
* </p>
*
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
*/
public static final String UTF_8 = "UTF-8";
}

Просмотреть файл

@ -0,0 +1,56 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec;
/**
* <p>Provides the highest level of abstraction for Decoders.
* This is the sister interface of {@link Encoder}. All
* Decoders implement this common generic interface.</p>
*
* <p>Allows a user to pass a generic Object to any Decoder
* implementation in the codec package.</p>
*
* <p>One of the two interfaces at the center of the codec package.</p>
*
* @author Apache Software Foundation
* @version $Id: Decoder.java 1075404 2011-02-28 16:17:29Z ggregory $
*/
public interface Decoder {
/**
* Decodes an "encoded" Object and returns a "decoded"
* Object. Note that the implementation of this
* interface will try to cast the Object parameter
* to the specific type expected by a particular Decoder
* implementation. If a {@link ClassCastException} occurs
* this decode method will throw a DecoderException.
*
* @param source the object to decode
*
* @return a 'decoded" object
*
* @throws DecoderException a decoder exception can
* be thrown for any number of reasons. Some good
* candidates are that the parameter passed to this
* method is null, a param cannot be cast to the
* appropriate type for a specific encoder.
*/
Object decode(Object source) throws DecoderException;
}

Просмотреть файл

@ -0,0 +1,90 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec;
/**
* Thrown when there is a failure condition during the decoding process. This exception is thrown when a {@link Decoder}
* encounters a decoding specific exception such as invalid data, or characters outside of the expected range.
*
* @author Apache Software Foundation
* @version $Id: DecoderException.java 1080701 2011-03-11 17:52:27Z ggregory $
*/
public class DecoderException extends Exception {
/**
* Declares the Serial Version Uid.
*
* @see <a href="http://c2.com/cgi/wiki?AlwaysDeclareSerialVersionUid">Always Declare Serial Version Uid</a>
*/
private static final long serialVersionUID = 1L;
/**
* Constructs a new exception with <code>null</code> as its detail message. The cause is not initialized, and may
* subsequently be initialized by a call to {@link #initCause}.
*
* @since 1.4
*/
public DecoderException() {
super();
}
/**
* Constructs a new exception with the specified detail message. The cause is not initialized, and may subsequently
* be initialized by a call to {@link #initCause}.
*
* @param message
* The detail message which is saved for later retrieval by the {@link #getMessage()} method.
*/
public DecoderException(String message) {
super(message);
}
/**
* Constructsa new exception with the specified detail message and cause.
*
* <p>
* Note that the detail message associated with <code>cause</code> is not automatically incorporated into this
* exception's detail message.
* </p>
*
* @param message
* The detail message which is saved for later retrieval by the {@link #getMessage()} method.
* @param cause
* The cause which is saved for later retrieval by the {@link #getCause()} method. A <code>null</code>
* value is permitted, and indicates that the cause is nonexistent or unknown.
* @since 1.4
*/
public DecoderException(String message, Throwable cause) {
super(message, cause);
}
/**
* Constructs a new exception with the specified cause and a detail message of <code>(cause==null ?
* null : cause.toString())</code> (which typically contains the class and detail message of <code>cause</code>).
* This constructor is useful for exceptions that are little more than wrappers for other throwables.
*
* @param cause
* The cause which is saved for later retrieval by the {@link #getCause()} method. A <code>null</code>
* value is permitted, and indicates that the cause is nonexistent or unknown.
* @since 1.4
*/
public DecoderException(Throwable cause) {
super(cause);
}
}

Просмотреть файл

@ -0,0 +1,47 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec;
/**
* <p>Provides the highest level of abstraction for Encoders.
* This is the sister interface of {@link Decoder}. Every implementation of
* Encoder provides this common generic interface whic allows a user to pass a
* generic Object to any Encoder implementation in the codec package.</p>
*
* @author Apache Software Foundation
* @version $Id: Encoder.java 1075406 2011-02-28 16:18:26Z ggregory $
*/
public interface Encoder {
/**
* Encodes an "Object" and returns the encoded content
* as an Object. The Objects here may just be <code>byte[]</code>
* or <code>String</code>s depending on the implementation used.
*
* @param source An object ot encode
*
* @return An "encoded" Object
*
* @throws EncoderException an encoder exception is
* thrown if the encoder experiences a failure
* condition during the encoding process.
*/
Object encode(Object source) throws EncoderException;
}

Просмотреть файл

@ -0,0 +1,91 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec;
/**
* Thrown when there is a failure condition during the encoding process. This exception is thrown when an
* {@link Encoder} encounters a encoding specific exception such as invalid data, inability to calculate a checksum,
* characters outside of the expected range.
*
* @author Apache Software Foundation
* @version $Id: EncoderException.java 1080701 2011-03-11 17:52:27Z ggregory $
*/
public class EncoderException extends Exception {
/**
* Declares the Serial Version Uid.
*
* @see <a href="http://c2.com/cgi/wiki?AlwaysDeclareSerialVersionUid">Always Declare Serial Version Uid</a>
*/
private static final long serialVersionUID = 1L;
/**
* Constructs a new exception with <code>null</code> as its detail message. The cause is not initialized, and may
* subsequently be initialized by a call to {@link #initCause}.
*
* @since 1.4
*/
public EncoderException() {
super();
}
/**
* Constructs a new exception with the specified detail message. The cause is not initialized, and may subsequently
* be initialized by a call to {@link #initCause}.
*
* @param message
* a useful message relating to the encoder specific error.
*/
public EncoderException(String message) {
super(message);
}
/**
* Constructs a new exception with the specified detail message and cause.
*
* <p>
* Note that the detail message associated with <code>cause</code> is not automatically incorporated into this
* exception's detail message.
* </p>
*
* @param message
* The detail message which is saved for later retrieval by the {@link #getMessage()} method.
* @param cause
* The cause which is saved for later retrieval by the {@link #getCause()} method. A <code>null</code>
* value is permitted, and indicates that the cause is nonexistent or unknown.
* @since 1.4
*/
public EncoderException(String message, Throwable cause) {
super(message, cause);
}
/**
* Constructs a new exception with the specified cause and a detail message of <code>(cause==null ?
* null : cause.toString())</code> (which typically contains the class and detail message of <code>cause</code>).
* This constructor is useful for exceptions that are little more than wrappers for other throwables.
*
* @param cause
* The cause which is saved for later retrieval by the {@link #getCause()} method. A <code>null</code>
* value is permitted, and indicates that the cause is nonexistent or unknown.
* @since 1.4
*/
public EncoderException(Throwable cause) {
super(cause);
}
}

Просмотреть файл

@ -0,0 +1,41 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec;
/**
* Defines common decoding methods for String decoders.
*
* @author Apache Software Foundation
* @version $Id: StringDecoder.java 1080701 2011-03-11 17:52:27Z ggregory $
*/
public interface StringDecoder extends Decoder {
/**
* Decodes a String and returns a String.
*
* @param source the String to decode
*
* @return the encoded String
*
* @throws DecoderException thrown if there is
* an error condition during the Encoding process.
*/
String decode(String source) throws DecoderException;
}

Просмотреть файл

@ -0,0 +1,41 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec;
/**
* Defines common encoding methods for String encoders.
*
* @author Apache Software Foundation
* @version $Id: StringEncoder.java 1080701 2011-03-11 17:52:27Z ggregory $
*/
public interface StringEncoder extends Encoder {
/**
* Encodes a String and returns a String.
*
* @param source the String to encode
*
* @return the encoded String
*
* @throws EncoderException thrown if there is
* an error conidition during the Encoding process.
*/
String encode(String source) throws EncoderException;
}

Просмотреть файл

@ -0,0 +1,85 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec;
import java.util.Comparator;
/**
* Compares Strings using a {@link StringEncoder}. This comparator is used to sort Strings by an encoding scheme such as
* Soundex, Metaphone, etc. This class can come in handy if one need to sort Strings by an encoded form of a name such
* as Soundex.
*
* @author Apache Software Foundation
* @version $Id: StringEncoderComparator.java 1080701 2011-03-11 17:52:27Z ggregory $
*/
public class StringEncoderComparator implements Comparator {
/**
* Internal encoder instance.
*/
private final StringEncoder stringEncoder;
/**
* Constructs a new instance.
*
* @deprecated Creating an instance without a {@link StringEncoder} leads to a {@link NullPointerException}. Will be
* removed in 2.0.
*/
public StringEncoderComparator() {
this.stringEncoder = null; // Trying to use this will cause things to break
}
/**
* Constructs a new instance with the given algorithm.
*
* @param stringEncoder
* the StringEncoder used for comparisons.
*/
public StringEncoderComparator(StringEncoder stringEncoder) {
this.stringEncoder = stringEncoder;
}
/**
* Compares two strings based not on the strings themselves, but on an encoding of the two strings using the
* StringEncoder this Comparator was created with.
*
* If an {@link EncoderException} is encountered, return <code>0</code>.
*
* @param o1
* the object to compare
* @param o2
* the object to compare to
* @return the Comparable.compareTo() return code or 0 if an encoding error was caught.
* @see Comparable
*/
public int compare(Object o1, Object o2) {
int compareCode = 0;
try {
Comparable s1 = (Comparable) this.stringEncoder.encode(o1);
Comparable s2 = (Comparable) this.stringEncoder.encode(o2);
compareCode = s1.compareTo(s2);
} catch (EncoderException ee) {
compareCode = 0;
}
return compareCode;
}
}

Просмотреть файл

@ -0,0 +1,471 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.binary;
/**
* Provides Base32 encoding and decoding as defined by <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a>.
*
* <p>
* The class can be parameterized in the following manner with various constructors:
* <ul>
* <li>Whether to use the "base32hex" variant instead of the default "base32"</li>
* <li>Line length: Default 76. Line length that aren't multiples of 8 will still essentially end up being multiples of
* 8 in the encoded data.
* <li>Line separator: Default is CRLF ("\r\n")</li>
* </ul>
* </p>
* <p>
* This class operates directly on byte streams, and not character streams.
* </p>
* <p>
* This class is not thread-safe. Each thread should use its own instance.
* </p>
*
* @see <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a>
*
* @since 1.5
* @version $Revision: 1080712 $
*/
public class Base32 extends BaseNCodec {
/**
* BASE32 characters are 5 bits in length.
* They are formed by taking a block of five octets to form a 40-bit string,
* which is converted into eight BASE32 characters.
*/
private static final int BITS_PER_ENCODED_BYTE = 5;
private static final int BYTES_PER_ENCODED_BLOCK = 8;
private static final int BYTES_PER_UNENCODED_BLOCK = 5;
/**
* Chunk separator per RFC 2045 section 2.1.
*
* @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 2.1</a>
*/
private static final byte[] CHUNK_SEPARATOR = {'\r', '\n'};
/**
* This array is a lookup table that translates Unicode characters drawn from the "Base32 Alphabet" (as specified in
* Table 3 of RFC 2045) into their 5-bit positive integer equivalents. Characters that are not in the Base32
* alphabet but fall within the bounds of the array are translated to -1.
*
*/
private static final byte[] DECODE_TABLE = {
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00-0f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10-1f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 63, // 20-2f
-1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, // 30-3f 2-7
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 40-4f A-N
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 50-5a O-Z
};
/**
* This array is a lookup table that translates 5-bit positive integer index values into their "Base32 Alphabet"
* equivalents as specified in Table 3 of RFC 2045.
*/
private static final byte[] ENCODE_TABLE = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'2', '3', '4', '5', '6', '7',
};
/**
* This array is a lookup table that translates Unicode characters drawn from the "Base32 |Hex Alphabet" (as specified in
* Table 3 of RFC 2045) into their 5-bit positive integer equivalents. Characters that are not in the Base32 Hex
* alphabet but fall within the bounds of the array are translated to -1.
*
*/
private static final byte[] HEX_DECODE_TABLE = {
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00-0f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10-1f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 63, // 20-2f
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 30-3f 2-7
-1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 40-4f A-N
25, 26, 27, 28, 29, 30, 31, 32, // 50-57 O-V
};
/**
* This array is a lookup table that translates 5-bit positive integer index values into their "Base32 Hex Alphabet"
* equivalents as specified in Table 3 of RFC 2045.
*/
private static final byte[] HEX_ENCODE_TABLE = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
};
/** Mask used to extract 5 bits, used when encoding Base32 bytes */
private static final int MASK_5BITS = 0x1f;
// The static final fields above are used for the original static byte[] methods on Base32.
// The private member fields below are used with the new streaming approach, which requires
// some state be preserved between calls of encode() and decode().
/**
* Place holder for the bytes we're dealing with for our based logic.
* Bitwise operations store and extract the encoding or decoding from this variable.
*/
private long bitWorkArea;
/**
* Convenience variable to help us determine when our buffer is going to run out of room and needs resizing.
* <code>decodeSize = {@link BYTES_PER_ENCODED_BLOCK} - 1 + lineSeparator.length;</code>
*/
private final int decodeSize;
/**
* Decode table to use.
*/
private final byte[] decodeTable;
/**
* Convenience variable to help us determine when our buffer is going to run out of room and needs resizing.
* <code>encodeSize = {@link BYTES_PER_ENCODED_BLOCK} + lineSeparator.length;</code>
*/
private final int encodeSize;
/**
* Encode table to use.
*/
private final byte[] encodeTable;
/**
* Line separator for encoding. Not used when decoding. Only used if lineLength > 0.
*/
private final byte[] lineSeparator;
/**
* Creates a Base32 codec used for decoding and encoding.
* <p>
* When encoding the line length is 0 (no chunking).
* </p>
*
*/
public Base32() {
this(false);
}
/**
* Creates a Base32 codec used for decoding and encoding.
* <p>
* When encoding the line length is 0 (no chunking).
* </p>
* @param useHex if <code>true</code> then use Base32 Hex alphabet
*/
public Base32(boolean useHex) {
this(0, null, useHex);
}
/**
* Creates a Base32 codec used for decoding and encoding.
* <p>
* When encoding the line length is given in the constructor, the line separator is CRLF.
* </p>
*
* @param lineLength
* Each line of encoded data will be at most of the given length (rounded down to nearest multiple of 8).
* If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
*/
public Base32(int lineLength) {
this(lineLength, CHUNK_SEPARATOR);
}
/**
* Creates a Base32 codec used for decoding and encoding.
* <p>
* When encoding the line length and line separator are given in the constructor.
* </p>
* <p>
* Line lengths that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
* </p>
*
* @param lineLength
* Each line of encoded data will be at most of the given length (rounded down to nearest multiple of 8).
* If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
* @param lineSeparator
* Each line of encoded data will end with this sequence of bytes.
* @throws IllegalArgumentException
* The provided lineSeparator included some Base32 characters. That's not going to work!
*/
public Base32(int lineLength, byte[] lineSeparator) {
this(lineLength, lineSeparator, false);
}
/**
* Creates a Base32 / Base32 Hex codec used for decoding and encoding.
* <p>
* When encoding the line length and line separator are given in the constructor.
* </p>
* <p>
* Line lengths that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
* </p>
*
* @param lineLength
* Each line of encoded data will be at most of the given length (rounded down to nearest multiple of 8).
* If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
* @param lineSeparator
* Each line of encoded data will end with this sequence of bytes.
* @param useHex if <code>true</code>, then use Base32 Hex alphabet, otherwise use Base32 alphabet
* @throws IllegalArgumentException
* The provided lineSeparator included some Base32 characters. That's not going to work!
* Or the lineLength > 0 and lineSeparator is null.
*/
public Base32(int lineLength, byte[] lineSeparator, boolean useHex) {
super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK,
lineLength,
lineSeparator == null ? 0 : lineSeparator.length);
if (useHex){
this.encodeTable = HEX_ENCODE_TABLE;
this.decodeTable = HEX_DECODE_TABLE;
} else {
this.encodeTable = ENCODE_TABLE;
this.decodeTable = DECODE_TABLE;
}
if (lineLength > 0) {
if (lineSeparator == null) {
throw new IllegalArgumentException("lineLength "+lineLength+" > 0, but lineSeparator is null");
}
// Must be done after initializing the tables
if (containsAlphabetOrPad(lineSeparator)) {
String sep = StringUtils.newStringUtf8(lineSeparator);
throw new IllegalArgumentException("lineSeparator must not contain Base32 characters: [" + sep + "]");
}
this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparator.length;
this.lineSeparator = new byte[lineSeparator.length];
System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length);
} else {
this.encodeSize = BYTES_PER_ENCODED_BLOCK;
this.lineSeparator = null;
}
this.decodeSize = this.encodeSize - 1;
}
/**
* <p>
* Decodes all of the provided data, starting at inPos, for inAvail bytes. Should be called at least twice: once
* with the data to decode, and once with inAvail set to "-1" to alert decoder that EOF has been reached. The "-1"
* call is not necessary when decoding, but it doesn't hurt, either.
* </p>
* <p>
* Ignores all non-Base32 characters. This is how chunked (e.g. 76 character) data is handled, since CR and LF are
* silently ignored, but has implications for other bytes, too. This method subscribes to the garbage-in,
* garbage-out philosophy: it will not check the provided data for validity.
* </p>
*
* @param in
* byte[] array of ascii data to Base32 decode.
* @param inPos
* Position to start reading data from.
* @param inAvail
* Amount of bytes available from input for encoding.
*
* Output is written to {@link #buffer} as 8-bit octets, using {@link pos} as the buffer position
*/
void decode(byte[] in, int inPos, int inAvail) { // package protected for access from I/O streams
if (eof) {
return;
}
if (inAvail < 0) {
eof = true;
}
for (int i = 0; i < inAvail; i++) {
byte b = in[inPos++];
if (b == PAD) {
// We're done.
eof = true;
break;
} else {
ensureBufferSize(decodeSize);
if (b >= 0 && b < this.decodeTable.length) {
int result = this.decodeTable[b];
if (result >= 0) {
modulus = (modulus+1) % BYTES_PER_ENCODED_BLOCK;
bitWorkArea = (bitWorkArea << BITS_PER_ENCODED_BYTE) + result; // collect decoded bytes
if (modulus == 0) { // we can output the 5 bytes
buffer[pos++] = (byte) ((bitWorkArea >> 32) & MASK_8BITS);
buffer[pos++] = (byte) ((bitWorkArea >> 24) & MASK_8BITS);
buffer[pos++] = (byte) ((bitWorkArea >> 16) & MASK_8BITS);
buffer[pos++] = (byte) ((bitWorkArea >> 8) & MASK_8BITS);
buffer[pos++] = (byte) (bitWorkArea & MASK_8BITS);
}
}
}
}
}
// Two forms of EOF as far as Base32 decoder is concerned: actual
// EOF (-1) and first time '=' character is encountered in stream.
// This approach makes the '=' padding characters completely optional.
if (eof && modulus >= 2) { // if modulus < 2, nothing to do
ensureBufferSize(decodeSize);
// we ignore partial bytes, i.e. only multiples of 8 count
switch (modulus) {
case 2 : // 10 bits, drop 2 and output one byte
buffer[pos++] = (byte) ((bitWorkArea >> 2) & MASK_8BITS);
break;
case 3 : // 15 bits, drop 7 and output 1 byte
buffer[pos++] = (byte) ((bitWorkArea >> 7) & MASK_8BITS);
break;
case 4 : // 20 bits = 2*8 + 4
bitWorkArea = bitWorkArea >> 4; // drop 4 bits
buffer[pos++] = (byte) ((bitWorkArea >> 8) & MASK_8BITS);
buffer[pos++] = (byte) ((bitWorkArea) & MASK_8BITS);
break;
case 5 : // 25bits = 3*8 + 1
bitWorkArea = bitWorkArea >> 1;
buffer[pos++] = (byte) ((bitWorkArea >> 16) & MASK_8BITS);
buffer[pos++] = (byte) ((bitWorkArea >> 8) & MASK_8BITS);
buffer[pos++] = (byte) ((bitWorkArea) & MASK_8BITS);
break;
case 6 : // 30bits = 3*8 + 6
bitWorkArea = bitWorkArea >> 6;
buffer[pos++] = (byte) ((bitWorkArea >> 16) & MASK_8BITS);
buffer[pos++] = (byte) ((bitWorkArea >> 8) & MASK_8BITS);
buffer[pos++] = (byte) ((bitWorkArea) & MASK_8BITS);
break;
case 7 : // 35 = 4*8 +3
bitWorkArea = bitWorkArea >> 3;
buffer[pos++] = (byte) ((bitWorkArea >> 24) & MASK_8BITS);
buffer[pos++] = (byte) ((bitWorkArea >> 16) & MASK_8BITS);
buffer[pos++] = (byte) ((bitWorkArea >> 8) & MASK_8BITS);
buffer[pos++] = (byte) ((bitWorkArea) & MASK_8BITS);
break;
}
}
}
/**
* <p>
* Encodes all of the provided data, starting at inPos, for inAvail bytes. Must be called at least twice: once with
* the data to encode, and once with inAvail set to "-1" to alert encoder that EOF has been reached, so flush last
* remaining bytes (if not multiple of 5).
* </p>
*
* @param in
* byte[] array of binary data to Base32 encode.
* @param inPos
* Position to start reading data from.
* @param inAvail
* Amount of bytes available from input for encoding.
*/
void encode(byte[] in, int inPos, int inAvail) { // package protected for access from I/O streams
if (eof) {
return;
}
// inAvail < 0 is how we're informed of EOF in the underlying data we're
// encoding.
if (inAvail < 0) {
eof = true;
if (0 == modulus && lineLength == 0) {
return; // no leftovers to process and not using chunking
}
ensureBufferSize(encodeSize);
int savedPos = pos;
switch (modulus) { // % 5
case 1 : // Only 1 octet; take top 5 bits then remainder
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 3) & MASK_5BITS]; // 8-1*5 = 3
buffer[pos++] = encodeTable[(int)(bitWorkArea << 2) & MASK_5BITS]; // 5-3=2
buffer[pos++] = PAD;
buffer[pos++] = PAD;
buffer[pos++] = PAD;
buffer[pos++] = PAD;
buffer[pos++] = PAD;
buffer[pos++] = PAD;
break;
case 2 : // 2 octets = 16 bits to use
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 11) & MASK_5BITS]; // 16-1*5 = 11
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 6) & MASK_5BITS]; // 16-2*5 = 6
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 1) & MASK_5BITS]; // 16-3*5 = 1
buffer[pos++] = encodeTable[(int)(bitWorkArea << 4) & MASK_5BITS]; // 5-1 = 4
buffer[pos++] = PAD;
buffer[pos++] = PAD;
buffer[pos++] = PAD;
buffer[pos++] = PAD;
break;
case 3 : // 3 octets = 24 bits to use
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 19) & MASK_5BITS]; // 24-1*5 = 19
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 14) & MASK_5BITS]; // 24-2*5 = 14
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 9) & MASK_5BITS]; // 24-3*5 = 9
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 4) & MASK_5BITS]; // 24-4*5 = 4
buffer[pos++] = encodeTable[(int)(bitWorkArea << 1) & MASK_5BITS]; // 5-4 = 1
buffer[pos++] = PAD;
buffer[pos++] = PAD;
buffer[pos++] = PAD;
break;
case 4 : // 4 octets = 32 bits to use
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 27) & MASK_5BITS]; // 32-1*5 = 27
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 22) & MASK_5BITS]; // 32-2*5 = 22
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 17) & MASK_5BITS]; // 32-3*5 = 17
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 12) & MASK_5BITS]; // 32-4*5 = 12
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 7) & MASK_5BITS]; // 32-5*5 = 7
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 2) & MASK_5BITS]; // 32-6*5 = 2
buffer[pos++] = encodeTable[(int)(bitWorkArea << 3) & MASK_5BITS]; // 5-2 = 3
buffer[pos++] = PAD;
break;
}
currentLinePos += pos - savedPos; // keep track of current line position
// if currentPos == 0 we are at the start of a line, so don't add CRLF
if (lineLength > 0 && currentLinePos > 0){ // add chunk separator if required
System.arraycopy(lineSeparator, 0, buffer, pos, lineSeparator.length);
pos += lineSeparator.length;
}
} else {
for (int i = 0; i < inAvail; i++) {
ensureBufferSize(encodeSize);
modulus = (modulus+1) % BYTES_PER_UNENCODED_BLOCK;
int b = in[inPos++];
if (b < 0) {
b += 256;
}
bitWorkArea = (bitWorkArea << 8) + b; // BITS_PER_BYTE
if (0 == modulus) { // we have enough bytes to create our output
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 35) & MASK_5BITS];
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 30) & MASK_5BITS];
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 25) & MASK_5BITS];
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 20) & MASK_5BITS];
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 15) & MASK_5BITS];
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 10) & MASK_5BITS];
buffer[pos++] = encodeTable[(int)(bitWorkArea >> 5) & MASK_5BITS];
buffer[pos++] = encodeTable[(int)bitWorkArea & MASK_5BITS];
currentLinePos += BYTES_PER_ENCODED_BLOCK;
if (lineLength > 0 && lineLength <= currentLinePos) {
System.arraycopy(lineSeparator, 0, buffer, pos, lineSeparator.length);
pos += lineSeparator.length;
currentLinePos = 0;
}
}
}
}
}
/**
* Returns whether or not the <code>octet</code> is in the Base32 alphabet.
*
* @param octet
* The value to test
* @return <code>true</code> if the value is defined in the the Base32 alphabet <code>false</code> otherwise.
*/
public boolean isInAlphabet(byte octet) {
return octet >= 0 && octet < decodeTable.length && decodeTable[octet] != -1;
}
}

Просмотреть файл

@ -0,0 +1,85 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.binary;
import java.io.InputStream;
/**
* Provides Base32 encoding and decoding in a streaming fashion (unlimited size). When encoding the default lineLength
* is 76 characters and the default lineEnding is CRLF, but these can be overridden by using the appropriate
* constructor.
* <p>
* The default behaviour of the Base32InputStream is to DECODE, whereas the default behaviour of the Base32OutputStream
* is to ENCODE, but this behaviour can be overridden by using a different constructor.
* </p>
* <p>
* Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
* character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
* </p>
*
* @version $Revision: 1063784 $
* @see <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a>
* @since 1.5
*/
public class Base32InputStream extends BaseNCodecInputStream {
/**
* Creates a Base32InputStream such that all data read is Base32-decoded from the original provided InputStream.
*
* @param in
* InputStream to wrap.
*/
public Base32InputStream(InputStream in) {
this(in, false);
}
/**
* Creates a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original
* provided InputStream.
*
* @param in
* InputStream to wrap.
* @param doEncode
* true if we should encode all data read from us, false if we should decode.
*/
public Base32InputStream(InputStream in, boolean doEncode) {
super(in, new Base32(false), doEncode);
}
/**
* Creates a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original
* provided InputStream.
*
* @param in
* InputStream to wrap.
* @param doEncode
* true if we should encode all data read from us, false if we should decode.
* @param lineLength
* If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to
* nearest multiple of 4). If lineLength <=0, the encoded data is not divided into lines. If doEncode is
* false, lineLength is ignored.
* @param lineSeparator
* If doEncode is true, each line of encoded data will be terminated with this byte sequence (e.g. \r\n).
* If lineLength <= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
*/
public Base32InputStream(InputStream in, boolean doEncode, int lineLength, byte[] lineSeparator) {
super(in, new Base32(lineLength, lineSeparator), doEncode);
}
}

Просмотреть файл

@ -0,0 +1,85 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.binary;
import java.io.OutputStream;
/**
* Provides Base32 encoding and decoding in a streaming fashion (unlimited size). When encoding the default lineLength
* is 76 characters and the default lineEnding is CRLF, but these can be overridden by using the appropriate
* constructor.
* <p>
* The default behaviour of the Base32OutputStream is to ENCODE, whereas the default behaviour of the Base32InputStream
* is to DECODE. But this behaviour can be overridden by using a different constructor.
* </p>
* <p>
* Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
* character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
* </p>
*
* @version $Revision: 1064132 $
* @see <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a>
* @since 1.5
*/
public class Base32OutputStream extends BaseNCodecOutputStream {
/**
* Creates a Base32OutputStream such that all data written is Base32-encoded to the original provided OutputStream.
*
* @param out
* OutputStream to wrap.
*/
public Base32OutputStream(OutputStream out) {
this(out, true);
}
/**
* Creates a Base32OutputStream such that all data written is either Base32-encoded or Base32-decoded to the
* original provided OutputStream.
*
* @param out
* OutputStream to wrap.
* @param doEncode
* true if we should encode all data written to us, false if we should decode.
*/
public Base32OutputStream(OutputStream out, boolean doEncode) {
super(out, new Base32(false), doEncode);
}
/**
* Creates a Base32OutputStream such that all data written is either Base32-encoded or Base32-decoded to the
* original provided OutputStream.
*
* @param out
* OutputStream to wrap.
* @param doEncode
* true if we should encode all data written to us, false if we should decode.
* @param lineLength
* If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to
* nearest multiple of 4). If lineLength <=0, the encoded data is not divided into lines. If doEncode is
* false, lineLength is ignored.
* @param lineSeparator
* If doEncode is true, each line of encoded data will be terminated with this byte sequence (e.g. \r\n).
* If lineLength <= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
*/
public Base32OutputStream(OutputStream out, boolean doEncode, int lineLength, byte[] lineSeparator) {
super(out, new Base32(lineLength, lineSeparator), doEncode);
}
}

Просмотреть файл

@ -0,0 +1,756 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.binary;
import java.math.BigInteger;
/**
* Provides Base64 encoding and decoding as defined by <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>.
*
* <p>
* This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose
* Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein.
* </p>
* <p>
* The class can be parameterized in the following manner with various constructors:
* <ul>
* <li>URL-safe mode: Default off.</li>
* <li>Line length: Default 76. Line length that aren't multiples of 4 will still essentially end up being multiples of
* 4 in the encoded data.
* <li>Line separator: Default is CRLF ("\r\n")</li>
* </ul>
* </p>
* <p>
* Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
* character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
* </p>
* <p>
* This class is not thread-safe. Each thread should use its own instance.
* </p>
*
* @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
* @author Apache Software Foundation
* @since 1.0
* @version $Revision: 1080712 $
*/
public class Base64 extends BaseNCodec {
/**
* BASE32 characters are 6 bits in length.
* They are formed by taking a block of 3 octets to form a 24-bit string,
* which is converted into 4 BASE64 characters.
*/
private static final int BITS_PER_ENCODED_BYTE = 6;
private static final int BYTES_PER_UNENCODED_BLOCK = 3;
private static final int BYTES_PER_ENCODED_BLOCK = 4;
/**
* Chunk separator per RFC 2045 section 2.1.
*
* <p>
* N.B. The next major release may break compatibility and make this field private.
* </p>
*
* @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 2.1</a>
*/
static final byte[] CHUNK_SEPARATOR = {'\r', '\n'};
/**
* This array is a lookup table that translates 6-bit positive integer index values into their "Base64 Alphabet"
* equivalents as specified in Table 1 of RFC 2045.
*
* Thanks to "commons" project in ws.apache.org for this code.
* http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
*/
private static final byte[] STANDARD_ENCODE_TABLE = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
};
/**
* This is a copy of the STANDARD_ENCODE_TABLE above, but with + and /
* changed to - and _ to make the encoded Base64 results more URL-SAFE.
* This table is only used when the Base64's mode is set to URL-SAFE.
*/
private static final byte[] URL_SAFE_ENCODE_TABLE = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
};
/**
* This array is a lookup table that translates Unicode characters drawn from the "Base64 Alphabet" (as specified in
* Table 1 of RFC 2045) into their 6-bit positive integer equivalents. Characters that are not in the Base64
* alphabet but fall within the bounds of the array are translated to -1.
*
* Note: '+' and '-' both decode to 62. '/' and '_' both decode to 63. This means decoder seamlessly handles both
* URL_SAFE and STANDARD base64. (The encoder, on the other hand, needs to know ahead of time what to emit).
*
* Thanks to "commons" project in ws.apache.org for this code.
* http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
*/
private static final byte[] DECODE_TABLE = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, -1, -1, -1, -1, 63, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34,
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
};
/**
* Base64 uses 6-bit fields.
*/
/** Mask used to extract 6 bits, used when encoding */
private static final int MASK_6BITS = 0x3f;
// The static final fields above are used for the original static byte[] methods on Base64.
// The private member fields below are used with the new streaming approach, which requires
// some state be preserved between calls of encode() and decode().
/**
* Encode table to use: either STANDARD or URL_SAFE. Note: the DECODE_TABLE above remains static because it is able
* to decode both STANDARD and URL_SAFE streams, but the encodeTable must be a member variable so we can switch
* between the two modes.
*/
private final byte[] encodeTable;
// Only one decode table currently; keep for consistency with Base32 code
private final byte[] decodeTable = DECODE_TABLE;
/**
* Line separator for encoding. Not used when decoding. Only used if lineLength > 0.
*/
private final byte[] lineSeparator;
/**
* Convenience variable to help us determine when our buffer is going to run out of room and needs resizing.
* <code>decodeSize = 3 + lineSeparator.length;</code>
*/
private final int decodeSize;
/**
* Convenience variable to help us determine when our buffer is going to run out of room and needs resizing.
* <code>encodeSize = 4 + lineSeparator.length;</code>
*/
private final int encodeSize;
/**
* Place holder for the bytes we're dealing with for our based logic.
* Bitwise operations store and extract the encoding or decoding from this variable.
*/
private int bitWorkArea;
/**
* Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
* <p>
* When encoding the line length is 0 (no chunking), and the encoding table is STANDARD_ENCODE_TABLE.
* </p>
*
* <p>
* When decoding all variants are supported.
* </p>
*/
public Base64() {
this(0);
}
/**
* Creates a Base64 codec used for decoding (all modes) and encoding in the given URL-safe mode.
* <p>
* When encoding the line length is 76, the line separator is CRLF, and the encoding table is STANDARD_ENCODE_TABLE.
* </p>
*
* <p>
* When decoding all variants are supported.
* </p>
*
* @param urlSafe
* if <code>true</code>, URL-safe encoding is used. In most cases this should be set to
* <code>false</code>.
* @since 1.4
*/
public Base64(boolean urlSafe) {
this(MIME_CHUNK_SIZE, CHUNK_SEPARATOR, urlSafe);
}
/**
* Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
* <p>
* When encoding the line length is given in the constructor, the line separator is CRLF, and the encoding table is
* STANDARD_ENCODE_TABLE.
* </p>
* <p>
* Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
* </p>
* <p>
* When decoding all variants are supported.
* </p>
*
* @param lineLength
* Each line of encoded data will be at most of the given length (rounded down to nearest multiple of 4).
* If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
* @since 1.4
*/
public Base64(int lineLength) {
this(lineLength, CHUNK_SEPARATOR);
}
/**
* Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
* <p>
* When encoding the line length and line separator are given in the constructor, and the encoding table is
* STANDARD_ENCODE_TABLE.
* </p>
* <p>
* Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
* </p>
* <p>
* When decoding all variants are supported.
* </p>
*
* @param lineLength
* Each line of encoded data will be at most of the given length (rounded down to nearest multiple of 4).
* If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
* @param lineSeparator
* Each line of encoded data will end with this sequence of bytes.
* @throws IllegalArgumentException
* Thrown when the provided lineSeparator included some base64 characters.
* @since 1.4
*/
public Base64(int lineLength, byte[] lineSeparator) {
this(lineLength, lineSeparator, false);
}
/**
* Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
* <p>
* When encoding the line length and line separator are given in the constructor, and the encoding table is
* STANDARD_ENCODE_TABLE.
* </p>
* <p>
* Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
* </p>
* <p>
* When decoding all variants are supported.
* </p>
*
* @param lineLength
* Each line of encoded data will be at most of the given length (rounded down to nearest multiple of 4).
* If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
* @param lineSeparator
* Each line of encoded data will end with this sequence of bytes.
* @param urlSafe
* Instead of emitting '+' and '/' we emit '-' and '_' respectively. urlSafe is only applied to encode
* operations. Decoding seamlessly handles both modes.
* @throws IllegalArgumentException
* The provided lineSeparator included some base64 characters. That's not going to work!
* @since 1.4
*/
public Base64(int lineLength, byte[] lineSeparator, boolean urlSafe) {
super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK,
lineLength,
lineSeparator == null ? 0 : lineSeparator.length);
// TODO could be simplified if there is no requirement to reject invalid line sep when length <=0
// @see test case Base64Test.testConstructors()
if (lineSeparator != null) {
if (containsAlphabetOrPad(lineSeparator)) {
String sep = StringUtils.newStringUtf8(lineSeparator);
throw new IllegalArgumentException("lineSeparator must not contain base64 characters: [" + sep + "]");
}
if (lineLength > 0){ // null line-sep forces no chunking rather than throwing IAE
this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparator.length;
this.lineSeparator = new byte[lineSeparator.length];
System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length);
} else {
this.encodeSize = BYTES_PER_ENCODED_BLOCK;
this.lineSeparator = null;
}
} else {
this.encodeSize = BYTES_PER_ENCODED_BLOCK;
this.lineSeparator = null;
}
this.decodeSize = this.encodeSize - 1;
this.encodeTable = urlSafe ? URL_SAFE_ENCODE_TABLE : STANDARD_ENCODE_TABLE;
}
/**
* Returns our current encode mode. True if we're URL-SAFE, false otherwise.
*
* @return true if we're in URL-SAFE mode, false otherwise.
* @since 1.4
*/
public boolean isUrlSafe() {
return this.encodeTable == URL_SAFE_ENCODE_TABLE;
}
/**
* <p>
* Encodes all of the provided data, starting at inPos, for inAvail bytes. Must be called at least twice: once with
* the data to encode, and once with inAvail set to "-1" to alert encoder that EOF has been reached, so flush last
* remaining bytes (if not multiple of 3).
* </p>
* <p>
* Thanks to "commons" project in ws.apache.org for the bitwise operations, and general approach.
* http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
* </p>
*
* @param in
* byte[] array of binary data to base64 encode.
* @param inPos
* Position to start reading data from.
* @param inAvail
* Amount of bytes available from input for encoding.
*/
void encode(byte[] in, int inPos, int inAvail) {
if (eof) {
return;
}
// inAvail < 0 is how we're informed of EOF in the underlying data we're
// encoding.
if (inAvail < 0) {
eof = true;
if (0 == modulus && lineLength == 0) {
return; // no leftovers to process and not using chunking
}
ensureBufferSize(encodeSize);
int savedPos = pos;
switch (modulus) { // 0-2
case 1 : // 8 bits = 6 + 2
buffer[pos++] = encodeTable[(bitWorkArea >> 2) & MASK_6BITS]; // top 6 bits
buffer[pos++] = encodeTable[(bitWorkArea << 4) & MASK_6BITS]; // remaining 2
// URL-SAFE skips the padding to further reduce size.
if (encodeTable == STANDARD_ENCODE_TABLE) {
buffer[pos++] = PAD;
buffer[pos++] = PAD;
}
break;
case 2 : // 16 bits = 6 + 6 + 4
buffer[pos++] = encodeTable[(bitWorkArea >> 10) & MASK_6BITS];
buffer[pos++] = encodeTable[(bitWorkArea >> 4) & MASK_6BITS];
buffer[pos++] = encodeTable[(bitWorkArea << 2) & MASK_6BITS];
// URL-SAFE skips the padding to further reduce size.
if (encodeTable == STANDARD_ENCODE_TABLE) {
buffer[pos++] = PAD;
}
break;
}
currentLinePos += pos - savedPos; // keep track of current line position
// if currentPos == 0 we are at the start of a line, so don't add CRLF
if (lineLength > 0 && currentLinePos > 0) {
System.arraycopy(lineSeparator, 0, buffer, pos, lineSeparator.length);
pos += lineSeparator.length;
}
} else {
for (int i = 0; i < inAvail; i++) {
ensureBufferSize(encodeSize);
modulus = (modulus+1) % BYTES_PER_UNENCODED_BLOCK;
int b = in[inPos++];
if (b < 0) {
b += 256;
}
bitWorkArea = (bitWorkArea << 8) + b; // BITS_PER_BYTE
if (0 == modulus) { // 3 bytes = 24 bits = 4 * 6 bits to extract
buffer[pos++] = encodeTable[(bitWorkArea >> 18) & MASK_6BITS];
buffer[pos++] = encodeTable[(bitWorkArea >> 12) & MASK_6BITS];
buffer[pos++] = encodeTable[(bitWorkArea >> 6) & MASK_6BITS];
buffer[pos++] = encodeTable[bitWorkArea & MASK_6BITS];
currentLinePos += BYTES_PER_ENCODED_BLOCK;
if (lineLength > 0 && lineLength <= currentLinePos) {
System.arraycopy(lineSeparator, 0, buffer, pos, lineSeparator.length);
pos += lineSeparator.length;
currentLinePos = 0;
}
}
}
}
}
/**
* <p>
* Decodes all of the provided data, starting at inPos, for inAvail bytes. Should be called at least twice: once
* with the data to decode, and once with inAvail set to "-1" to alert decoder that EOF has been reached. The "-1"
* call is not necessary when decoding, but it doesn't hurt, either.
* </p>
* <p>
* Ignores all non-base64 characters. This is how chunked (e.g. 76 character) data is handled, since CR and LF are
* silently ignored, but has implications for other bytes, too. This method subscribes to the garbage-in,
* garbage-out philosophy: it will not check the provided data for validity.
* </p>
* <p>
* Thanks to "commons" project in ws.apache.org for the bitwise operations, and general approach.
* http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
* </p>
*
* @param in
* byte[] array of ascii data to base64 decode.
* @param inPos
* Position to start reading data from.
* @param inAvail
* Amount of bytes available from input for encoding.
*/
void decode(byte[] in, int inPos, int inAvail) {
if (eof) {
return;
}
if (inAvail < 0) {
eof = true;
}
for (int i = 0; i < inAvail; i++) {
ensureBufferSize(decodeSize);
byte b = in[inPos++];
if (b == PAD) {
// We're done.
eof = true;
break;
} else {
if (b >= 0 && b < DECODE_TABLE.length) {
int result = DECODE_TABLE[b];
if (result >= 0) {
modulus = (modulus+1) % BYTES_PER_ENCODED_BLOCK;
bitWorkArea = (bitWorkArea << BITS_PER_ENCODED_BYTE) + result;
if (modulus == 0) {
buffer[pos++] = (byte) ((bitWorkArea >> 16) & MASK_8BITS);
buffer[pos++] = (byte) ((bitWorkArea >> 8) & MASK_8BITS);
buffer[pos++] = (byte) (bitWorkArea & MASK_8BITS);
}
}
}
}
}
// Two forms of EOF as far as base64 decoder is concerned: actual
// EOF (-1) and first time '=' character is encountered in stream.
// This approach makes the '=' padding characters completely optional.
if (eof && modulus != 0) {
ensureBufferSize(decodeSize);
// We have some spare bits remaining
// Output all whole multiples of 8 bits and ignore the rest
switch (modulus) {
// case 1: // 6 bits - ignore entirely
// break;
case 2 : // 12 bits = 8 + 4
bitWorkArea = bitWorkArea >> 4; // dump the extra 4 bits
buffer[pos++] = (byte) ((bitWorkArea) & MASK_8BITS);
break;
case 3 : // 18 bits = 8 + 8 + 2
bitWorkArea = bitWorkArea >> 2; // dump 2 bits
buffer[pos++] = (byte) ((bitWorkArea >> 8) & MASK_8BITS);
buffer[pos++] = (byte) ((bitWorkArea) & MASK_8BITS);
break;
}
}
}
/**
* Returns whether or not the <code>octet</code> is in the base 64 alphabet.
*
* @param octet
* The value to test
* @return <code>true</code> if the value is defined in the the base 64 alphabet, <code>false</code> otherwise.
* @since 1.4
*/
public static boolean isBase64(byte octet) {
return octet == PAD_DEFAULT || (octet >= 0 && octet < DECODE_TABLE.length && DECODE_TABLE[octet] != -1);
}
/**
* Tests a given String to see if it contains only valid characters within the Base64 alphabet. Currently the
* method treats whitespace as valid.
*
* @param base64
* String to test
* @return <code>true</code> if all characters in the String are valid characters in the Base64 alphabet or if
* the String is empty; <code>false</code>, otherwise
* @since 1.5
*/
public static boolean isBase64(String base64) {
return isBase64(StringUtils.getBytesUtf8(base64));
}
/**
* Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently the
* method treats whitespace as valid.
*
* @param arrayOctet
* byte array to test
* @return <code>true</code> if all bytes are valid characters in the Base64 alphabet or if the byte array is empty;
* <code>false</code>, otherwise
* @deprecated 1.5 Use {@link #isBase64(byte[])}, will be removed in 2.0.
*/
public static boolean isArrayByteBase64(byte[] arrayOctet) {
return isBase64(arrayOctet);
}
/**
* Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently the
* method treats whitespace as valid.
*
* @param arrayOctet
* byte array to test
* @return <code>true</code> if all bytes are valid characters in the Base64 alphabet or if the byte array is empty;
* <code>false</code>, otherwise
* @since 1.5
*/
public static boolean isBase64(byte[] arrayOctet) {
for (int i = 0; i < arrayOctet.length; i++) {
if (!isBase64(arrayOctet[i]) && !isWhiteSpace(arrayOctet[i])) {
return false;
}
}
return true;
}
/**
* Encodes binary data using the base64 algorithm but does not chunk the output.
*
* @param binaryData
* binary data to encode
* @return byte[] containing Base64 characters in their UTF-8 representation.
*/
public static byte[] encodeBase64(byte[] binaryData) {
return encodeBase64(binaryData, false);
}
/**
* Encodes binary data using the base64 algorithm but does not chunk the output.
*
* NOTE: We changed the behaviour of this method from multi-line chunking (commons-codec-1.4) to
* single-line non-chunking (commons-codec-1.5).
*
* @param binaryData
* binary data to encode
* @return String containing Base64 characters.
* @since 1.4 (NOTE: 1.4 chunked the output, whereas 1.5 does not).
*/
public static String encodeBase64String(byte[] binaryData) {
return StringUtils.newStringUtf8(encodeBase64(binaryData, false));
}
/**
* Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The
* url-safe variation emits - and _ instead of + and / characters.
*
* @param binaryData
* binary data to encode
* @return byte[] containing Base64 characters in their UTF-8 representation.
* @since 1.4
*/
public static byte[] encodeBase64URLSafe(byte[] binaryData) {
return encodeBase64(binaryData, false, true);
}
/**
* Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The
* url-safe variation emits - and _ instead of + and / characters.
*
* @param binaryData
* binary data to encode
* @return String containing Base64 characters
* @since 1.4
*/
public static String encodeBase64URLSafeString(byte[] binaryData) {
return StringUtils.newStringUtf8(encodeBase64(binaryData, false, true));
}
/**
* Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks
*
* @param binaryData
* binary data to encode
* @return Base64 characters chunked in 76 character blocks
*/
public static byte[] encodeBase64Chunked(byte[] binaryData) {
return encodeBase64(binaryData, true);
}
/**
* Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
*
* @param binaryData
* Array containing binary data to encode.
* @param isChunked
* if <code>true</code> this encoder will chunk the base64 output into 76 character blocks
* @return Base64-encoded data.
* @throws IllegalArgumentException
* Thrown when the input array needs an output array bigger than {@link Integer#MAX_VALUE}
*/
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) {
return encodeBase64(binaryData, isChunked, false);
}
/**
* Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
*
* @param binaryData
* Array containing binary data to encode.
* @param isChunked
* if <code>true</code> this encoder will chunk the base64 output into 76 character blocks
* @param urlSafe
* if <code>true</code> this encoder will emit - and _ instead of the usual + and / characters.
* @return Base64-encoded data.
* @throws IllegalArgumentException
* Thrown when the input array needs an output array bigger than {@link Integer#MAX_VALUE}
* @since 1.4
*/
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe) {
return encodeBase64(binaryData, isChunked, urlSafe, Integer.MAX_VALUE);
}
/**
* Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
*
* @param binaryData
* Array containing binary data to encode.
* @param isChunked
* if <code>true</code> this encoder will chunk the base64 output into 76 character blocks
* @param urlSafe
* if <code>true</code> this encoder will emit - and _ instead of the usual + and / characters.
* @param maxResultSize
* The maximum result size to accept.
* @return Base64-encoded data.
* @throws IllegalArgumentException
* Thrown when the input array needs an output array bigger than maxResultSize
* @since 1.4
*/
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe, int maxResultSize) {
if (binaryData == null || binaryData.length == 0) {
return binaryData;
}
// Create this so can use the super-class method
// Also ensures that the same roundings are performed by the ctor and the code
Base64 b64 = isChunked ? new Base64(urlSafe) : new Base64(0, CHUNK_SEPARATOR, urlSafe);
long len = b64.getEncodedLength(binaryData);
if (len > maxResultSize) {
throw new IllegalArgumentException("Input array too big, the output array would be bigger (" +
len +
") than the specified maximum size of " +
maxResultSize);
}
return b64.encode(binaryData);
}
/**
* Decodes a Base64 String into octets
*
* @param base64String
* String containing Base64 data
* @return Array containing decoded data.
* @since 1.4
*/
public static byte[] decodeBase64(String base64String) {
return new Base64().decode(base64String);
}
/**
* Decodes Base64 data into octets
*
* @param base64Data
* Byte array containing Base64 data
* @return Array containing decoded data.
*/
public static byte[] decodeBase64(byte[] base64Data) {
return new Base64().decode(base64Data);
}
// Implementation of the Encoder Interface
// Implementation of integer encoding used for crypto
/**
* Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signature
*
* @param pArray
* a byte array containing base64 character data
* @return A BigInteger
* @since 1.4
*/
public static BigInteger decodeInteger(byte[] pArray) {
return new BigInteger(1, decodeBase64(pArray));
}
/**
* Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature
*
* @param bigInt
* a BigInteger
* @return A byte array containing base64 character data
* @throws NullPointerException
* if null is passed in
* @since 1.4
*/
public static byte[] encodeInteger(BigInteger bigInt) {
if (bigInt == null) {
throw new NullPointerException("encodeInteger called with null parameter");
}
return encodeBase64(toIntegerBytes(bigInt), false);
}
/**
* Returns a byte-array representation of a <code>BigInteger</code> without sign bit.
*
* @param bigInt
* <code>BigInteger</code> to be converted
* @return a byte array representation of the BigInteger parameter
*/
static byte[] toIntegerBytes(BigInteger bigInt) {
int bitlen = bigInt.bitLength();
// round bitlen
bitlen = ((bitlen + 7) >> 3) << 3;
byte[] bigBytes = bigInt.toByteArray();
if (((bigInt.bitLength() % 8) != 0) && (((bigInt.bitLength() / 8) + 1) == (bitlen / 8))) {
return bigBytes;
}
// set up params for copying everything but sign bit
int startSrc = 0;
int len = bigBytes.length;
// if bigInt is exactly byte-aligned, just skip signbit in copy
if ((bigInt.bitLength() % 8) == 0) {
startSrc = 1;
len--;
}
int startDst = bitlen / 8 - len; // to pad w/ nulls as per spec
byte[] resizedBytes = new byte[bitlen / 8];
System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, len);
return resizedBytes;
}
/**
* Returns whether or not the <code>octet</code> is in the Base32 alphabet.
*
* @param octet
* The value to test
* @return <code>true</code> if the value is defined in the the Base32 alphabet <code>false</code> otherwise.
*/
protected boolean isInAlphabet(byte octet) {
return octet >= 0 && octet < decodeTable.length && decodeTable[octet] != -1;
}
}

Просмотреть файл

@ -0,0 +1,89 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.binary;
import java.io.InputStream;
/**
* Provides Base64 encoding and decoding in a streaming fashion (unlimited size). When encoding the default lineLength
* is 76 characters and the default lineEnding is CRLF, but these can be overridden by using the appropriate
* constructor.
* <p>
* The default behaviour of the Base64InputStream is to DECODE, whereas the default behaviour of the Base64OutputStream
* is to ENCODE, but this behaviour can be overridden by using a different constructor.
* </p>
* <p>
* This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose
* Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein.
* </p>
* <p>
* Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
* character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
* </p>
*
* @author Apache Software Foundation
* @version $Id: Base64InputStream.java 1064424 2011-01-28 02:02:46Z sebb $
* @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
* @since 1.4
*/
public class Base64InputStream extends BaseNCodecInputStream {
/**
* Creates a Base64InputStream such that all data read is Base64-decoded from the original provided InputStream.
*
* @param in
* InputStream to wrap.
*/
public Base64InputStream(InputStream in) {
this(in, false);
}
/**
* Creates a Base64InputStream such that all data read is either Base64-encoded or Base64-decoded from the original
* provided InputStream.
*
* @param in
* InputStream to wrap.
* @param doEncode
* true if we should encode all data read from us, false if we should decode.
*/
public Base64InputStream(InputStream in, boolean doEncode) {
super(in, new Base64(false), doEncode);
}
/**
* Creates a Base64InputStream such that all data read is either Base64-encoded or Base64-decoded from the original
* provided InputStream.
*
* @param in
* InputStream to wrap.
* @param doEncode
* true if we should encode all data read from us, false if we should decode.
* @param lineLength
* If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to
* nearest multiple of 4). If lineLength <=0, the encoded data is not divided into lines. If doEncode is
* false, lineLength is ignored.
* @param lineSeparator
* If doEncode is true, each line of encoded data will be terminated with this byte sequence (e.g. \r\n).
* If lineLength <= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
*/
public Base64InputStream(InputStream in, boolean doEncode, int lineLength, byte[] lineSeparator) {
super(in, new Base64(lineLength, lineSeparator), doEncode);
}
}

Просмотреть файл

@ -0,0 +1,89 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.binary;
import java.io.OutputStream;
/**
* Provides Base64 encoding and decoding in a streaming fashion (unlimited size). When encoding the default lineLength
* is 76 characters and the default lineEnding is CRLF, but these can be overridden by using the appropriate
* constructor.
* <p>
* The default behaviour of the Base64OutputStream is to ENCODE, whereas the default behaviour of the Base64InputStream
* is to DECODE. But this behaviour can be overridden by using a different constructor.
* </p>
* <p>
* This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose
* Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein.
* </p>
* <p>
* Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
* character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
* </p>
*
* @author Apache Software Foundation
* @version $Id: Base64OutputStream.java 1064424 2011-01-28 02:02:46Z sebb $
* @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
* @since 1.4
*/
public class Base64OutputStream extends BaseNCodecOutputStream {
/**
* Creates a Base64OutputStream such that all data written is Base64-encoded to the original provided OutputStream.
*
* @param out
* OutputStream to wrap.
*/
public Base64OutputStream(OutputStream out) {
this(out, true);
}
/**
* Creates a Base64OutputStream such that all data written is either Base64-encoded or Base64-decoded to the
* original provided OutputStream.
*
* @param out
* OutputStream to wrap.
* @param doEncode
* true if we should encode all data written to us, false if we should decode.
*/
public Base64OutputStream(OutputStream out, boolean doEncode) {
super(out,new Base64(false), doEncode);
}
/**
* Creates a Base64OutputStream such that all data written is either Base64-encoded or Base64-decoded to the
* original provided OutputStream.
*
* @param out
* OutputStream to wrap.
* @param doEncode
* true if we should encode all data written to us, false if we should decode.
* @param lineLength
* If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to
* nearest multiple of 4). If lineLength <=0, the encoded data is not divided into lines. If doEncode is
* false, lineLength is ignored.
* @param lineSeparator
* If doEncode is true, each line of encoded data will be terminated with this byte sequence (e.g. \r\n).
* If lineLength <= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored.
*/
public Base64OutputStream(OutputStream out, boolean doEncode, int lineLength, byte[] lineSeparator) {
super(out, new Base64(lineLength, lineSeparator), doEncode);
}
}

Просмотреть файл

@ -0,0 +1,445 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.binary;
import org.mozilla.apache.commons.codec.BinaryDecoder;
import org.mozilla.apache.commons.codec.BinaryEncoder;
import org.mozilla.apache.commons.codec.DecoderException;
import org.mozilla.apache.commons.codec.EncoderException;
/**
* Abstract superclass for Base-N encoders and decoders.
*
* <p>
* This class is not thread-safe.
* Each thread should use its own instance.
* </p>
*/
public abstract class BaseNCodec implements BinaryEncoder, BinaryDecoder {
/**
* MIME chunk size per RFC 2045 section 6.8.
*
* <p>
* The {@value} character limit does not count the trailing CRLF, but counts all other characters, including any
* equal signs.
* </p>
*
* @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 6.8</a>
*/
public static final int MIME_CHUNK_SIZE = 76;
/**
* PEM chunk size per RFC 1421 section 4.3.2.4.
*
* <p>
* The {@value} character limit does not count the trailing CRLF, but counts all other characters, including any
* equal signs.
* </p>
*
* @see <a href="http://tools.ietf.org/html/rfc1421">RFC 1421 section 4.3.2.4</a>
*/
public static final int PEM_CHUNK_SIZE = 64;
private static final int DEFAULT_BUFFER_RESIZE_FACTOR = 2;
/**
* Defines the default buffer size - currently {@value}
* - must be large enough for at least one encoded block+separator
*/
private static final int DEFAULT_BUFFER_SIZE = 8192;
/** Mask used to extract 8 bits, used in decoding bytes */
protected static final int MASK_8BITS = 0xff;
/**
* Byte used to pad output.
*/
protected static final byte PAD_DEFAULT = '='; // Allow static access to default
protected final byte PAD = PAD_DEFAULT; // instance variable just in case it needs to vary later
/** Number of bytes in each full block of unencoded data, e.g. 4 for Base64 and 5 for Base32 */
private final int unencodedBlockSize;
/** Number of bytes in each full block of encoded data, e.g. 3 for Base64 and 8 for Base32 */
private final int encodedBlockSize;
/**
* Chunksize for encoding. Not used when decoding.
* A value of zero or less implies no chunking of the encoded data.
* Rounded down to nearest multiple of encodedBlockSize.
*/
protected final int lineLength;
/**
* Size of chunk separator. Not used unless {@link #lineLength} > 0.
*/
private final int chunkSeparatorLength;
/**
* Buffer for streaming.
*/
protected byte[] buffer;
/**
* Position where next character should be written in the buffer.
*/
protected int pos;
/**
* Position where next character should be read from the buffer.
*/
private int readPos;
/**
* Boolean flag to indicate the EOF has been reached. Once EOF has been reached, this object becomes useless,
* and must be thrown away.
*/
protected boolean eof;
/**
* Variable tracks how many characters have been written to the current line. Only used when encoding. We use it to
* make sure each encoded line never goes beyond lineLength (if lineLength > 0).
*/
protected int currentLinePos;
/**
* Writes to the buffer only occur after every 3/5 reads when encoding, and every 4/8 reads when decoding.
* This variable helps track that.
*/
protected int modulus;
/**
* Note <code>lineLength</code> is rounded down to the nearest multiple of {@link #encodedBlockSize}
* If <code>chunkSeparatorLength</code> is zero, then chunking is disabled.
* @param unencodedBlockSize the size of an unencoded block (e.g. Base64 = 3)
* @param encodedBlockSize the size of an encoded block (e.g. Base64 = 4)
* @param lineLength if &gt; 0, use chunking with a length <code>lineLength</code>
* @param chunkSeparatorLength the chunk separator length, if relevant
*/
protected BaseNCodec(int unencodedBlockSize, int encodedBlockSize, int lineLength, int chunkSeparatorLength){
this.unencodedBlockSize = unencodedBlockSize;
this.encodedBlockSize = encodedBlockSize;
this.lineLength = (lineLength > 0 && chunkSeparatorLength > 0) ? (lineLength / encodedBlockSize) * encodedBlockSize : 0;
this.chunkSeparatorLength = chunkSeparatorLength;
}
/**
* Returns true if this object has buffered data for reading.
*
* @return true if there is data still available for reading.
*/
boolean hasData() { // package protected for access from I/O streams
return this.buffer != null;
}
/**
* Returns the amount of buffered data available for reading.
*
* @return The amount of buffered data available for reading.
*/
int available() { // package protected for access from I/O streams
return buffer != null ? pos - readPos : 0;
}
/**
* Get the default buffer size. Can be overridden.
*
* @return {@link #DEFAULT_BUFFER_SIZE}
*/
protected int getDefaultBufferSize() {
return DEFAULT_BUFFER_SIZE;
}
/** Increases our buffer by the {@link #DEFAULT_BUFFER_RESIZE_FACTOR}. */
private void resizeBuffer() {
if (buffer == null) {
buffer = new byte[getDefaultBufferSize()];
pos = 0;
readPos = 0;
} else {
byte[] b = new byte[buffer.length * DEFAULT_BUFFER_RESIZE_FACTOR];
System.arraycopy(buffer, 0, b, 0, buffer.length);
buffer = b;
}
}
/**
* Ensure that the buffer has room for <code>size</code> bytes
*
* @param size minimum spare space required
*/
protected void ensureBufferSize(int size){
if ((buffer == null) || (buffer.length < pos + size)){
resizeBuffer();
}
}
/**
* Extracts buffered data into the provided byte[] array, starting at position bPos,
* up to a maximum of bAvail bytes. Returns how many bytes were actually extracted.
*
* @param b
* byte[] array to extract the buffered data into.
* @param bPos
* position in byte[] array to start extraction at.
* @param bAvail
* amount of bytes we're allowed to extract. We may extract fewer (if fewer are available).
* @return The number of bytes successfully extracted into the provided byte[] array.
*/
int readResults(byte[] b, int bPos, int bAvail) { // package protected for access from I/O streams
if (buffer != null) {
int len = Math.min(available(), bAvail);
System.arraycopy(buffer, readPos, b, bPos, len);
readPos += len;
if (readPos >= pos) {
buffer = null; // so hasData() will return false, and this method can return -1
}
return len;
}
return eof ? -1 : 0;
}
/**
* Checks if a byte value is whitespace or not.
* Whitespace is taken to mean: space, tab, CR, LF
* @param byteToCheck
* the byte to check
* @return true if byte is whitespace, false otherwise
*/
protected static boolean isWhiteSpace(byte byteToCheck) {
switch (byteToCheck) {
case ' ' :
case '\n' :
case '\r' :
case '\t' :
return true;
default :
return false;
}
}
/**
* Resets this object to its initial newly constructed state.
*/
private void reset() {
buffer = null;
pos = 0;
readPos = 0;
currentLinePos = 0;
modulus = 0;
eof = false;
}
/**
* Encodes an Object using the Base-N algorithm. This method is provided in order to satisfy the requirements of the
* Encoder interface, and will throw an EncoderException if the supplied object is not of type byte[].
*
* @param pObject
* Object to encode
* @return An object (of type byte[]) containing the Base-N encoded data which corresponds to the byte[] supplied.
* @throws EncoderException
* if the parameter supplied is not of type byte[]
*/
public Object encode(Object pObject) throws EncoderException {
if (!(pObject instanceof byte[])) {
throw new EncoderException("Parameter supplied to Base-N encode is not a byte[]");
}
return encode((byte[]) pObject);
}
/**
* Encodes a byte[] containing binary data, into a String containing characters in the Base-N alphabet.
*
* @param pArray
* a byte array containing binary data
* @return A String containing only Base-N character data
*/
public String encodeToString(byte[] pArray) {
return StringUtils.newStringUtf8(encode(pArray));
}
/**
* Decodes an Object using the Base-N algorithm. This method is provided in order to satisfy the requirements of the
* Decoder interface, and will throw a DecoderException if the supplied object is not of type byte[] or String.
*
* @param pObject
* Object to decode
* @return An object (of type byte[]) containing the binary data which corresponds to the byte[] or String supplied.
* @throws DecoderException
* if the parameter supplied is not of type byte[]
*/
public Object decode(Object pObject) throws DecoderException {
if (pObject instanceof byte[]) {
return decode((byte[]) pObject);
} else if (pObject instanceof String) {
return decode((String) pObject);
} else {
throw new DecoderException("Parameter supplied to Base-N decode is not a byte[] or a String");
}
}
/**
* Decodes a String containing characters in the Base-N alphabet.
*
* @param pArray
* A String containing Base-N character data
* @return a byte array containing binary data
*/
public byte[] decode(String pArray) {
return decode(StringUtils.getBytesUtf8(pArray));
}
/**
* Decodes a byte[] containing characters in the Base-N alphabet.
*
* @param pArray
* A byte array containing Base-N character data
* @return a byte array containing binary data
*/
public byte[] decode(byte[] pArray) {
reset();
if (pArray == null || pArray.length == 0) {
return pArray;
}
decode(pArray, 0, pArray.length);
decode(pArray, 0, -1); // Notify decoder of EOF.
byte[] result = new byte[pos];
readResults(result, 0, result.length);
return result;
}
/**
* Encodes a byte[] containing binary data, into a byte[] containing characters in the alphabet.
*
* @param pArray
* a byte array containing binary data
* @return A byte array containing only the basen alphabetic character data
*/
public byte[] encode(byte[] pArray) {
reset();
if (pArray == null || pArray.length == 0) {
return pArray;
}
encode(pArray, 0, pArray.length);
encode(pArray, 0, -1); // Notify encoder of EOF.
byte[] buf = new byte[pos - readPos];
readResults(buf, 0, buf.length);
return buf;
}
/**
* Encodes a byte[] containing binary data, into a String containing characters in the appropriate alphabet.
* Uses UTF8 encoding.
*
* @param pArray a byte array containing binary data
* @return String containing only character data in the appropriate alphabet.
*/
public String encodeAsString(byte[] pArray){
return StringUtils.newStringUtf8(encode(pArray));
}
abstract void encode(byte[] pArray, int i, int length); // package protected for access from I/O streams
abstract void decode(byte[] pArray, int i, int length); // package protected for access from I/O streams
/**
* Returns whether or not the <code>octet</code> is in the current alphabet.
* Does not allow whitespace or pad.
*
* @param value The value to test
*
* @return <code>true</code> if the value is defined in the current alphabet, <code>false</code> otherwise.
*/
protected abstract boolean isInAlphabet(byte value);
/**
* Tests a given byte array to see if it contains only valid characters within the alphabet.
* The method optionally treats whitespace and pad as valid.
*
* @param arrayOctet byte array to test
* @param allowWSPad if <code>true</code>, then whitespace and PAD are also allowed
*
* @return <code>true</code> if all bytes are valid characters in the alphabet or if the byte array is empty;
* <code>false</code>, otherwise
*/
public boolean isInAlphabet(byte[] arrayOctet, boolean allowWSPad) {
for (int i = 0; i < arrayOctet.length; i++) {
if (!isInAlphabet(arrayOctet[i]) &&
(!allowWSPad || (arrayOctet[i] != PAD) && !isWhiteSpace(arrayOctet[i]))) {
return false;
}
}
return true;
}
/**
* Tests a given String to see if it contains only valid characters within the alphabet.
* The method treats whitespace and PAD as valid.
*
* @param basen String to test
* @return <code>true</code> if all characters in the String are valid characters in the alphabet or if
* the String is empty; <code>false</code>, otherwise
* @see #isInAlphabet(byte[], boolean)
*/
public boolean isInAlphabet(String basen) {
return isInAlphabet(StringUtils.getBytesUtf8(basen), true);
}
/**
* Tests a given byte array to see if it contains any characters within the alphabet or PAD.
*
* Intended for use in checking line-ending arrays
*
* @param arrayOctet
* byte array to test
* @return <code>true</code> if any byte is a valid character in the alphabet or PAD; <code>false</code> otherwise
*/
protected boolean containsAlphabetOrPad(byte[] arrayOctet) {
if (arrayOctet == null) {
return false;
}
for (int i = 0; i < arrayOctet.length; i++) {
if (PAD == arrayOctet[i] || isInAlphabet(arrayOctet[i])) {
return true;
}
}
return false;
}
/**
* Calculates the amount of space needed to encode the supplied array.
*
* @param pArray byte[] array which will later be encoded
*
* @return amount of space needed to encoded the supplied array.
* Returns a long since a max-len array will require > Integer.MAX_VALUE
*/
public long getEncodedLength(byte[] pArray) {
// Calculate non-chunked size - rounded up to allow for padding
// cast to long is needed to avoid possibility of overflow
long len = ((pArray.length + unencodedBlockSize-1) / unencodedBlockSize) * (long) encodedBlockSize;
if (lineLength > 0) { // We're using chunking
// Round up to nearest multiple
len += ((len + lineLength-1) / lineLength) * chunkSeparatorLength;
}
return len;
}
}

Просмотреть файл

@ -0,0 +1,132 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.binary;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Abstract superclass for Base-N input streams.
*
* @since 1.5
*/
public class BaseNCodecInputStream extends FilterInputStream {
private final boolean doEncode;
private final BaseNCodec baseNCodec;
private final byte[] singleByte = new byte[1];
protected BaseNCodecInputStream(InputStream in, BaseNCodec baseNCodec, boolean doEncode) {
super(in);
this.doEncode = doEncode;
this.baseNCodec = baseNCodec;
}
/**
* Reads one <code>byte</code> from this input stream.
*
* @return the byte as an integer in the range 0 to 255. Returns -1 if EOF has been reached.
* @throws IOException
* if an I/O error occurs.
*/
public int read() throws IOException {
int r = read(singleByte, 0, 1);
while (r == 0) {
r = read(singleByte, 0, 1);
}
if (r > 0) {
return singleByte[0] < 0 ? 256 + singleByte[0] : singleByte[0];
}
return -1;
}
/**
* Attempts to read <code>len</code> bytes into the specified <code>b</code> array starting at <code>offset</code>
* from this InputStream.
*
* @param b
* destination byte array
* @param offset
* where to start writing the bytes
* @param len
* maximum number of bytes to read
*
* @return number of bytes read
* @throws IOException
* if an I/O error occurs.
* @throws NullPointerException
* if the byte array parameter is null
* @throws IndexOutOfBoundsException
* if offset, len or buffer size are invalid
*/
public int read(byte b[], int offset, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (offset < 0 || len < 0) {
throw new IndexOutOfBoundsException();
} else if (offset > b.length || offset + len > b.length) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
} else {
int readLen = 0;
/*
Rationale for while-loop on (readLen == 0):
-----
Base32.readResults() usually returns > 0 or EOF (-1). In the
rare case where it returns 0, we just keep trying.
This is essentially an undocumented contract for InputStream
implementors that want their code to work properly with
java.io.InputStreamReader, since the latter hates it when
InputStream.read(byte[]) returns a zero. Unfortunately our
readResults() call must return 0 if a large amount of the data
being decoded was non-base32, so this while-loop enables proper
interop with InputStreamReader for that scenario.
-----
This is a fix for CODEC-101
*/
while (readLen == 0) {
if (!baseNCodec.hasData()) {
byte[] buf = new byte[doEncode ? 4096 : 8192];
int c = in.read(buf);
if (doEncode) {
baseNCodec.encode(buf, 0, c);
} else {
baseNCodec.decode(buf, 0, c);
}
}
readLen = baseNCodec.readResults(b, offset, len);
}
return readLen;
}
}
/**
* {@inheritDoc}
*
* @return false
*/
public boolean markSupported() {
return false; // not an easy job to support marks
}
}

Просмотреть файл

@ -0,0 +1,142 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.binary;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* Abstract superclass for Base-N output streams.
*
* @since 1.5
*/
public class BaseNCodecOutputStream extends FilterOutputStream {
private final boolean doEncode;
private final BaseNCodec baseNCodec;
private final byte[] singleByte = new byte[1];
public BaseNCodecOutputStream(OutputStream out, BaseNCodec basedCodec, boolean doEncode) {
super(out);
this.baseNCodec = basedCodec;
this.doEncode = doEncode;
}
/**
* Writes the specified <code>byte</code> to this output stream.
*
* @param i
* source byte
* @throws IOException
* if an I/O error occurs.
*/
public void write(int i) throws IOException {
singleByte[0] = (byte) i;
write(singleByte, 0, 1);
}
/**
* Writes <code>len</code> bytes from the specified <code>b</code> array starting at <code>offset</code> to this
* output stream.
*
* @param b
* source byte array
* @param offset
* where to start reading the bytes
* @param len
* maximum number of bytes to write
*
* @throws IOException
* if an I/O error occurs.
* @throws NullPointerException
* if the byte array parameter is null
* @throws IndexOutOfBoundsException
* if offset, len or buffer size are invalid
*/
public void write(byte b[], int offset, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (offset < 0 || len < 0) {
throw new IndexOutOfBoundsException();
} else if (offset > b.length || offset + len > b.length) {
throw new IndexOutOfBoundsException();
} else if (len > 0) {
if (doEncode) {
baseNCodec.encode(b, offset, len);
} else {
baseNCodec.decode(b, offset, len);
}
flush(false);
}
}
/**
* Flushes this output stream and forces any buffered output bytes to be written out to the stream. If propogate is
* true, the wrapped stream will also be flushed.
*
* @param propogate
* boolean flag to indicate whether the wrapped OutputStream should also be flushed.
* @throws IOException
* if an I/O error occurs.
*/
private void flush(boolean propogate) throws IOException {
int avail = baseNCodec.available();
if (avail > 0) {
byte[] buf = new byte[avail];
int c = baseNCodec.readResults(buf, 0, avail);
if (c > 0) {
out.write(buf, 0, c);
}
}
if (propogate) {
out.flush();
}
}
/**
* Flushes this output stream and forces any buffered output bytes to be written out to the stream.
*
* @throws IOException
* if an I/O error occurs.
*/
public void flush() throws IOException {
flush(true);
}
/**
* Closes this output stream and releases any system resources associated with the stream.
*
* @throws IOException
* if an I/O error occurs.
*/
public void close() throws IOException {
// Notify encoder of EOF (-1).
if (doEncode) {
baseNCodec.encode(singleByte, 0, -1);
} else {
baseNCodec.decode(singleByte, 0, -1);
}
flush();
out.close();
}
}

Просмотреть файл

@ -0,0 +1,297 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.binary;
import org.mozilla.apache.commons.codec.BinaryDecoder;
import org.mozilla.apache.commons.codec.BinaryEncoder;
import org.mozilla.apache.commons.codec.DecoderException;
import org.mozilla.apache.commons.codec.EncoderException;
/**
* Converts between byte arrays and strings of "0"s and "1"s.
*
* TODO: may want to add more bit vector functions like and/or/xor/nand
* TODO: also might be good to generate boolean[] from byte[] et cetera.
*
* @author Apache Software Foundation
* @since 1.3
* @version $Id: BinaryCodec.java 1080701 2011-03-11 17:52:27Z ggregory $
*/
public class BinaryCodec implements BinaryDecoder, BinaryEncoder {
/*
* tried to avoid using ArrayUtils to minimize dependencies while using these empty arrays - dep is just not worth
* it.
*/
/** Empty char array. */
private static final char[] EMPTY_CHAR_ARRAY = new char[0];
/** Empty byte array. */
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
/** Mask for bit 0 of a byte. */
private static final int BIT_0 = 1;
/** Mask for bit 1 of a byte. */
private static final int BIT_1 = 0x02;
/** Mask for bit 2 of a byte. */
private static final int BIT_2 = 0x04;
/** Mask for bit 3 of a byte. */
private static final int BIT_3 = 0x08;
/** Mask for bit 4 of a byte. */
private static final int BIT_4 = 0x10;
/** Mask for bit 5 of a byte. */
private static final int BIT_5 = 0x20;
/** Mask for bit 6 of a byte. */
private static final int BIT_6 = 0x40;
/** Mask for bit 7 of a byte. */
private static final int BIT_7 = 0x80;
private static final int[] BITS = {BIT_0, BIT_1, BIT_2, BIT_3, BIT_4, BIT_5, BIT_6, BIT_7};
/**
* Converts an array of raw binary data into an array of ASCII 0 and 1 characters.
*
* @param raw
* the raw binary data to convert
* @return 0 and 1 ASCII character bytes one for each bit of the argument
* @see org.mozilla.apache.commons.codec.BinaryEncoder#encode(byte[])
*/
public byte[] encode(byte[] raw) {
return toAsciiBytes(raw);
}
/**
* Converts an array of raw binary data into an array of ASCII 0 and 1 chars.
*
* @param raw
* the raw binary data to convert
* @return 0 and 1 ASCII character chars one for each bit of the argument
* @throws EncoderException
* if the argument is not a byte[]
* @see org.mozilla.apache.commons.codec.Encoder#encode(Object)
*/
public Object encode(Object raw) throws EncoderException {
if (!(raw instanceof byte[])) {
throw new EncoderException("argument not a byte array");
}
return toAsciiChars((byte[]) raw);
}
/**
* Decodes a byte array where each byte represents an ASCII '0' or '1'.
*
* @param ascii
* each byte represents an ASCII '0' or '1'
* @return the raw encoded binary where each bit corresponds to a byte in the byte array argument
* @throws DecoderException
* if argument is not a byte[], char[] or String
* @see org.mozilla.apache.commons.codec.Decoder#decode(Object)
*/
public Object decode(Object ascii) throws DecoderException {
if (ascii == null) {
return EMPTY_BYTE_ARRAY;
}
if (ascii instanceof byte[]) {
return fromAscii((byte[]) ascii);
}
if (ascii instanceof char[]) {
return fromAscii((char[]) ascii);
}
if (ascii instanceof String) {
return fromAscii(((String) ascii).toCharArray());
}
throw new DecoderException("argument not a byte array");
}
/**
* Decodes a byte array where each byte represents an ASCII '0' or '1'.
*
* @param ascii
* each byte represents an ASCII '0' or '1'
* @return the raw encoded binary where each bit corresponds to a byte in the byte array argument
* @see org.mozilla.apache.commons.codec.Decoder#decode(Object)
*/
public byte[] decode(byte[] ascii) {
return fromAscii(ascii);
}
/**
* Decodes a String where each char of the String represents an ASCII '0' or '1'.
*
* @param ascii
* String of '0' and '1' characters
* @return the raw encoded binary where each bit corresponds to a byte in the byte array argument
* @see org.mozilla.apache.commons.codec.Decoder#decode(Object)
*/
public byte[] toByteArray(String ascii) {
if (ascii == null) {
return EMPTY_BYTE_ARRAY;
}
return fromAscii(ascii.toCharArray());
}
// ------------------------------------------------------------------------
//
// static codec operations
//
// ------------------------------------------------------------------------
/**
* Decodes a char array where each char represents an ASCII '0' or '1'.
*
* @param ascii
* each char represents an ASCII '0' or '1'
* @return the raw encoded binary where each bit corresponds to a char in the char array argument
*/
public static byte[] fromAscii(char[] ascii) {
if (ascii == null || ascii.length == 0) {
return EMPTY_BYTE_ARRAY;
}
// get length/8 times bytes with 3 bit shifts to the right of the length
byte[] l_raw = new byte[ascii.length >> 3];
/*
* We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
* loop.
*/
for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) {
for (int bits = 0; bits < BITS.length; ++bits) {
if (ascii[jj - bits] == '1') {
l_raw[ii] |= BITS[bits];
}
}
}
return l_raw;
}
/**
* Decodes a byte array where each byte represents an ASCII '0' or '1'.
*
* @param ascii
* each byte represents an ASCII '0' or '1'
* @return the raw encoded binary where each bit corresponds to a byte in the byte array argument
*/
public static byte[] fromAscii(byte[] ascii) {
if (isEmpty(ascii)) {
return EMPTY_BYTE_ARRAY;
}
// get length/8 times bytes with 3 bit shifts to the right of the length
byte[] l_raw = new byte[ascii.length >> 3];
/*
* We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
* loop.
*/
for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) {
for (int bits = 0; bits < BITS.length; ++bits) {
if (ascii[jj - bits] == '1') {
l_raw[ii] |= BITS[bits];
}
}
}
return l_raw;
}
/**
* Returns <code>true</code> if the given array is <code>null</code> or empty (size 0.)
*
* @param array
* the source array
* @return <code>true</code> if the given array is <code>null</code> or empty (size 0.)
*/
private static boolean isEmpty(byte[] array) {
return array == null || array.length == 0;
}
/**
* Converts an array of raw binary data into an array of ASCII 0 and 1 character bytes - each byte is a truncated
* char.
*
* @param raw
* the raw binary data to convert
* @return an array of 0 and 1 character bytes for each bit of the argument
* @see org.mozilla.apache.commons.codec.BinaryEncoder#encode(byte[])
*/
public static byte[] toAsciiBytes(byte[] raw) {
if (isEmpty(raw)) {
return EMPTY_BYTE_ARRAY;
}
// get 8 times the bytes with 3 bit shifts to the left of the length
byte[] l_ascii = new byte[raw.length << 3];
/*
* We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
* loop.
*/
for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) {
for (int bits = 0; bits < BITS.length; ++bits) {
if ((raw[ii] & BITS[bits]) == 0) {
l_ascii[jj - bits] = '0';
} else {
l_ascii[jj - bits] = '1';
}
}
}
return l_ascii;
}
/**
* Converts an array of raw binary data into an array of ASCII 0 and 1 characters.
*
* @param raw
* the raw binary data to convert
* @return an array of 0 and 1 characters for each bit of the argument
* @see org.mozilla.apache.commons.codec.BinaryEncoder#encode(byte[])
*/
public static char[] toAsciiChars(byte[] raw) {
if (isEmpty(raw)) {
return EMPTY_CHAR_ARRAY;
}
// get 8 times the bytes with 3 bit shifts to the left of the length
char[] l_ascii = new char[raw.length << 3];
/*
* We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
* loop.
*/
for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) {
for (int bits = 0; bits < BITS.length; ++bits) {
if ((raw[ii] & BITS[bits]) == 0) {
l_ascii[jj - bits] = '0';
} else {
l_ascii[jj - bits] = '1';
}
}
}
return l_ascii;
}
/**
* Converts an array of raw binary data into a String of ASCII 0 and 1 characters.
*
* @param raw
* the raw binary data to convert
* @return a String of 0 and 1 characters representing the binary data
* @see org.mozilla.apache.commons.codec.BinaryEncoder#encode(byte[])
*/
public static String toAsciiString(byte[] raw) {
return new String(toAsciiChars(raw));
}
}

Просмотреть файл

@ -0,0 +1,302 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.binary;
import java.io.UnsupportedEncodingException;
import org.mozilla.apache.commons.codec.BinaryDecoder;
import org.mozilla.apache.commons.codec.BinaryEncoder;
import org.mozilla.apache.commons.codec.CharEncoding;
import org.mozilla.apache.commons.codec.DecoderException;
import org.mozilla.apache.commons.codec.EncoderException;
/**
* Converts hexadecimal Strings. The charset used for certain operation can be set, the default is set in
* {@link #DEFAULT_CHARSET_NAME}
*
* @since 1.1
* @author Apache Software Foundation
* @version $Id: Hex.java 1080701 2011-03-11 17:52:27Z ggregory $
*/
public class Hex implements BinaryEncoder, BinaryDecoder {
/**
* Default charset name is {@link CharEncoding#UTF_8}
*
* @since 1.4
*/
public static final String DEFAULT_CHARSET_NAME = CharEncoding.UTF_8;
/**
* Used to build output as Hex
*/
private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* Used to build output as Hex
*/
private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
/**
* Converts an array of characters representing hexadecimal values into an array of bytes of those same values. The
* returned array will be half the length of the passed array, as it takes two characters to represent any given
* byte. An exception is thrown if the passed char array has an odd number of elements.
*
* @param data
* An array of characters containing hexadecimal digits
* @return A byte array containing binary data decoded from the supplied char array.
* @throws DecoderException
* Thrown if an odd number or illegal of characters is supplied
*/
public static byte[] decodeHex(char[] data) throws DecoderException {
int len = data.length;
if ((len & 0x01) != 0) {
throw new DecoderException("Odd number of characters.");
}
byte[] out = new byte[len >> 1];
// two characters form the hex value.
for (int i = 0, j = 0; j < len; i++) {
int f = toDigit(data[j], j) << 4;
j++;
f = f | toDigit(data[j], j);
j++;
out[i] = (byte) (f & 0xFF);
}
return out;
}
/**
* Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
* The returned array will be double the length of the passed array, as it takes two characters to represent any
* given byte.
*
* @param data
* a byte[] to convert to Hex characters
* @return A char[] containing hexadecimal characters
*/
public static char[] encodeHex(byte[] data) {
return encodeHex(data, true);
}
/**
* Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
* The returned array will be double the length of the passed array, as it takes two characters to represent any
* given byte.
*
* @param data
* a byte[] to convert to Hex characters
* @param toLowerCase
* <code>true</code> converts to lowercase, <code>false</code> to uppercase
* @return A char[] containing hexadecimal characters
* @since 1.4
*/
public static char[] encodeHex(byte[] data, boolean toLowerCase) {
return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
}
/**
* Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
* The returned array will be double the length of the passed array, as it takes two characters to represent any
* given byte.
*
* @param data
* a byte[] to convert to Hex characters
* @param toDigits
* the output alphabet
* @return A char[] containing hexadecimal characters
* @since 1.4
*/
protected static char[] encodeHex(byte[] data, char[] toDigits) {
int l = data.length;
char[] out = new char[l << 1];
// two characters form the hex value.
for (int i = 0, j = 0; i < l; i++) {
out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
out[j++] = toDigits[0x0F & data[i]];
}
return out;
}
/**
* Converts an array of bytes into a String representing the hexadecimal values of each byte in order. The returned
* String will be double the length of the passed array, as it takes two characters to represent any given byte.
*
* @param data
* a byte[] to convert to Hex characters
* @return A String containing hexadecimal characters
* @since 1.4
*/
public static String encodeHexString(byte[] data) {
return new String(encodeHex(data));
}
/**
* Converts a hexadecimal character to an integer.
*
* @param ch
* A character to convert to an integer digit
* @param index
* The index of the character in the source
* @return An integer
* @throws DecoderException
* Thrown if ch is an illegal hex character
*/
protected static int toDigit(char ch, int index) throws DecoderException {
int digit = Character.digit(ch, 16);
if (digit == -1) {
throw new DecoderException("Illegal hexadecimal character " + ch + " at index " + index);
}
return digit;
}
private final String charsetName;
/**
* Creates a new codec with the default charset name {@link #DEFAULT_CHARSET_NAME}
*/
public Hex() {
// use default encoding
this.charsetName = DEFAULT_CHARSET_NAME;
}
/**
* Creates a new codec with the given charset name.
*
* @param csName
* the charset name.
* @since 1.4
*/
public Hex(String csName) {
this.charsetName = csName;
}
/**
* Converts an array of character bytes representing hexadecimal values into an array of bytes of those same values.
* The returned array will be half the length of the passed array, as it takes two characters to represent any given
* byte. An exception is thrown if the passed char array has an odd number of elements.
*
* @param array
* An array of character bytes containing hexadecimal digits
* @return A byte array containing binary data decoded from the supplied byte array (representing characters).
* @throws DecoderException
* Thrown if an odd number of characters is supplied to this function
* @see #decodeHex(char[])
*/
public byte[] decode(byte[] array) throws DecoderException {
try {
return decodeHex(new String(array, getCharsetName()).toCharArray());
} catch (UnsupportedEncodingException e) {
throw new DecoderException(e.getMessage(), e);
}
}
/**
* Converts a String or an array of character bytes representing hexadecimal values into an array of bytes of those
* same values. The returned array will be half the length of the passed String or array, as it takes two characters
* to represent any given byte. An exception is thrown if the passed char array has an odd number of elements.
*
* @param object
* A String or, an array of character bytes containing hexadecimal digits
* @return A byte array containing binary data decoded from the supplied byte array (representing characters).
* @throws DecoderException
* Thrown if an odd number of characters is supplied to this function or the object is not a String or
* char[]
* @see #decodeHex(char[])
*/
public Object decode(Object object) throws DecoderException {
try {
char[] charArray = object instanceof String ? ((String) object).toCharArray() : (char[]) object;
return decodeHex(charArray);
} catch (ClassCastException e) {
throw new DecoderException(e.getMessage(), e);
}
}
/**
* Converts an array of bytes into an array of bytes for the characters representing the hexadecimal values of each
* byte in order. The returned array will be double the length of the passed array, as it takes two characters to
* represent any given byte.
* <p>
* The conversion from hexadecimal characters to the returned bytes is performed with the charset named by
* {@link #getCharsetName()}.
* </p>
*
* @param array
* a byte[] to convert to Hex characters
* @return A byte[] containing the bytes of the hexadecimal characters
* @throws IllegalStateException
* if the charsetName is invalid. This API throws {@link IllegalStateException} instead of
* {@link UnsupportedEncodingException} for backward compatibility.
* @see #encodeHex(byte[])
*/
public byte[] encode(byte[] array) {
return StringUtils.getBytesUnchecked(encodeHexString(array), getCharsetName());
}
/**
* Converts a String or an array of bytes into an array of characters representing the hexadecimal values of each
* byte in order. The returned array will be double the length of the passed String or array, as it takes two
* characters to represent any given byte.
* <p>
* The conversion from hexadecimal characters to bytes to be encoded to performed with the charset named by
* {@link #getCharsetName()}.
* </p>
*
* @param object
* a String, or byte[] to convert to Hex characters
* @return A char[] containing hexadecimal characters
* @throws EncoderException
* Thrown if the given object is not a String or byte[]
* @see #encodeHex(byte[])
*/
public Object encode(Object object) throws EncoderException {
try {
byte[] byteArray = object instanceof String ? ((String) object).getBytes(getCharsetName()) : (byte[]) object;
return encodeHex(byteArray);
} catch (ClassCastException e) {
throw new EncoderException(e.getMessage(), e);
} catch (UnsupportedEncodingException e) {
throw new EncoderException(e.getMessage(), e);
}
}
/**
* Gets the charset name.
*
* @return the charset name.
* @since 1.4
*/
public String getCharsetName() {
return this.charsetName;
}
/**
* Returns a string representation of the object, which includes the charset name.
*
* @return a string representation of the object.
*/
public String toString() {
return super.toString() + "[charsetName=" + this.charsetName + "]";
}
}

Просмотреть файл

@ -0,0 +1,287 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.binary;
import java.io.UnsupportedEncodingException;
import org.mozilla.apache.commons.codec.CharEncoding;
/**
* Converts String to and from bytes using the encodings required by the Java specification. These encodings are specified in <a
* href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
*
* @see CharEncoding
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @version $Id: StringUtils.java 950460 2010-06-02 09:43:02Z sebb $
* @since 1.4
*/
public class StringUtils {
/**
* Encodes the given string into a sequence of bytes using the ISO-8859-1 charset, storing the result into a new
* byte array.
*
* @param string
* the String to encode, may be <code>null</code>
* @return encoded bytes, or <code>null</code> if the input string was <code>null</code>
* @throws IllegalStateException
* Thrown when the charset is missing, which should be never according the the Java specification.
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
* @see #getBytesUnchecked(String, String)
*/
public static byte[] getBytesIso8859_1(String string) {
return StringUtils.getBytesUnchecked(string, CharEncoding.ISO_8859_1);
}
/**
* Encodes the given string into a sequence of bytes using the US-ASCII charset, storing the result into a new byte
* array.
*
* @param string
* the String to encode, may be <code>null</code>
* @return encoded bytes, or <code>null</code> if the input string was <code>null</code>
* @throws IllegalStateException
* Thrown when the charset is missing, which should be never according the the Java specification.
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
* @see #getBytesUnchecked(String, String)
*/
public static byte[] getBytesUsAscii(String string) {
return StringUtils.getBytesUnchecked(string, CharEncoding.US_ASCII);
}
/**
* Encodes the given string into a sequence of bytes using the UTF-16 charset, storing the result into a new byte
* array.
*
* @param string
* the String to encode, may be <code>null</code>
* @return encoded bytes, or <code>null</code> if the input string was <code>null</code>
* @throws IllegalStateException
* Thrown when the charset is missing, which should be never according the the Java specification.
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
* @see #getBytesUnchecked(String, String)
*/
public static byte[] getBytesUtf16(String string) {
return StringUtils.getBytesUnchecked(string, CharEncoding.UTF_16);
}
/**
* Encodes the given string into a sequence of bytes using the UTF-16BE charset, storing the result into a new byte
* array.
*
* @param string
* the String to encode, may be <code>null</code>
* @return encoded bytes, or <code>null</code> if the input string was <code>null</code>
* @throws IllegalStateException
* Thrown when the charset is missing, which should be never according the the Java specification.
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
* @see #getBytesUnchecked(String, String)
*/
public static byte[] getBytesUtf16Be(String string) {
return StringUtils.getBytesUnchecked(string, CharEncoding.UTF_16BE);
}
/**
* Encodes the given string into a sequence of bytes using the UTF-16LE charset, storing the result into a new byte
* array.
*
* @param string
* the String to encode, may be <code>null</code>
* @return encoded bytes, or <code>null</code> if the input string was <code>null</code>
* @throws IllegalStateException
* Thrown when the charset is missing, which should be never according the the Java specification.
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
* @see #getBytesUnchecked(String, String)
*/
public static byte[] getBytesUtf16Le(String string) {
return StringUtils.getBytesUnchecked(string, CharEncoding.UTF_16LE);
}
/**
* Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte
* array.
*
* @param string
* the String to encode, may be <code>null</code>
* @return encoded bytes, or <code>null</code> if the input string was <code>null</code>
* @throws IllegalStateException
* Thrown when the charset is missing, which should be never according the the Java specification.
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
* @see #getBytesUnchecked(String, String)
*/
public static byte[] getBytesUtf8(String string) {
return StringUtils.getBytesUnchecked(string, CharEncoding.UTF_8);
}
/**
* Encodes the given string into a sequence of bytes using the named charset, storing the result into a new byte
* array.
* <p>
* This method catches {@link UnsupportedEncodingException} and rethrows it as {@link IllegalStateException}, which
* should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.
* </p>
*
* @param string
* the String to encode, may be <code>null</code>
* @param charsetName
* The name of a required {@link java.nio.charset.Charset}
* @return encoded bytes, or <code>null</code> if the input string was <code>null</code>
* @throws IllegalStateException
* Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen for a
* required charset name.
* @see CharEncoding
* @see String#getBytes(String)
*/
public static byte[] getBytesUnchecked(String string, String charsetName) {
if (string == null) {
return null;
}
try {
return string.getBytes(charsetName);
} catch (UnsupportedEncodingException e) {
throw StringUtils.newIllegalStateException(charsetName, e);
}
}
private static IllegalStateException newIllegalStateException(String charsetName, UnsupportedEncodingException e) {
return new IllegalStateException(charsetName + ": " + e);
}
/**
* Constructs a new <code>String</code> by decoding the specified array of bytes using the given charset.
* <p>
* This method catches {@link UnsupportedEncodingException} and re-throws it as {@link IllegalStateException}, which
* should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.
* </p>
*
* @param bytes
* The bytes to be decoded into characters, may be <code>null</code>
* @param charsetName
* The name of a required {@link java.nio.charset.Charset}
* @return A new <code>String</code> decoded from the specified array of bytes using the given charset,
* or <code>null</code> if the input byte arrray was <code>null</code>.
* @throws IllegalStateException
* Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen for a
* required charset name.
* @see CharEncoding
* @see String#String(byte[], String)
*/
public static String newString(byte[] bytes, String charsetName) {
if (bytes == null) {
return null;
}
try {
return new String(bytes, charsetName);
} catch (UnsupportedEncodingException e) {
throw StringUtils.newIllegalStateException(charsetName, e);
}
}
/**
* Constructs a new <code>String</code> by decoding the specified array of bytes using the ISO-8859-1 charset.
*
* @param bytes
* The bytes to be decoded into characters, may be <code>null</code>
* @return A new <code>String</code> decoded from the specified array of bytes using the ISO-8859-1 charset,
* or <code>null</code> if the input byte array was <code>null</code>.
* @throws IllegalStateException
* Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
* charset is required.
*/
public static String newStringIso8859_1(byte[] bytes) {
return StringUtils.newString(bytes, CharEncoding.ISO_8859_1);
}
/**
* Constructs a new <code>String</code> by decoding the specified array of bytes using the US-ASCII charset.
*
* @param bytes
* The bytes to be decoded into characters
* @return A new <code>String</code> decoded from the specified array of bytes using the US-ASCII charset,
* or <code>null</code> if the input byte array was <code>null</code>.
* @throws IllegalStateException
* Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
* charset is required.
*/
public static String newStringUsAscii(byte[] bytes) {
return StringUtils.newString(bytes, CharEncoding.US_ASCII);
}
/**
* Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16 charset.
*
* @param bytes
* The bytes to be decoded into characters
* @return A new <code>String</code> decoded from the specified array of bytes using the UTF-16 charset
* or <code>null</code> if the input byte array was <code>null</code>.
* @throws IllegalStateException
* Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
* charset is required.
*/
public static String newStringUtf16(byte[] bytes) {
return StringUtils.newString(bytes, CharEncoding.UTF_16);
}
/**
* Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16BE charset.
*
* @param bytes
* The bytes to be decoded into characters
* @return A new <code>String</code> decoded from the specified array of bytes using the UTF-16BE charset,
* or <code>null</code> if the input byte array was <code>null</code>.
* @throws IllegalStateException
* Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
* charset is required.
*/
public static String newStringUtf16Be(byte[] bytes) {
return StringUtils.newString(bytes, CharEncoding.UTF_16BE);
}
/**
* Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16LE charset.
*
* @param bytes
* The bytes to be decoded into characters
* @return A new <code>String</code> decoded from the specified array of bytes using the UTF-16LE charset,
* or <code>null</code> if the input byte array was <code>null</code>.
* @throws IllegalStateException
* Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
* charset is required.
*/
public static String newStringUtf16Le(byte[] bytes) {
return StringUtils.newString(bytes, CharEncoding.UTF_16LE);
}
/**
* Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-8 charset.
*
* @param bytes
* The bytes to be decoded into characters
* @return A new <code>String</code> decoded from the specified array of bytes using the UTF-8 charset,
* or <code>null</code> if the input byte array was <code>null</code>.
* @throws IllegalStateException
* Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
* charset is required.
*/
public static String newStringUtf8(byte[] bytes) {
return StringUtils.newString(bytes, CharEncoding.UTF_8);
}
}

Просмотреть файл

@ -0,0 +1,21 @@
<!--
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.
-->
<html>
<body>
Base64, Base32, Binary, and Hexadecimal String encoding and decoding.
</body>
</html>

Просмотреть файл

@ -0,0 +1,583 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.digest;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.mozilla.apache.commons.codec.binary.Hex;
import org.mozilla.apache.commons.codec.binary.StringUtils;
/**
* Operations to simplify common {@link java.security.MessageDigest} tasks. This class is thread safe.
*
* @author Apache Software Foundation
* @version $Id: DigestUtils.java 1064793 2011-01-28 17:42:55Z ggregory $
*/
public class DigestUtils {
private static final int STREAM_BUFFER_LENGTH = 1024;
/**
* Read through an InputStream and returns the digest for the data
*
* @param digest
* The MessageDigest to use (e.g. MD5)
* @param data
* Data to digest
* @return MD5 digest
* @throws IOException
* On error reading from the stream
*/
private static byte[] digest(MessageDigest digest, InputStream data) throws IOException {
byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
while (read > -1) {
digest.update(buffer, 0, read);
read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
}
return digest.digest();
}
/**
* Calls {@link StringUtils#getBytesUtf8(String)}
*
* @param data
* the String to encode
* @return encoded bytes
*/
private static byte[] getBytesUtf8(String data) {
return StringUtils.getBytesUtf8(data);
}
/**
* Returns a <code>MessageDigest</code> for the given <code>algorithm</code>.
*
* @param algorithm
* the name of the algorithm requested. See <a
* href="http://java.sun.com/j2se/1.3/docs/guide/security/CryptoSpec.html#AppA">Appendix A in the Java
* Cryptography Architecture API Specification & Reference</a> for information about standard algorithm
* names.
* @return An MD5 digest instance.
* @see MessageDigest#getInstance(String)
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
static MessageDigest getDigest(String algorithm) {
try {
return MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* Returns an MD5 MessageDigest.
*
* @return An MD5 digest instance.
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
private static MessageDigest getMd5Digest() {
return getDigest("MD5");
}
/**
* Returns an SHA-256 digest.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @return An SHA-256 digest instance.
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
private static MessageDigest getSha256Digest() {
return getDigest("SHA-256");
}
/**
* Returns an SHA-384 digest.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @return An SHA-384 digest instance.
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
private static MessageDigest getSha384Digest() {
return getDigest("SHA-384");
}
/**
* Returns an SHA-512 digest.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @return An SHA-512 digest instance.
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
private static MessageDigest getSha512Digest() {
return getDigest("SHA-512");
}
/**
* Returns an SHA-1 digest.
*
* @return An SHA-1 digest instance.
* @throws RuntimeException
* when a {@link java.security.NoSuchAlgorithmException} is caught.
*/
private static MessageDigest getShaDigest() {
return getDigest("SHA");
}
/**
* Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
*
* @param data
* Data to digest
* @return MD5 digest
*/
public static byte[] md5(byte[] data) {
return getMd5Digest().digest(data);
}
/**
* Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
*
* @param data
* Data to digest
* @return MD5 digest
* @throws IOException
* On error reading from the stream
* @since 1.4
*/
public static byte[] md5(InputStream data) throws IOException {
return digest(getMd5Digest(), data);
}
/**
* Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
*
* @param data
* Data to digest
* @return MD5 digest
*/
public static byte[] md5(String data) {
return md5(getBytesUtf8(data));
}
/**
* Calculates the MD5 digest and returns the value as a 32 character hex string.
*
* @param data
* Data to digest
* @return MD5 digest as a hex string
*/
public static String md5Hex(byte[] data) {
return Hex.encodeHexString(md5(data));
}
/**
* Calculates the MD5 digest and returns the value as a 32 character hex string.
*
* @param data
* Data to digest
* @return MD5 digest as a hex string
* @throws IOException
* On error reading from the stream
* @since 1.4
*/
public static String md5Hex(InputStream data) throws IOException {
return Hex.encodeHexString(md5(data));
}
/**
* Calculates the MD5 digest and returns the value as a 32 character hex string.
*
* @param data
* Data to digest
* @return MD5 digest as a hex string
*/
public static String md5Hex(String data) {
return Hex.encodeHexString(md5(data));
}
/**
* Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>.
*
* @param data
* Data to digest
* @return SHA-1 digest
*/
public static byte[] sha(byte[] data) {
return getShaDigest().digest(data);
}
/**
* Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>.
*
* @param data
* Data to digest
* @return SHA-1 digest
* @throws IOException
* On error reading from the stream
* @since 1.4
*/
public static byte[] sha(InputStream data) throws IOException {
return digest(getShaDigest(), data);
}
/**
* Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>.
*
* @param data
* Data to digest
* @return SHA-1 digest
*/
public static byte[] sha(String data) {
return sha(getBytesUtf8(data));
}
/**
* Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-256 digest
* @since 1.4
*/
public static byte[] sha256(byte[] data) {
return getSha256Digest().digest(data);
}
/**
* Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-256 digest
* @throws IOException
* On error reading from the stream
* @since 1.4
*/
public static byte[] sha256(InputStream data) throws IOException {
return digest(getSha256Digest(), data);
}
/**
* Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-256 digest
* @since 1.4
*/
public static byte[] sha256(String data) {
return sha256(getBytesUtf8(data));
}
/**
* Calculates the SHA-256 digest and returns the value as a hex string.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-256 digest as a hex string
* @since 1.4
*/
public static String sha256Hex(byte[] data) {
return Hex.encodeHexString(sha256(data));
}
/**
* Calculates the SHA-256 digest and returns the value as a hex string.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-256 digest as a hex string
* @throws IOException
* On error reading from the stream
* @since 1.4
*/
public static String sha256Hex(InputStream data) throws IOException {
return Hex.encodeHexString(sha256(data));
}
/**
* Calculates the SHA-256 digest and returns the value as a hex string.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-256 digest as a hex string
* @since 1.4
*/
public static String sha256Hex(String data) {
return Hex.encodeHexString(sha256(data));
}
/**
* Calculates the SHA-384 digest and returns the value as a <code>byte[]</code>.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-384 digest
* @since 1.4
*/
public static byte[] sha384(byte[] data) {
return getSha384Digest().digest(data);
}
/**
* Calculates the SHA-384 digest and returns the value as a <code>byte[]</code>.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-384 digest
* @throws IOException
* On error reading from the stream
* @since 1.4
*/
public static byte[] sha384(InputStream data) throws IOException {
return digest(getSha384Digest(), data);
}
/**
* Calculates the SHA-384 digest and returns the value as a <code>byte[]</code>.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-384 digest
* @since 1.4
*/
public static byte[] sha384(String data) {
return sha384(getBytesUtf8(data));
}
/**
* Calculates the SHA-384 digest and returns the value as a hex string.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-384 digest as a hex string
* @since 1.4
*/
public static String sha384Hex(byte[] data) {
return Hex.encodeHexString(sha384(data));
}
/**
* Calculates the SHA-384 digest and returns the value as a hex string.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-384 digest as a hex string
* @throws IOException
* On error reading from the stream
* @since 1.4
*/
public static String sha384Hex(InputStream data) throws IOException {
return Hex.encodeHexString(sha384(data));
}
/**
* Calculates the SHA-384 digest and returns the value as a hex string.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-384 digest as a hex string
* @since 1.4
*/
public static String sha384Hex(String data) {
return Hex.encodeHexString(sha384(data));
}
/**
* Calculates the SHA-512 digest and returns the value as a <code>byte[]</code>.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-512 digest
* @since 1.4
*/
public static byte[] sha512(byte[] data) {
return getSha512Digest().digest(data);
}
/**
* Calculates the SHA-512 digest and returns the value as a <code>byte[]</code>.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-512 digest
* @throws IOException
* On error reading from the stream
* @since 1.4
*/
public static byte[] sha512(InputStream data) throws IOException {
return digest(getSha512Digest(), data);
}
/**
* Calculates the SHA-512 digest and returns the value as a <code>byte[]</code>.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-512 digest
* @since 1.4
*/
public static byte[] sha512(String data) {
return sha512(getBytesUtf8(data));
}
/**
* Calculates the SHA-512 digest and returns the value as a hex string.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-512 digest as a hex string
* @since 1.4
*/
public static String sha512Hex(byte[] data) {
return Hex.encodeHexString(sha512(data));
}
/**
* Calculates the SHA-512 digest and returns the value as a hex string.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-512 digest as a hex string
* @throws IOException
* On error reading from the stream
* @since 1.4
*/
public static String sha512Hex(InputStream data) throws IOException {
return Hex.encodeHexString(sha512(data));
}
/**
* Calculates the SHA-512 digest and returns the value as a hex string.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
*
* @param data
* Data to digest
* @return SHA-512 digest as a hex string
* @since 1.4
*/
public static String sha512Hex(String data) {
return Hex.encodeHexString(sha512(data));
}
/**
* Calculates the SHA-1 digest and returns the value as a hex string.
*
* @param data
* Data to digest
* @return SHA-1 digest as a hex string
*/
public static String shaHex(byte[] data) {
return Hex.encodeHexString(sha(data));
}
/**
* Calculates the SHA-1 digest and returns the value as a hex string.
*
* @param data
* Data to digest
* @return SHA-1 digest as a hex string
* @throws IOException
* On error reading from the stream
* @since 1.4
*/
public static String shaHex(InputStream data) throws IOException {
return Hex.encodeHexString(sha(data));
}
/**
* Calculates the SHA-1 digest and returns the value as a hex string.
*
* @param data
* Data to digest
* @return SHA-1 digest as a hex string
*/
public static String shaHex(String data) {
return Hex.encodeHexString(sha(data));
}
}

Просмотреть файл

@ -0,0 +1,21 @@
<!--
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.
-->
<html>
<body>
Simplifies common {@link java.security.MessageDigest} tasks.
</body>
</html>

Просмотреть файл

@ -0,0 +1,78 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.language;
import org.mozilla.apache.commons.codec.EncoderException;
import org.mozilla.apache.commons.codec.StringEncoder;
/**
* Encodes a string into a Caverphone value.
*
* This is an algorithm created by the Caversham Project at the University of Otago. It implements the Caverphone 2.0
* algorithm:
*
* @author Apache Software Foundation
* @version $Id: Caverphone.java 1075947 2011-03-01 17:56:14Z ggregory $
* @see <a href="http://en.wikipedia.org/wiki/Caverphone">Wikipedia - Caverphone</a>
* @since 1.5
*/
public abstract class AbstractCaverphone implements StringEncoder {
/**
* Creates an instance of the Caverphone encoder
*/
public AbstractCaverphone() {
super();
}
/**
* Encodes an Object using the caverphone algorithm. This method is provided in order to satisfy the requirements of
* the Encoder interface, and will throw an EncoderException if the supplied object is not of type java.lang.String.
*
* @param source
* Object to encode
* @return An object (or type java.lang.String) containing the caverphone code which corresponds to the String
* supplied.
* @throws EncoderException
* if the parameter supplied is not of type java.lang.String
*/
public Object encode(Object source) throws EncoderException {
if (!(source instanceof String)) {
throw new EncoderException("Parameter supplied to Caverphone encode is not of type java.lang.String");
}
return this.encode((String) source);
}
/**
* Tests if the encodings of two strings are equal.
*
* This method might be promoted to a new AbstractStringEncoder superclass.
*
* @param str1
* First of two strings to compare
* @param str2
* Second of two strings to compare
* @return <code>true</code> if the encodings of these strings are identical, <code>false</code> otherwise.
* @throws EncoderException
*/
public boolean isEncodeEqual(String str1, String str2) throws EncoderException {
return this.encode(str1).equals(this.encode(str2));
}
}

Просмотреть файл

@ -0,0 +1,104 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.language;
import org.mozilla.apache.commons.codec.EncoderException;
import org.mozilla.apache.commons.codec.StringEncoder;
/**
* Encodes a string into a Caverphone 2.0 value. Delegate to a {@link Caverphone2} instance.
*
* This is an algorithm created by the Caversham Project at the University of Otago. It implements the Caverphone 2.0
* algorithm:
*
* @author Apache Software Foundation
* @version $Id: Caverphone.java 1079535 2011-03-08 20:54:37Z ggregory $
* @see <a href="http://en.wikipedia.org/wiki/Caverphone">Wikipedia - Caverphone</a>
* @see <a href="http://caversham.otago.ac.nz/files/working/ctp150804.pdf">Caverphone 2.0 specification</a>
* @since 1.4
* @deprecated 1.5 Replaced by {@link Caverphone2}, will be removed in 2.0.
*/
public class Caverphone implements StringEncoder {
/**
* Delegate to a {@link Caverphone2} instance to avoid code duplication.
*/
final private Caverphone2 encoder = new Caverphone2();
/**
* Creates an instance of the Caverphone encoder
*/
public Caverphone() {
super();
}
/**
* Encodes the given String into a Caverphone value.
*
* @param source
* String the source string
* @return A caverphone code for the given String
*/
public String caverphone(String source) {
return this.encoder.encode(source);
}
/**
* Encodes an Object using the caverphone algorithm. This method is provided in order to satisfy the requirements of
* the Encoder interface, and will throw an EncoderException if the supplied object is not of type java.lang.String.
*
* @param pObject
* Object to encode
* @return An object (or type java.lang.String) containing the caverphone code which corresponds to the String
* supplied.
* @throws EncoderException
* if the parameter supplied is not of type java.lang.String
*/
public Object encode(Object pObject) throws EncoderException {
if (!(pObject instanceof String)) {
throw new EncoderException("Parameter supplied to Caverphone encode is not of type java.lang.String");
}
return this.caverphone((String) pObject);
}
/**
* Encodes a String using the Caverphone algorithm.
*
* @param pString
* String object to encode
* @return The caverphone code corresponding to the String supplied
*/
public String encode(String pString) {
return this.caverphone(pString);
}
/**
* Tests if the caverphones of two strings are identical.
*
* @param str1
* First of two strings to compare
* @param str2
* Second of two strings to compare
* @return <code>true</code> if the caverphones of these strings are identical, <code>false</code> otherwise.
*/
public boolean isCaverphoneEqual(String str1, String str2) {
return this.caverphone(str1).equals(this.caverphone(str2));
}
}

Просмотреть файл

@ -0,0 +1,126 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.language;
/**
* Encodes a string into a Caverphone 1.0 value.
*
* This is an algorithm created by the Caversham Project at the University of Otago. It implements the Caverphone 1.0
* algorithm:
*
* @author Apache Software Foundation
* @version $Id: Caverphone.java 1075947 2011-03-01 17:56:14Z ggregory $
* @see <a href="http://en.wikipedia.org/wiki/Caverphone">Wikipedia - Caverphone</a>
* @see <a href="http://caversham.otago.ac.nz/files/working/ctp060902.pdf">Caverphone 1.0 specification</a>
* @since 1.5
*/
public class Caverphone1 extends AbstractCaverphone {
private static final String SIX_1 = "111111";
/**
* Encodes the given String into a Caverphone value.
*
* @param source
* String the source string
* @return A caverphone code for the given String
*/
public String encode(String source) {
String txt = source;
if (txt == null || txt.length() == 0) {
return SIX_1;
}
// 1. Convert to lowercase
txt = txt.toLowerCase(java.util.Locale.ENGLISH);
// 2. Remove anything not A-Z
txt = txt.replaceAll("[^a-z]", "");
// 3. Handle various start options
// 2 is a temporary placeholder to indicate a consonant which we are no longer interested in.
txt = txt.replaceAll("^cough", "cou2f");
txt = txt.replaceAll("^rough", "rou2f");
txt = txt.replaceAll("^tough", "tou2f");
txt = txt.replaceAll("^enough", "enou2f");
txt = txt.replaceAll("^gn", "2n");
// End
txt = txt.replaceAll("mb$", "m2");
// 4. Handle replacements
txt = txt.replaceAll("cq", "2q");
txt = txt.replaceAll("ci", "si");
txt = txt.replaceAll("ce", "se");
txt = txt.replaceAll("cy", "sy");
txt = txt.replaceAll("tch", "2ch");
txt = txt.replaceAll("c", "k");
txt = txt.replaceAll("q", "k");
txt = txt.replaceAll("x", "k");
txt = txt.replaceAll("v", "f");
txt = txt.replaceAll("dg", "2g");
txt = txt.replaceAll("tio", "sio");
txt = txt.replaceAll("tia", "sia");
txt = txt.replaceAll("d", "t");
txt = txt.replaceAll("ph", "fh");
txt = txt.replaceAll("b", "p");
txt = txt.replaceAll("sh", "s2");
txt = txt.replaceAll("z", "s");
txt = txt.replaceAll("^[aeiou]", "A");
// 3 is a temporary placeholder marking a vowel
txt = txt.replaceAll("[aeiou]", "3");
txt = txt.replaceAll("3gh3", "3kh3");
txt = txt.replaceAll("gh", "22");
txt = txt.replaceAll("g", "k");
txt = txt.replaceAll("s+", "S");
txt = txt.replaceAll("t+", "T");
txt = txt.replaceAll("p+", "P");
txt = txt.replaceAll("k+", "K");
txt = txt.replaceAll("f+", "F");
txt = txt.replaceAll("m+", "M");
txt = txt.replaceAll("n+", "N");
txt = txt.replaceAll("w3", "W3");
txt = txt.replaceAll("wy", "Wy"); // 1.0 only
txt = txt.replaceAll("wh3", "Wh3");
txt = txt.replaceAll("why", "Why"); // 1.0 only
txt = txt.replaceAll("w", "2");
txt = txt.replaceAll("^h", "A");
txt = txt.replaceAll("h", "2");
txt = txt.replaceAll("r3", "R3");
txt = txt.replaceAll("ry", "Ry"); // 1.0 only
txt = txt.replaceAll("r", "2");
txt = txt.replaceAll("l3", "L3");
txt = txt.replaceAll("ly", "Ly"); // 1.0 only
txt = txt.replaceAll("l", "2");
txt = txt.replaceAll("j", "y"); // 1.0 only
txt = txt.replaceAll("y3", "Y3"); // 1.0 only
txt = txt.replaceAll("y", "2"); // 1.0 only
// 5. Handle removals
txt = txt.replaceAll("2", "");
txt = txt.replaceAll("3", "");
// 6. put ten 1s on the end
txt = txt + SIX_1;
// 7. take the first six characters as the code
return txt.substring(0, SIX_1.length());
}
}

Просмотреть файл

@ -0,0 +1,129 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.language;
/**
* Encodes a string into a Caverphone 2.0 value.
*
* This is an algorithm created by the Caversham Project at the University of Otago. It implements the Caverphone 2.0
* algorithm:
*
* @author Apache Software Foundation
* @version $Id: Caverphone.java 1075947 2011-03-01 17:56:14Z ggregory $
* @see <a href="http://en.wikipedia.org/wiki/Caverphone">Wikipedia - Caverphone</a>
* @see <a href="http://caversham.otago.ac.nz/files/working/ctp150804.pdf">Caverphone 2.0 specification</a>
* @since 1.5
*/
public class Caverphone2 extends AbstractCaverphone {
private static final String TEN_1 = "1111111111";
/**
* Encodes the given String into a Caverphone 2.0 value.
*
* @param source
* String the source string
* @return A caverphone code for the given String
*/
public String encode(String source) {
String txt = source;
if (txt == null || txt.length() == 0) {
return TEN_1;
}
// 1. Convert to lowercase
txt = txt.toLowerCase(java.util.Locale.ENGLISH);
// 2. Remove anything not A-Z
txt = txt.replaceAll("[^a-z]", "");
// 2.5. Remove final e
txt = txt.replaceAll("e$", ""); // 2.0 only
// 3. Handle various start options
txt = txt.replaceAll("^cough", "cou2f");
txt = txt.replaceAll("^rough", "rou2f");
txt = txt.replaceAll("^tough", "tou2f");
txt = txt.replaceAll("^enough", "enou2f"); // 2.0 only
txt = txt.replaceAll("^trough", "trou2f"); // 2.0 only - note the spec says ^enough here again, c+p error I assume
txt = txt.replaceAll("^gn", "2n");
// End
txt = txt.replaceAll("mb$", "m2");
// 4. Handle replacements
txt = txt.replaceAll("cq", "2q");
txt = txt.replaceAll("ci", "si");
txt = txt.replaceAll("ce", "se");
txt = txt.replaceAll("cy", "sy");
txt = txt.replaceAll("tch", "2ch");
txt = txt.replaceAll("c", "k");
txt = txt.replaceAll("q", "k");
txt = txt.replaceAll("x", "k");
txt = txt.replaceAll("v", "f");
txt = txt.replaceAll("dg", "2g");
txt = txt.replaceAll("tio", "sio");
txt = txt.replaceAll("tia", "sia");
txt = txt.replaceAll("d", "t");
txt = txt.replaceAll("ph", "fh");
txt = txt.replaceAll("b", "p");
txt = txt.replaceAll("sh", "s2");
txt = txt.replaceAll("z", "s");
txt = txt.replaceAll("^[aeiou]", "A");
txt = txt.replaceAll("[aeiou]", "3");
txt = txt.replaceAll("j", "y"); // 2.0 only
txt = txt.replaceAll("^y3", "Y3"); // 2.0 only
txt = txt.replaceAll("^y", "A"); // 2.0 only
txt = txt.replaceAll("y", "3"); // 2.0 only
txt = txt.replaceAll("3gh3", "3kh3");
txt = txt.replaceAll("gh", "22");
txt = txt.replaceAll("g", "k");
txt = txt.replaceAll("s+", "S");
txt = txt.replaceAll("t+", "T");
txt = txt.replaceAll("p+", "P");
txt = txt.replaceAll("k+", "K");
txt = txt.replaceAll("f+", "F");
txt = txt.replaceAll("m+", "M");
txt = txt.replaceAll("n+", "N");
txt = txt.replaceAll("w3", "W3");
txt = txt.replaceAll("wh3", "Wh3");
txt = txt.replaceAll("w$", "3"); // 2.0 only
txt = txt.replaceAll("w", "2");
txt = txt.replaceAll("^h", "A");
txt = txt.replaceAll("h", "2");
txt = txt.replaceAll("r3", "R3");
txt = txt.replaceAll("r$", "3"); // 2.0 only
txt = txt.replaceAll("r", "2");
txt = txt.replaceAll("l3", "L3");
txt = txt.replaceAll("l$", "3"); // 2.0 only
txt = txt.replaceAll("l", "2");
// 5. Handle removals
txt = txt.replaceAll("2", "");
txt = txt.replaceAll("3$", "A"); // 2.0 only
txt = txt.replaceAll("3", "");
// 6. put ten 1s on the end
txt = txt + TEN_1;
// 7. take the first ten characters as the code
return txt.substring(0, TEN_1.length());
}
}

Просмотреть файл

@ -0,0 +1,417 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.language;
import java.util.Locale;
import org.mozilla.apache.commons.codec.EncoderException;
import org.mozilla.apache.commons.codec.StringEncoder;
/**
* <p>
* Encodes a string into a Cologne Phonetic value.
* </p>
* <p>
* Implements the <a href="http://de.wikipedia.org/wiki/K%C3%B6lner_Phonetik">Kölner Phonetic</a> (Cologne Phonetic)
* algorithm issued by Hans Joachim Postel in 1969.
* </p>
*
* <p>
* The <i>Kölner Phonetik</i> is a phonetic algorithm which is optimized for the German language. It is related to the
* well-known soundex algorithm.
* </p>
*
* <h2>Algorithm</h2>
*
* <ul>
*
* <li>
* <h3>Step 1:</h3>
* After preprocessing (convertion to upper case, transcription of <a
* href="http://en.wikipedia.org/wiki/Germanic_umlaut">germanic umlauts</a>, removal of non alphabetical characters) the
* letters of the supplied text are replaced by their phonetic code according to the folowing table.
* <table border="1">
* <tbody>
* <tr>
* <th>Letter</th>
* <th>Context</th>
* <th align="center">Code</th>
* </tr>
* <tr>
* <td>A, E, I, J, O, U, Y</td>
* <td></td>
* <td align="center">0</td>
* </tr>
* <tr>
*
* <td>H</td>
* <td></td>
* <td align="center">-</td>
* </tr>
* <tr>
* <td>B</td>
* <td></td>
* <td rowspan="2" align="center">1</td>
* </tr>
* <tr>
* <td>P</td>
* <td>not before H</td>
*
* </tr>
* <tr>
* <td>D, T</td>
* <td>not before C, S, Z</td>
* <td align="center">2</td>
* </tr>
* <tr>
* <td>F, V, W</td>
* <td></td>
* <td rowspan="2" align="center">3</td>
* </tr>
* <tr>
*
* <td>P</td>
* <td>before H</td>
* </tr>
* <tr>
* <td>G, K, Q</td>
* <td></td>
* <td rowspan="3" align="center">4</td>
* </tr>
* <tr>
* <td rowspan="2">C</td>
* <td>at onset before A, H, K, L, O, Q, R, U, X</td>
*
* </tr>
* <tr>
* <td>before A, H, K, O, Q, U, X except after S, Z</td>
* </tr>
* <tr>
* <td>X</td>
* <td>not after C, K, Q</td>
* <td align="center">48</td>
* </tr>
* <tr>
* <td>L</td>
* <td></td>
*
* <td align="center">5</td>
* </tr>
* <tr>
* <td>M, N</td>
* <td></td>
* <td align="center">6</td>
* </tr>
* <tr>
* <td>R</td>
* <td></td>
* <td align="center">7</td>
* </tr>
*
* <tr>
* <td>S, Z</td>
* <td></td>
* <td rowspan="6" align="center">8</td>
* </tr>
* <tr>
* <td rowspan="3">C</td>
* <td>after S, Z</td>
* </tr>
* <tr>
* <td>at onset except before A, H, K, L, O, Q, R, U, X</td>
* </tr>
*
* <tr>
* <td>not before A, H, K, O, Q, U, X</td>
* </tr>
* <tr>
* <td>D, T</td>
* <td>before C, S, Z</td>
* </tr>
* <tr>
* <td>X</td>
* <td>after C, K, Q</td>
* </tr>
* </tbody>
* </table>
* <p>
* <small><i>(Source: <a href= "http://de.wikipedia.org/wiki/K%C3%B6lner_Phonetik#Buchstabencodes" >Wikipedia (de):
* Kölner Phonetik Buchstabencodes</a>)</i></small>
* </p>
*
* <h4>Example:</h4>
*
* {@code "Müller-Lüdenscheidt" => "MULLERLUDENSCHEIDT" => "6005507500206880022"}
*
* </li>
*
* <li>
* <h3>Step 2:</h3>
* Collapse of all multiple consecutive code digits.
* <h4>Example:</h4>
* {@code "6005507500206880022" => "6050750206802"}</li>
*
* <li>
* <h3>Step 3:</h3>
* Removal of all codes 0 except at the beginning. This means that two or more identical consecutive digits can occur
* if they occur after removing the "0" digits.
*
* <h4>Example:</h4>
* {@code "6050750206802" => "65752682"}</li>
*
* </ul>
*
* @see <a href="http://de.wikipedia.org/wiki/K%C3%B6lner_Phonetik">Wikipedia (de): Kölner Phonetik (in German)</a>
* @author Apache Software Foundation
* @since 1.5
*/
public class ColognePhonetic implements StringEncoder {
private abstract class CologneBuffer {
protected final char[] data;
protected int length = 0;
public CologneBuffer(char[] data) {
this.data = data;
this.length = data.length;
}
public CologneBuffer(int buffSize) {
this.data = new char[buffSize];
this.length = 0;
}
protected abstract char[] copyData(int start, final int length);
public int length() {
return length;
}
public String toString() {
return new String(copyData(0, length));
}
}
private class CologneOutputBuffer extends CologneBuffer {
public CologneOutputBuffer(int buffSize) {
super(buffSize);
}
public void addRight(char chr) {
data[length] = chr;
length++;
}
protected char[] copyData(int start, final int length) {
char[] newData = new char[length];
System.arraycopy(data, start, newData, 0, length);
return newData;
}
}
private class CologneInputBuffer extends CologneBuffer {
public CologneInputBuffer(char[] data) {
super(data);
}
public void addLeft(char ch) {
length++;
data[getNextPos()] = ch;
}
protected char[] copyData(int start, final int length) {
char[] newData = new char[length];
System.arraycopy(data, data.length - this.length + start, newData, 0, length);
return newData;
}
public char getNextChar() {
return data[getNextPos()];
}
protected int getNextPos() {
return data.length - length;
}
public char removeNext() {
char ch = getNextChar();
length--;
return ch;
}
}
private static final char[][] PREPROCESS_MAP = new char[][]{{'\u00C4', 'A'}, // Ä
{'\u00DC', 'U'}, // Ü
{'\u00D6', 'O'}, // Ö
{'\u00DF', 'S'} // ß
};
/*
* Returns whether the array contains the key, or not.
*/
private static boolean arrayContains(char[] arr, char key) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
return true;
}
}
return false;
}
/**
* <p>
* <b>colognePhonetic()</b> is the actual implementations of the <i>Kölner Phonetik</i> algorithm.
* </p>
* <p>
* In contrast to the initial description of the algorithm, this implementation does the encoding in one pass.
* </p>
*
* @param text
* @return the corresponding encoding according to the <i>Kölner Phonetik</i> algorithm
*/
public String colognePhonetic(String text) {
if (text == null) {
return null;
}
text = preprocess(text);
CologneOutputBuffer output = new CologneOutputBuffer(text.length() * 2);
CologneInputBuffer input = new CologneInputBuffer(text.toCharArray());
char nextChar;
char lastChar = '-';
char lastCode = '/';
char code;
char chr;
int rightLength = input.length();
while (rightLength > 0) {
chr = input.removeNext();
if ((rightLength = input.length()) > 0) {
nextChar = input.getNextChar();
} else {
nextChar = '-';
}
if (arrayContains(new char[]{'A', 'E', 'I', 'J', 'O', 'U', 'Y'}, chr)) {
code = '0';
} else if (chr == 'H' || chr < 'A' || chr > 'Z') {
if (lastCode == '/') {
continue;
}
code = '-';
} else if (chr == 'B' || (chr == 'P' && nextChar != 'H')) {
code = '1';
} else if ((chr == 'D' || chr == 'T') && !arrayContains(new char[]{'S', 'C', 'Z'}, nextChar)) {
code = '2';
} else if (arrayContains(new char[]{'W', 'F', 'P', 'V'}, chr)) {
code = '3';
} else if (arrayContains(new char[]{'G', 'K', 'Q'}, chr)) {
code = '4';
} else if (chr == 'X' && !arrayContains(new char[]{'C', 'K', 'Q'}, lastChar)) {
code = '4';
input.addLeft('S');
rightLength++;
} else if (chr == 'S' || chr == 'Z') {
code = '8';
} else if (chr == 'C') {
if (lastCode == '/') {
if (arrayContains(new char[]{'A', 'H', 'K', 'L', 'O', 'Q', 'R', 'U', 'X'}, nextChar)) {
code = '4';
} else {
code = '8';
}
} else {
if (arrayContains(new char[]{'S', 'Z'}, lastChar) ||
!arrayContains(new char[]{'A', 'H', 'O', 'U', 'K', 'Q', 'X'}, nextChar)) {
code = '8';
} else {
code = '4';
}
}
} else if (arrayContains(new char[]{'T', 'D', 'X'}, chr)) {
code = '8';
} else if (chr == 'R') {
code = '7';
} else if (chr == 'L') {
code = '5';
} else if (chr == 'M' || chr == 'N') {
code = '6';
} else {
code = chr;
}
if (code != '-' && (lastCode != code && (code != '0' || lastCode == '/') || code < '0' || code > '8')) {
output.addRight(code);
}
lastChar = chr;
lastCode = code;
}
return output.toString();
}
public Object encode(Object object) throws EncoderException {
if (!(object instanceof String)) {
throw new EncoderException("This methods parameter was expected to be of the type " +
String.class.getName() +
". But actually it was of the type " +
object.getClass().getName() +
".");
}
return encode((String) object);
}
public String encode(String text) {
return colognePhonetic(text);
}
public boolean isEncodeEqual(String text1, String text2) {
return colognePhonetic(text1).equals(colognePhonetic(text2));
}
/*
* Converts the string to upper case and replaces germanic umlauts, and the ß.
*/
private String preprocess(String text) {
text = text.toUpperCase(Locale.GERMAN);
char[] chrs = text.toCharArray();
for (int index = 0; index < chrs.length; index++) {
if (chrs[index] > 'Z') {
for (int replacement = 0; replacement < PREPROCESS_MAP.length; replacement++) {
if (chrs[index] == PREPROCESS_MAP[replacement][0]) {
chrs[index] = PREPROCESS_MAP[replacement][1];
break;
}
}
}
}
return new String(chrs);
}
}

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -0,0 +1,408 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.language;
import org.mozilla.apache.commons.codec.EncoderException;
import org.mozilla.apache.commons.codec.StringEncoder;
/**
* Encodes a string into a Metaphone value.
* <p>
* Initial Java implementation by <CITE>William B. Brogden. December, 1997</CITE>.
* Permission given by <CITE>wbrogden</CITE> for code to be used anywhere.
* </p>
* <p>
* <CITE>Hanging on the Metaphone</CITE> by <CITE>Lawrence Philips</CITE> in <CITE>Computer Language of Dec. 1990, p
* 39.</CITE>
* </p>
* <p>
* Note, that this does not match the algorithm that ships with PHP, or the algorithm
* found in the Perl <a href="http://search.cpan.org/~mschwern/Text-Metaphone-1.96/Metaphone.pm">Text:Metaphone-1.96</a>.
* They have had undocumented changes from the originally published algorithm.
* For more information, see <a href="https://issues.apache.org/jira/browse/CODEC-57">CODEC-57</a>.
* </p>
*
* @author Apache Software Foundation
* @version $Id: Metaphone.java 1080867 2011-03-12 06:06:46Z ggregory $
*/
public class Metaphone implements StringEncoder {
/**
* Five values in the English language
*/
private static final String VOWELS = "AEIOU" ;
/**
* Variable used in Metaphone algorithm
*/
private static final String FRONTV = "EIY" ;
/**
* Variable used in Metaphone algorithm
*/
private static final String VARSON = "CSPTG" ;
/**
* The max code length for metaphone is 4
*/
private int maxCodeLen = 4 ;
/**
* Creates an instance of the Metaphone encoder
*/
public Metaphone() {
super();
}
/**
* Find the metaphone value of a String. This is similar to the
* soundex algorithm, but better at finding similar sounding words.
* All input is converted to upper case.
* Limitations: Input format is expected to be a single ASCII word
* with only characters in the A - Z range, no punctuation or numbers.
*
* @param txt String to find the metaphone code for
* @return A metaphone code corresponding to the String supplied
*/
public String metaphone(String txt) {
boolean hard = false ;
if ((txt == null) || (txt.length() == 0)) {
return "" ;
}
// single character is itself
if (txt.length() == 1) {
return txt.toUpperCase(java.util.Locale.ENGLISH) ;
}
char[] inwd = txt.toUpperCase(java.util.Locale.ENGLISH).toCharArray() ;
StringBuffer local = new StringBuffer(40); // manipulate
StringBuffer code = new StringBuffer(10) ; // output
// handle initial 2 characters exceptions
switch(inwd[0]) {
case 'K' :
case 'G' :
case 'P' : /* looking for KN, etc*/
if (inwd[1] == 'N') {
local.append(inwd, 1, inwd.length - 1);
} else {
local.append(inwd);
}
break;
case 'A': /* looking for AE */
if (inwd[1] == 'E') {
local.append(inwd, 1, inwd.length - 1);
} else {
local.append(inwd);
}
break;
case 'W' : /* looking for WR or WH */
if (inwd[1] == 'R') { // WR -> R
local.append(inwd, 1, inwd.length - 1);
break ;
}
if (inwd[1] == 'H') {
local.append(inwd, 1, inwd.length - 1);
local.setCharAt(0, 'W'); // WH -> W
} else {
local.append(inwd);
}
break;
case 'X' : /* initial X becomes S */
inwd[0] = 'S';
local.append(inwd);
break ;
default :
local.append(inwd);
} // now local has working string with initials fixed
int wdsz = local.length();
int n = 0 ;
while ((code.length() < this.getMaxCodeLen()) &&
(n < wdsz) ) { // max code size of 4 works well
char symb = local.charAt(n) ;
// remove duplicate letters except C
if ((symb != 'C') && (isPreviousChar( local, n, symb )) ) {
n++ ;
} else { // not dup
switch(symb) {
case 'A' : case 'E' : case 'I' : case 'O' : case 'U' :
if (n == 0) {
code.append(symb);
}
break ; // only use vowel if leading char
case 'B' :
if ( isPreviousChar(local, n, 'M') &&
isLastChar(wdsz, n) ) { // B is silent if word ends in MB
break;
}
code.append(symb);
break;
case 'C' : // lots of C special cases
/* discard if SCI, SCE or SCY */
if ( isPreviousChar(local, n, 'S') &&
!isLastChar(wdsz, n) &&
(FRONTV.indexOf(local.charAt(n + 1)) >= 0) ) {
break;
}
if (regionMatch(local, n, "CIA")) { // "CIA" -> X
code.append('X');
break;
}
if (!isLastChar(wdsz, n) &&
(FRONTV.indexOf(local.charAt(n + 1)) >= 0)) {
code.append('S');
break; // CI,CE,CY -> S
}
if (isPreviousChar(local, n, 'S') &&
isNextChar(local, n, 'H') ) { // SCH->sk
code.append('K') ;
break ;
}
if (isNextChar(local, n, 'H')) { // detect CH
if ((n == 0) &&
(wdsz >= 3) &&
isVowel(local,2) ) { // CH consonant -> K consonant
code.append('K');
} else {
code.append('X'); // CHvowel -> X
}
} else {
code.append('K');
}
break ;
case 'D' :
if (!isLastChar(wdsz, n + 1) &&
isNextChar(local, n, 'G') &&
(FRONTV.indexOf(local.charAt(n + 2)) >= 0)) { // DGE DGI DGY -> J
code.append('J'); n += 2 ;
} else {
code.append('T');
}
break ;
case 'G' : // GH silent at end or before consonant
if (isLastChar(wdsz, n + 1) &&
isNextChar(local, n, 'H')) {
break;
}
if (!isLastChar(wdsz, n + 1) &&
isNextChar(local,n,'H') &&
!isVowel(local,n+2)) {
break;
}
if ((n > 0) &&
( regionMatch(local, n, "GN") ||
regionMatch(local, n, "GNED") ) ) {
break; // silent G
}
if (isPreviousChar(local, n, 'G')) {
// NOTE: Given that duplicated chars are removed, I don't see how this can ever be true
hard = true ;
} else {
hard = false ;
}
if (!isLastChar(wdsz, n) &&
(FRONTV.indexOf(local.charAt(n + 1)) >= 0) &&
(!hard)) {
code.append('J');
} else {
code.append('K');
}
break ;
case 'H':
if (isLastChar(wdsz, n)) {
break ; // terminal H
}
if ((n > 0) &&
(VARSON.indexOf(local.charAt(n - 1)) >= 0)) {
break;
}
if (isVowel(local,n+1)) {
code.append('H'); // Hvowel
}
break;
case 'F':
case 'J' :
case 'L' :
case 'M':
case 'N' :
case 'R' :
code.append(symb);
break;
case 'K' :
if (n > 0) { // not initial
if (!isPreviousChar(local, n, 'C')) {
code.append(symb);
}
} else {
code.append(symb); // initial K
}
break ;
case 'P' :
if (isNextChar(local,n,'H')) {
// PH -> F
code.append('F');
} else {
code.append(symb);
}
break ;
case 'Q' :
code.append('K');
break;
case 'S' :
if (regionMatch(local,n,"SH") ||
regionMatch(local,n,"SIO") ||
regionMatch(local,n,"SIA")) {
code.append('X');
} else {
code.append('S');
}
break;
case 'T' :
if (regionMatch(local,n,"TIA") ||
regionMatch(local,n,"TIO")) {
code.append('X');
break;
}
if (regionMatch(local,n,"TCH")) {
// Silent if in "TCH"
break;
}
// substitute numeral 0 for TH (resembles theta after all)
if (regionMatch(local,n,"TH")) {
code.append('0');
} else {
code.append('T');
}
break ;
case 'V' :
code.append('F'); break ;
case 'W' : case 'Y' : // silent if not followed by vowel
if (!isLastChar(wdsz,n) &&
isVowel(local,n+1)) {
code.append(symb);
}
break ;
case 'X' :
code.append('K'); code.append('S');
break ;
case 'Z' :
code.append('S'); break ;
} // end switch
n++ ;
} // end else from symb != 'C'
if (code.length() > this.getMaxCodeLen()) {
code.setLength(this.getMaxCodeLen());
}
}
return code.toString();
}
private boolean isVowel(StringBuffer string, int index) {
return VOWELS.indexOf(string.charAt(index)) >= 0;
}
private boolean isPreviousChar(StringBuffer string, int index, char c) {
boolean matches = false;
if( index > 0 &&
index < string.length() ) {
matches = string.charAt(index - 1) == c;
}
return matches;
}
private boolean isNextChar(StringBuffer string, int index, char c) {
boolean matches = false;
if( index >= 0 &&
index < string.length() - 1 ) {
matches = string.charAt(index + 1) == c;
}
return matches;
}
private boolean regionMatch(StringBuffer string, int index, String test) {
boolean matches = false;
if( index >= 0 &&
(index + test.length() - 1) < string.length() ) {
String substring = string.substring( index, index + test.length());
matches = substring.equals( test );
}
return matches;
}
private boolean isLastChar(int wdsz, int n) {
return n + 1 == wdsz;
}
/**
* Encodes an Object using the metaphone algorithm. This method
* is provided in order to satisfy the requirements of the
* Encoder interface, and will throw an EncoderException if the
* supplied object is not of type java.lang.String.
*
* @param pObject Object to encode
* @return An object (or type java.lang.String) containing the
* metaphone code which corresponds to the String supplied.
* @throws EncoderException if the parameter supplied is not
* of type java.lang.String
*/
public Object encode(Object pObject) throws EncoderException {
if (!(pObject instanceof String)) {
throw new EncoderException("Parameter supplied to Metaphone encode is not of type java.lang.String");
}
return metaphone((String) pObject);
}
/**
* Encodes a String using the Metaphone algorithm.
*
* @param pString String object to encode
* @return The metaphone code corresponding to the String supplied
*/
public String encode(String pString) {
return metaphone(pString);
}
/**
* Tests is the metaphones of two strings are identical.
*
* @param str1 First of two strings to compare
* @param str2 Second of two strings to compare
* @return <code>true</code> if the metaphones of these strings are identical,
* <code>false</code> otherwise.
*/
public boolean isMetaphoneEqual(String str1, String str2) {
return metaphone(str1).equals(metaphone(str2));
}
/**
* Returns the maxCodeLen.
* @return int
*/
public int getMaxCodeLen() { return this.maxCodeLen; }
/**
* Sets the maxCodeLen.
* @param maxCodeLen The maxCodeLen to set
*/
public void setMaxCodeLen(int maxCodeLen) { this.maxCodeLen = maxCodeLen; }
}

Просмотреть файл

@ -0,0 +1,203 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.language;
import org.mozilla.apache.commons.codec.EncoderException;
import org.mozilla.apache.commons.codec.StringEncoder;
/**
* Encodes a string into a Refined Soundex value. A refined soundex code is
* optimized for spell checking words. Soundex method originally developed by
* <CITE>Margaret Odell</CITE> and <CITE>Robert Russell</CITE>.
*
* @author Apache Software Foundation
* @version $Id: RefinedSoundex.java 1064455 2011-01-28 04:40:27Z ggregory $
*/
public class RefinedSoundex implements StringEncoder {
/**
* @since 1.4
*/
public static final String US_ENGLISH_MAPPING_STRING = "01360240043788015936020505";
/**
* RefinedSoundex is *refined* for a number of reasons one being that the
* mappings have been altered. This implementation contains default
* mappings for US English.
*/
private static final char[] US_ENGLISH_MAPPING = US_ENGLISH_MAPPING_STRING.toCharArray();
/**
* Every letter of the alphabet is "mapped" to a numerical value. This char
* array holds the values to which each letter is mapped. This
* implementation contains a default map for US_ENGLISH
*/
private final char[] soundexMapping;
/**
* This static variable contains an instance of the RefinedSoundex using
* the US_ENGLISH mapping.
*/
public static final RefinedSoundex US_ENGLISH = new RefinedSoundex();
/**
* Creates an instance of the RefinedSoundex object using the default US
* English mapping.
*/
public RefinedSoundex() {
this.soundexMapping = US_ENGLISH_MAPPING;
}
/**
* Creates a refined soundex instance using a custom mapping. This
* constructor can be used to customize the mapping, and/or possibly
* provide an internationalized mapping for a non-Western character set.
*
* @param mapping
* Mapping array to use when finding the corresponding code for
* a given character
*/
public RefinedSoundex(char[] mapping) {
this.soundexMapping = new char[mapping.length];
System.arraycopy(mapping, 0, this.soundexMapping, 0, mapping.length);
}
/**
* Creates a refined Soundex instance using a custom mapping. This constructor can be used to customize the mapping,
* and/or possibly provide an internationalized mapping for a non-Western character set.
*
* @param mapping
* Mapping string to use when finding the corresponding code for a given character
* @since 1.4
*/
public RefinedSoundex(String mapping) {
this.soundexMapping = mapping.toCharArray();
}
/**
* Returns the number of characters in the two encoded Strings that are the
* same. This return value ranges from 0 to the length of the shortest
* encoded String: 0 indicates little or no similarity, and 4 out of 4 (for
* example) indicates strong similarity or identical values. For refined
* Soundex, the return value can be greater than 4.
*
* @param s1
* A String that will be encoded and compared.
* @param s2
* A String that will be encoded and compared.
* @return The number of characters in the two encoded Strings that are the
* same from 0 to to the length of the shortest encoded String.
*
* @see SoundexUtils#difference(StringEncoder,String,String)
* @see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp">
* MS T-SQL DIFFERENCE</a>
*
* @throws EncoderException
* if an error occurs encoding one of the strings
* @since 1.3
*/
public int difference(String s1, String s2) throws EncoderException {
return SoundexUtils.difference(this, s1, s2);
}
/**
* Encodes an Object using the refined soundex algorithm. This method is
* provided in order to satisfy the requirements of the Encoder interface,
* and will throw an EncoderException if the supplied object is not of type
* java.lang.String.
*
* @param pObject
* Object to encode
* @return An object (or type java.lang.String) containing the refined
* soundex code which corresponds to the String supplied.
* @throws EncoderException
* if the parameter supplied is not of type java.lang.String
*/
public Object encode(Object pObject) throws EncoderException {
if (!(pObject instanceof String)) {
throw new EncoderException("Parameter supplied to RefinedSoundex encode is not of type java.lang.String");
}
return soundex((String) pObject);
}
/**
* Encodes a String using the refined soundex algorithm.
*
* @param pString
* A String object to encode
* @return A Soundex code corresponding to the String supplied
*/
public String encode(String pString) {
return soundex(pString);
}
/**
* Returns the mapping code for a given character. The mapping codes are
* maintained in an internal char array named soundexMapping, and the
* default values of these mappings are US English.
*
* @param c
* char to get mapping for
* @return A character (really a numeral) to return for the given char
*/
char getMappingCode(char c) {
if (!Character.isLetter(c)) {
return 0;
}
return this.soundexMapping[Character.toUpperCase(c) - 'A'];
}
/**
* Retreives the Refined Soundex code for a given String object.
*
* @param str
* String to encode using the Refined Soundex algorithm
* @return A soundex code for the String supplied
*/
public String soundex(String str) {
if (str == null) {
return null;
}
str = SoundexUtils.clean(str);
if (str.length() == 0) {
return str;
}
StringBuffer sBuf = new StringBuffer();
sBuf.append(str.charAt(0));
char last, current;
last = '*';
for (int i = 0; i < str.length(); i++) {
current = getMappingCode(str.charAt(i));
if (current == last) {
continue;
} else if (current != 0) {
sBuf.append(current);
}
last = current;
}
return sBuf.toString();
}
}

Просмотреть файл

@ -0,0 +1,279 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.language;
import org.mozilla.apache.commons.codec.EncoderException;
import org.mozilla.apache.commons.codec.StringEncoder;
/**
* Encodes a string into a Soundex value. Soundex is an encoding used to relate similar names, but can also be used as a
* general purpose scheme to find word with similar phonemes.
*
* @author Apache Software Foundation
* @version $Id: Soundex.java 1064454 2011-01-28 04:40:02Z ggregory $
*/
public class Soundex implements StringEncoder {
/**
* This is a default mapping of the 26 letters used in US English. A value of <code>0</code> for a letter position
* means do not encode.
* <p>
* (This constant is provided as both an implementation convenience and to allow Javadoc to pick
* up the value for the constant values page.)
* </p>
*
* @see #US_ENGLISH_MAPPING
*/
public static final String US_ENGLISH_MAPPING_STRING = "01230120022455012623010202";
/**
* This is a default mapping of the 26 letters used in US English. A value of <code>0</code> for a letter position
* means do not encode.
*
* @see Soundex#Soundex(char[])
*/
private static final char[] US_ENGLISH_MAPPING = US_ENGLISH_MAPPING_STRING.toCharArray();
/**
* An instance of Soundex using the US_ENGLISH_MAPPING mapping.
*
* @see #US_ENGLISH_MAPPING
*/
public static final Soundex US_ENGLISH = new Soundex();
/**
* Encodes the Strings and returns the number of characters in the two encoded Strings that are the same. This
* return value ranges from 0 through 4: 0 indicates little or no similarity, and 4 indicates strong similarity or
* identical values.
*
* @param s1
* A String that will be encoded and compared.
* @param s2
* A String that will be encoded and compared.
* @return The number of characters in the two encoded Strings that are the same from 0 to 4.
*
* @see SoundexUtils#difference(StringEncoder,String,String)
* @see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp"> MS
* T-SQL DIFFERENCE </a>
*
* @throws EncoderException
* if an error occurs encoding one of the strings
* @since 1.3
*/
public int difference(String s1, String s2) throws EncoderException {
return SoundexUtils.difference(this, s1, s2);
}
/**
* The maximum length of a Soundex code - Soundex codes are only four characters by definition.
*
* @deprecated This feature is not needed since the encoding size must be constant. Will be removed in 2.0.
*/
private int maxLength = 4;
/**
* Every letter of the alphabet is "mapped" to a numerical value. This char array holds the values to which each
* letter is mapped. This implementation contains a default map for US_ENGLISH
*/
private final char[] soundexMapping;
/**
* Creates an instance using US_ENGLISH_MAPPING
*
* @see Soundex#Soundex(char[])
* @see Soundex#US_ENGLISH_MAPPING
*/
public Soundex() {
this.soundexMapping = US_ENGLISH_MAPPING;
}
/**
* Creates a soundex instance using the given mapping. This constructor can be used to provide an internationalized
* mapping for a non-Western character set.
*
* Every letter of the alphabet is "mapped" to a numerical value. This char array holds the values to which each
* letter is mapped. This implementation contains a default map for US_ENGLISH
*
* @param mapping
* Mapping array to use when finding the corresponding code for a given character
*/
public Soundex(char[] mapping) {
this.soundexMapping = new char[mapping.length];
System.arraycopy(mapping, 0, this.soundexMapping, 0, mapping.length);
}
/**
* Creates a refined soundex instance using a custom mapping. This constructor can be used to customize the mapping,
* and/or possibly provide an internationalized mapping for a non-Western character set.
*
* @param mapping
* Mapping string to use when finding the corresponding code for a given character
* @since 1.4
*/
public Soundex(String mapping) {
this.soundexMapping = mapping.toCharArray();
}
/**
* Encodes an Object using the soundex algorithm. This method is provided in order to satisfy the requirements of
* the Encoder interface, and will throw an EncoderException if the supplied object is not of type java.lang.String.
*
* @param pObject
* Object to encode
* @return An object (or type java.lang.String) containing the soundex code which corresponds to the String
* supplied.
* @throws EncoderException
* if the parameter supplied is not of type java.lang.String
* @throws IllegalArgumentException
* if a character is not mapped
*/
public Object encode(Object pObject) throws EncoderException {
if (!(pObject instanceof String)) {
throw new EncoderException("Parameter supplied to Soundex encode is not of type java.lang.String");
}
return soundex((String) pObject);
}
/**
* Encodes a String using the soundex algorithm.
*
* @param pString
* A String object to encode
* @return A Soundex code corresponding to the String supplied
* @throws IllegalArgumentException
* if a character is not mapped
*/
public String encode(String pString) {
return soundex(pString);
}
/**
* Used internally by the SoundEx algorithm.
*
* Consonants from the same code group separated by W or H are treated as one.
*
* @param str
* the cleaned working string to encode (in upper case).
* @param index
* the character position to encode
* @return Mapping code for a particular character
* @throws IllegalArgumentException
* if the character is not mapped
*/
private char getMappingCode(String str, int index) {
// map() throws IllegalArgumentException
char mappedChar = this.map(str.charAt(index));
// HW rule check
if (index > 1 && mappedChar != '0') {
char hwChar = str.charAt(index - 1);
if ('H' == hwChar || 'W' == hwChar) {
char preHWChar = str.charAt(index - 2);
char firstCode = this.map(preHWChar);
if (firstCode == mappedChar || 'H' == preHWChar || 'W' == preHWChar) {
return 0;
}
}
}
return mappedChar;
}
/**
* Returns the maxLength. Standard Soundex
*
* @deprecated This feature is not needed since the encoding size must be constant. Will be removed in 2.0.
* @return int
*/
public int getMaxLength() {
return this.maxLength;
}
/**
* Returns the soundex mapping.
*
* @return soundexMapping.
*/
private char[] getSoundexMapping() {
return this.soundexMapping;
}
/**
* Maps the given upper-case character to its Soundex code.
*
* @param ch
* An upper-case character.
* @return A Soundex code.
* @throws IllegalArgumentException
* Thrown if <code>ch</code> is not mapped.
*/
private char map(char ch) {
int index = ch - 'A';
if (index < 0 || index >= this.getSoundexMapping().length) {
throw new IllegalArgumentException("The character is not mapped: " + ch);
}
return this.getSoundexMapping()[index];
}
/**
* Sets the maxLength.
*
* @deprecated This feature is not needed since the encoding size must be constant. Will be removed in 2.0.
* @param maxLength
* The maxLength to set
*/
public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
/**
* Retrieves the Soundex code for a given String object.
*
* @param str
* String to encode using the Soundex algorithm
* @return A soundex code for the String supplied
* @throws IllegalArgumentException
* if a character is not mapped
*/
public String soundex(String str) {
if (str == null) {
return null;
}
str = SoundexUtils.clean(str);
if (str.length() == 0) {
return str;
}
char out[] = {'0', '0', '0', '0'};
char last, mapped;
int incount = 1, count = 1;
out[0] = str.charAt(0);
// getMappingCode() throws IllegalArgumentException
last = getMappingCode(str, 0);
while ((incount < str.length()) && (count < out.length)) {
mapped = getMappingCode(str, incount++);
if (mapped != 0) {
if ((mapped != '0') && (mapped != last)) {
out[count++] = mapped;
}
last = mapped;
}
}
return new String(out);
}
}

Просмотреть файл

@ -0,0 +1,124 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.language;
import org.mozilla.apache.commons.codec.EncoderException;
import org.mozilla.apache.commons.codec.StringEncoder;
/**
* Utility methods for {@link Soundex} and {@link RefinedSoundex} classes.
*
* @author Apache Software Foundation
* @version $Id: SoundexUtils.java 658834 2008-05-21 19:57:51Z niallp $
* @since 1.3
*/
final class SoundexUtils {
/**
* Cleans up the input string before Soundex processing by only returning
* upper case letters.
*
* @param str
* The String to clean.
* @return A clean String.
*/
static String clean(String str) {
if (str == null || str.length() == 0) {
return str;
}
int len = str.length();
char[] chars = new char[len];
int count = 0;
for (int i = 0; i < len; i++) {
if (Character.isLetter(str.charAt(i))) {
chars[count++] = str.charAt(i);
}
}
if (count == len) {
return str.toUpperCase(java.util.Locale.ENGLISH);
}
return new String(chars, 0, count).toUpperCase(java.util.Locale.ENGLISH);
}
/**
* Encodes the Strings and returns the number of characters in the two
* encoded Strings that are the same.
* <ul>
* <li>For Soundex, this return value ranges from 0 through 4: 0 indicates
* little or no similarity, and 4 indicates strong similarity or identical
* values.</li>
* <li>For refined Soundex, the return value can be greater than 4.</li>
* </ul>
*
* @param encoder
* The encoder to use to encode the Strings.
* @param s1
* A String that will be encoded and compared.
* @param s2
* A String that will be encoded and compared.
* @return The number of characters in the two Soundex encoded Strings that
* are the same.
*
* @see #differenceEncoded(String,String)
* @see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp">
* MS T-SQL DIFFERENCE</a>
*
* @throws EncoderException
* if an error occurs encoding one of the strings
*/
static int difference(StringEncoder encoder, String s1, String s2) throws EncoderException {
return differenceEncoded(encoder.encode(s1), encoder.encode(s2));
}
/**
* Returns the number of characters in the two Soundex encoded Strings that
* are the same.
* <ul>
* <li>For Soundex, this return value ranges from 0 through 4: 0 indicates
* little or no similarity, and 4 indicates strong similarity or identical
* values.</li>
* <li>For refined Soundex, the return value can be greater than 4.</li>
* </ul>
*
* @param es1
* An encoded String.
* @param es2
* An encoded String.
* @return The number of characters in the two Soundex encoded Strings that
* are the same.
*
* @see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp">
* MS T-SQL DIFFERENCE</a>
*/
static int differenceEncoded(String es1, String es2) {
if (es1 == null || es2 == null) {
return 0;
}
int lengthToMatch = Math.min(es1.length(), es2.length());
int diff = 0;
for (int i = 0; i < lengthToMatch; i++) {
if (es1.charAt(i) == es2.charAt(i)) {
diff++;
}
}
return diff;
}
}

Просмотреть файл

@ -0,0 +1,21 @@
<!--
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.
-->
<html>
<body>
Language and phonetic encoders.
</body>
</html>

Просмотреть файл

@ -0,0 +1,209 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.net;
import java.io.UnsupportedEncodingException;
import org.mozilla.apache.commons.codec.DecoderException;
import org.mozilla.apache.commons.codec.EncoderException;
import org.mozilla.apache.commons.codec.CharEncoding;
import org.mozilla.apache.commons.codec.StringDecoder;
import org.mozilla.apache.commons.codec.StringEncoder;
import org.mozilla.apache.commons.codec.binary.Base64;
/**
* <p>
* Identical to the Base64 encoding defined by <a href="http://www.ietf.org/rfc/rfc1521.txt">RFC
* 1521</a> and allows a character set to be specified.
* </p>
*
* <p>
* <a href="http://www.ietf.org/rfc/rfc1522.txt">RFC 1522</a> describes techniques to allow the encoding of non-ASCII
* text in various portions of a RFC 822 [2] message header, in a manner which is unlikely to confuse existing message
* handling software.
* </p>
*
* @see <a href="http://www.ietf.org/rfc/rfc1522.txt">MIME (Multipurpose Internet Mail Extensions) Part Two: Message
* Header Extensions for Non-ASCII Text</a>
*
* @author Apache Software Foundation
* @since 1.3
* @version $Id: BCodec.java 797857 2009-07-25 23:43:33Z ggregory $
*/
public class BCodec extends RFC1522Codec implements StringEncoder, StringDecoder {
/**
* The default charset used for string decoding and encoding.
*/
private final String charset;
/**
* Default constructor.
*/
public BCodec() {
this(CharEncoding.UTF_8);
}
/**
* Constructor which allows for the selection of a default charset
*
* @param charset
* the default string charset to use.
*
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
*/
public BCodec(final String charset) {
super();
this.charset = charset;
}
protected String getEncoding() {
return "B";
}
protected byte[] doEncoding(byte[] bytes) {
if (bytes == null) {
return null;
}
return Base64.encodeBase64(bytes);
}
protected byte[] doDecoding(byte[] bytes) {
if (bytes == null) {
return null;
}
return Base64.decodeBase64(bytes);
}
/**
* Encodes a string into its Base64 form using the specified charset. Unsafe characters are escaped.
*
* @param value
* string to convert to Base64 form
* @param charset
* the charset for <code>value</code>
* @return Base64 string
*
* @throws EncoderException
* thrown if a failure condition is encountered during the encoding process.
*/
public String encode(final String value, final String charset) throws EncoderException {
if (value == null) {
return null;
}
try {
return encodeText(value, charset);
} catch (UnsupportedEncodingException e) {
throw new EncoderException(e.getMessage(), e);
}
}
/**
* Encodes a string into its Base64 form using the default charset. Unsafe characters are escaped.
*
* @param value
* string to convert to Base64 form
* @return Base64 string
*
* @throws EncoderException
* thrown if a failure condition is encountered during the encoding process.
*/
public String encode(String value) throws EncoderException {
if (value == null) {
return null;
}
return encode(value, getDefaultCharset());
}
/**
* Decodes a Base64 string into its original form. Escaped characters are converted back to their original
* representation.
*
* @param value
* Base64 string to convert into its original form
* @return original string
* @throws DecoderException
* A decoder exception is thrown if a failure condition is encountered during the decode process.
*/
public String decode(String value) throws DecoderException {
if (value == null) {
return null;
}
try {
return decodeText(value);
} catch (UnsupportedEncodingException e) {
throw new DecoderException(e.getMessage(), e);
}
}
/**
* Encodes an object into its Base64 form using the default charset. Unsafe characters are escaped.
*
* @param value
* object to convert to Base64 form
* @return Base64 object
*
* @throws EncoderException
* thrown if a failure condition is encountered during the encoding process.
*/
public Object encode(Object value) throws EncoderException {
if (value == null) {
return null;
} else if (value instanceof String) {
return encode((String) value);
} else {
throw new EncoderException("Objects of type " +
value.getClass().getName() +
" cannot be encoded using BCodec");
}
}
/**
* Decodes a Base64 object into its original form. Escaped characters are converted back to their original
* representation.
*
* @param value
* Base64 object to convert into its original form
*
* @return original object
*
* @throws DecoderException
* Thrown if the argument is not a <code>String</code>. Thrown if a failure condition is
* encountered during the decode process.
*/
public Object decode(Object value) throws DecoderException {
if (value == null) {
return null;
} else if (value instanceof String) {
return decode((String) value);
} else {
throw new DecoderException("Objects of type " +
value.getClass().getName() +
" cannot be decoded using BCodec");
}
}
/**
* The default charset used for string decoding and encoding.
*
* @return the default string charset.
*/
public String getDefaultCharset() {
return this.charset;
}
}

Просмотреть файл

@ -0,0 +1,312 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.net;
import java.io.UnsupportedEncodingException;
import java.util.BitSet;
import org.mozilla.apache.commons.codec.DecoderException;
import org.mozilla.apache.commons.codec.EncoderException;
import org.mozilla.apache.commons.codec.CharEncoding;
import org.mozilla.apache.commons.codec.StringDecoder;
import org.mozilla.apache.commons.codec.StringEncoder;
/**
* <p>
* Similar to the Quoted-Printable content-transfer-encoding defined in <a
* href="http://www.ietf.org/rfc/rfc1521.txt">RFC 1521</a> and designed to allow text containing mostly ASCII
* characters to be decipherable on an ASCII terminal without decoding.
* </p>
*
* <p>
* <a href="http://www.ietf.org/rfc/rfc1522.txt">RFC 1522</a> describes techniques to allow the encoding of non-ASCII
* text in various portions of a RFC 822 [2] message header, in a manner which is unlikely to confuse existing message
* handling software.
* </p>
*
* @see <a href="http://www.ietf.org/rfc/rfc1522.txt">MIME (Multipurpose Internet Mail Extensions) Part Two: Message
* Header Extensions for Non-ASCII Text</a>
*
* @author Apache Software Foundation
* @since 1.3
* @version $Id: QCodec.java 797857 2009-07-25 23:43:33Z ggregory $
*/
public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder {
/**
* The default charset used for string decoding and encoding.
*/
private final String charset;
/**
* BitSet of printable characters as defined in RFC 1522.
*/
private static final BitSet PRINTABLE_CHARS = new BitSet(256);
// Static initializer for printable chars collection
static {
// alpha characters
PRINTABLE_CHARS.set(' ');
PRINTABLE_CHARS.set('!');
PRINTABLE_CHARS.set('"');
PRINTABLE_CHARS.set('#');
PRINTABLE_CHARS.set('$');
PRINTABLE_CHARS.set('%');
PRINTABLE_CHARS.set('&');
PRINTABLE_CHARS.set('\'');
PRINTABLE_CHARS.set('(');
PRINTABLE_CHARS.set(')');
PRINTABLE_CHARS.set('*');
PRINTABLE_CHARS.set('+');
PRINTABLE_CHARS.set(',');
PRINTABLE_CHARS.set('-');
PRINTABLE_CHARS.set('.');
PRINTABLE_CHARS.set('/');
for (int i = '0'; i <= '9'; i++) {
PRINTABLE_CHARS.set(i);
}
PRINTABLE_CHARS.set(':');
PRINTABLE_CHARS.set(';');
PRINTABLE_CHARS.set('<');
PRINTABLE_CHARS.set('>');
PRINTABLE_CHARS.set('@');
for (int i = 'A'; i <= 'Z'; i++) {
PRINTABLE_CHARS.set(i);
}
PRINTABLE_CHARS.set('[');
PRINTABLE_CHARS.set('\\');
PRINTABLE_CHARS.set(']');
PRINTABLE_CHARS.set('^');
PRINTABLE_CHARS.set('`');
for (int i = 'a'; i <= 'z'; i++) {
PRINTABLE_CHARS.set(i);
}
PRINTABLE_CHARS.set('{');
PRINTABLE_CHARS.set('|');
PRINTABLE_CHARS.set('}');
PRINTABLE_CHARS.set('~');
}
private static final byte BLANK = 32;
private static final byte UNDERSCORE = 95;
private boolean encodeBlanks = false;
/**
* Default constructor.
*/
public QCodec() {
this(CharEncoding.UTF_8);
}
/**
* Constructor which allows for the selection of a default charset
*
* @param charset
* the default string charset to use.
*
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
*/
public QCodec(final String charset) {
super();
this.charset = charset;
}
protected String getEncoding() {
return "Q";
}
protected byte[] doEncoding(byte[] bytes) {
if (bytes == null) {
return null;
}
byte[] data = QuotedPrintableCodec.encodeQuotedPrintable(PRINTABLE_CHARS, bytes);
if (this.encodeBlanks) {
for (int i = 0; i < data.length; i++) {
if (data[i] == BLANK) {
data[i] = UNDERSCORE;
}
}
}
return data;
}
protected byte[] doDecoding(byte[] bytes) throws DecoderException {
if (bytes == null) {
return null;
}
boolean hasUnderscores = false;
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] == UNDERSCORE) {
hasUnderscores = true;
break;
}
}
if (hasUnderscores) {
byte[] tmp = new byte[bytes.length];
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
if (b != UNDERSCORE) {
tmp[i] = b;
} else {
tmp[i] = BLANK;
}
}
return QuotedPrintableCodec.decodeQuotedPrintable(tmp);
}
return QuotedPrintableCodec.decodeQuotedPrintable(bytes);
}
/**
* Encodes a string into its quoted-printable form using the specified charset. Unsafe characters are escaped.
*
* @param pString
* string to convert to quoted-printable form
* @param charset
* the charset for pString
* @return quoted-printable string
*
* @throws EncoderException
* thrown if a failure condition is encountered during the encoding process.
*/
public String encode(final String pString, final String charset) throws EncoderException {
if (pString == null) {
return null;
}
try {
return encodeText(pString, charset);
} catch (UnsupportedEncodingException e) {
throw new EncoderException(e.getMessage(), e);
}
}
/**
* Encodes a string into its quoted-printable form using the default charset. Unsafe characters are escaped.
*
* @param pString
* string to convert to quoted-printable form
* @return quoted-printable string
*
* @throws EncoderException
* thrown if a failure condition is encountered during the encoding process.
*/
public String encode(String pString) throws EncoderException {
if (pString == null) {
return null;
}
return encode(pString, getDefaultCharset());
}
/**
* Decodes a quoted-printable string into its original form. Escaped characters are converted back to their original
* representation.
*
* @param pString
* quoted-printable string to convert into its original form
*
* @return original string
*
* @throws DecoderException
* A decoder exception is thrown if a failure condition is encountered during the decode process.
*/
public String decode(String pString) throws DecoderException {
if (pString == null) {
return null;
}
try {
return decodeText(pString);
} catch (UnsupportedEncodingException e) {
throw new DecoderException(e.getMessage(), e);
}
}
/**
* Encodes an object into its quoted-printable form using the default charset. Unsafe characters are escaped.
*
* @param pObject
* object to convert to quoted-printable form
* @return quoted-printable object
*
* @throws EncoderException
* thrown if a failure condition is encountered during the encoding process.
*/
public Object encode(Object pObject) throws EncoderException {
if (pObject == null) {
return null;
} else if (pObject instanceof String) {
return encode((String) pObject);
} else {
throw new EncoderException("Objects of type " +
pObject.getClass().getName() +
" cannot be encoded using Q codec");
}
}
/**
* Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original
* representation.
*
* @param pObject
* quoted-printable object to convert into its original form
*
* @return original object
*
* @throws DecoderException
* Thrown if the argument is not a <code>String</code>. Thrown if a failure condition is
* encountered during the decode process.
*/
public Object decode(Object pObject) throws DecoderException {
if (pObject == null) {
return null;
} else if (pObject instanceof String) {
return decode((String) pObject);
} else {
throw new DecoderException("Objects of type " +
pObject.getClass().getName() +
" cannot be decoded using Q codec");
}
}
/**
* The default charset used for string decoding and encoding.
*
* @return the default string charset.
*/
public String getDefaultCharset() {
return this.charset;
}
/**
* Tests if optional tranformation of SPACE characters is to be used
*
* @return <code>true</code> if SPACE characters are to be transformed, <code>false</code> otherwise
*/
public boolean isEncodeBlanks() {
return this.encodeBlanks;
}
/**
* Defines whether optional tranformation of SPACE characters is to be used
*
* @param b
* <code>true</code> if SPACE characters are to be transformed, <code>false</code> otherwise
*/
public void setEncodeBlanks(boolean b) {
this.encodeBlanks = b;
}
}

Просмотреть файл

@ -0,0 +1,388 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.net;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.BitSet;
import org.mozilla.apache.commons.codec.BinaryDecoder;
import org.mozilla.apache.commons.codec.BinaryEncoder;
import org.mozilla.apache.commons.codec.DecoderException;
import org.mozilla.apache.commons.codec.EncoderException;
import org.mozilla.apache.commons.codec.CharEncoding;
import org.mozilla.apache.commons.codec.StringDecoder;
import org.mozilla.apache.commons.codec.StringEncoder;
import org.mozilla.apache.commons.codec.binary.StringUtils;
/**
* <p>
* Codec for the Quoted-Printable section of <a href="http://www.ietf.org/rfc/rfc1521.txt">RFC 1521</a>.
* </p>
* <p>
* The Quoted-Printable encoding is intended to represent data that largely consists of octets that correspond to
* printable characters in the ASCII character set. It encodes the data in such a way that the resulting octets are
* unlikely to be modified by mail transport. If the data being encoded are mostly ASCII text, the encoded form of the
* data remains largely recognizable by humans. A body which is entirely ASCII may also be encoded in Quoted-Printable
* to ensure the integrity of the data should the message pass through a character- translating, and/or line-wrapping
* gateway.
* </p>
*
* <p>
* Note:
* </p>
* <p>
* Rules #3, #4, and #5 of the quoted-printable spec are not implemented yet because the complete quoted-printable spec
* does not lend itself well into the byte[] oriented codec framework. Complete the codec once the steamable codec
* framework is ready. The motivation behind providing the codec in a partial form is that it can already come in handy
* for those applications that do not require quoted-printable line formatting (rules #3, #4, #5), for instance Q codec.
* </p>
*
* @see <a href="http://www.ietf.org/rfc/rfc1521.txt"> RFC 1521 MIME (Multipurpose Internet Mail Extensions) Part One:
* Mechanisms for Specifying and Describing the Format of Internet Message Bodies </a>
*
* @author Apache Software Foundation
* @since 1.3
* @version $Id: QuotedPrintableCodec.java 1080712 2011-03-11 18:26:59Z ggregory $
*/
public class QuotedPrintableCodec implements BinaryEncoder, BinaryDecoder, StringEncoder, StringDecoder {
/**
* The default charset used for string decoding and encoding.
*/
private final String charset;
/**
* BitSet of printable characters as defined in RFC 1521.
*/
private static final BitSet PRINTABLE_CHARS = new BitSet(256);
private static final byte ESCAPE_CHAR = '=';
private static final byte TAB = 9;
private static final byte SPACE = 32;
// Static initializer for printable chars collection
static {
// alpha characters
for (int i = 33; i <= 60; i++) {
PRINTABLE_CHARS.set(i);
}
for (int i = 62; i <= 126; i++) {
PRINTABLE_CHARS.set(i);
}
PRINTABLE_CHARS.set(TAB);
PRINTABLE_CHARS.set(SPACE);
}
/**
* Default constructor.
*/
public QuotedPrintableCodec() {
this(CharEncoding.UTF_8);
}
/**
* Constructor which allows for the selection of a default charset
*
* @param charset
* the default string charset to use.
*/
public QuotedPrintableCodec(String charset) {
super();
this.charset = charset;
}
/**
* Encodes byte into its quoted-printable representation.
*
* @param b
* byte to encode
* @param buffer
* the buffer to write to
*/
private static final void encodeQuotedPrintable(int b, ByteArrayOutputStream buffer) {
buffer.write(ESCAPE_CHAR);
char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));
char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
buffer.write(hex1);
buffer.write(hex2);
}
/**
* Encodes an array of bytes into an array of quoted-printable 7-bit characters. Unsafe characters are escaped.
*
* <p>
* This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
* RFC 1521 and is suitable for encoding binary data and unformatted text.
* </p>
*
* @param printable
* bitset of characters deemed quoted-printable
* @param bytes
* array of bytes to be encoded
* @return array of bytes containing quoted-printable data
*/
public static final byte[] encodeQuotedPrintable(BitSet printable, byte[] bytes) {
if (bytes == null) {
return null;
}
if (printable == null) {
printable = PRINTABLE_CHARS;
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i];
if (b < 0) {
b = 256 + b;
}
if (printable.get(b)) {
buffer.write(b);
} else {
encodeQuotedPrintable(b, buffer);
}
}
return buffer.toByteArray();
}
/**
* Decodes an array quoted-printable characters into an array of original bytes. Escaped characters are converted
* back to their original representation.
*
* <p>
* This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
* RFC 1521.
* </p>
*
* @param bytes
* array of quoted-printable characters
* @return array of original bytes
* @throws DecoderException
* Thrown if quoted-printable decoding is unsuccessful
*/
public static final byte[] decodeQuotedPrintable(byte[] bytes) throws DecoderException {
if (bytes == null) {
return null;
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i];
if (b == ESCAPE_CHAR) {
try {
int u = Utils.digit16(bytes[++i]);
int l = Utils.digit16(bytes[++i]);
buffer.write((char) ((u << 4) + l));
} catch (ArrayIndexOutOfBoundsException e) {
throw new DecoderException("Invalid quoted-printable encoding", e);
}
} else {
buffer.write(b);
}
}
return buffer.toByteArray();
}
/**
* Encodes an array of bytes into an array of quoted-printable 7-bit characters. Unsafe characters are escaped.
*
* <p>
* This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
* RFC 1521 and is suitable for encoding binary data and unformatted text.
* </p>
*
* @param bytes
* array of bytes to be encoded
* @return array of bytes containing quoted-printable data
*/
public byte[] encode(byte[] bytes) {
return encodeQuotedPrintable(PRINTABLE_CHARS, bytes);
}
/**
* Decodes an array of quoted-printable characters into an array of original bytes. Escaped characters are converted
* back to their original representation.
*
* <p>
* This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
* RFC 1521.
* </p>
*
* @param bytes
* array of quoted-printable characters
* @return array of original bytes
* @throws DecoderException
* Thrown if quoted-printable decoding is unsuccessful
*/
public byte[] decode(byte[] bytes) throws DecoderException {
return decodeQuotedPrintable(bytes);
}
/**
* Encodes a string into its quoted-printable form using the default string charset. Unsafe characters are escaped.
*
* <p>
* This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
* RFC 1521 and is suitable for encoding binary data.
* </p>
*
* @param pString
* string to convert to quoted-printable form
* @return quoted-printable string
*
* @throws EncoderException
* Thrown if quoted-printable encoding is unsuccessful
*
* @see #getDefaultCharset()
*/
public String encode(String pString) throws EncoderException {
if (pString == null) {
return null;
}
try {
return encode(pString, getDefaultCharset());
} catch (UnsupportedEncodingException e) {
throw new EncoderException(e.getMessage(), e);
}
}
/**
* Decodes a quoted-printable string into its original form using the specified string charset. Escaped characters
* are converted back to their original representation.
*
* @param pString
* quoted-printable string to convert into its original form
* @param charset
* the original string charset
* @return original string
* @throws DecoderException
* Thrown if quoted-printable decoding is unsuccessful
* @throws UnsupportedEncodingException
* Thrown if charset is not supported
*/
public String decode(String pString, String charset) throws DecoderException, UnsupportedEncodingException {
if (pString == null) {
return null;
}
return new String(decode(StringUtils.getBytesUsAscii(pString)), charset);
}
/**
* Decodes a quoted-printable string into its original form using the default string charset. Escaped characters are
* converted back to their original representation.
*
* @param pString
* quoted-printable string to convert into its original form
* @return original string
* @throws DecoderException
* Thrown if quoted-printable decoding is unsuccessful.
* Thrown if charset is not supported.
* @see #getDefaultCharset()
*/
public String decode(String pString) throws DecoderException {
if (pString == null) {
return null;
}
try {
return decode(pString, getDefaultCharset());
} catch (UnsupportedEncodingException e) {
throw new DecoderException(e.getMessage(), e);
}
}
/**
* Encodes an object into its quoted-printable safe form. Unsafe characters are escaped.
*
* @param pObject
* string to convert to a quoted-printable form
* @return quoted-printable object
* @throws EncoderException
* Thrown if quoted-printable encoding is not applicable to objects of this type or if encoding is
* unsuccessful
*/
public Object encode(Object pObject) throws EncoderException {
if (pObject == null) {
return null;
} else if (pObject instanceof byte[]) {
return encode((byte[]) pObject);
} else if (pObject instanceof String) {
return encode((String) pObject);
} else {
throw new EncoderException("Objects of type " +
pObject.getClass().getName() +
" cannot be quoted-printable encoded");
}
}
/**
* Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original
* representation.
*
* @param pObject
* quoted-printable object to convert into its original form
* @return original object
* @throws DecoderException
* Thrown if the argument is not a <code>String</code> or <code>byte[]</code>. Thrown if a failure condition is
* encountered during the decode process.
*/
public Object decode(Object pObject) throws DecoderException {
if (pObject == null) {
return null;
} else if (pObject instanceof byte[]) {
return decode((byte[]) pObject);
} else if (pObject instanceof String) {
return decode((String) pObject);
} else {
throw new DecoderException("Objects of type " +
pObject.getClass().getName() +
" cannot be quoted-printable decoded");
}
}
/**
* Returns the default charset used for string decoding and encoding.
*
* @return the default string charset.
*/
public String getDefaultCharset() {
return this.charset;
}
/**
* Encodes a string into its quoted-printable form using the specified charset. Unsafe characters are escaped.
*
* <p>
* This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
* RFC 1521 and is suitable for encoding binary data and unformatted text.
* </p>
*
* @param pString
* string to convert to quoted-printable form
* @param charset
* the charset for pString
* @return quoted-printable string
*
* @throws UnsupportedEncodingException
* Thrown if the charset is not supported
*/
public String encode(String pString, String charset) throws UnsupportedEncodingException {
if (pString == null) {
return null;
}
return StringUtils.newStringUsAscii(encode(pString.getBytes(charset)));
}
}

Просмотреть файл

@ -0,0 +1,179 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.net;
import java.io.UnsupportedEncodingException;
import org.mozilla.apache.commons.codec.DecoderException;
import org.mozilla.apache.commons.codec.EncoderException;
import org.mozilla.apache.commons.codec.binary.StringUtils;
/**
* <p>
* Implements methods common to all codecs defined in RFC 1522.
* </p>
*
* <p>
* <a href="http://www.ietf.org/rfc/rfc1522.txt">RFC 1522</a>
* describes techniques to allow the encoding of non-ASCII text in
* various portions of a RFC 822 [2] message header, in a manner which
* is unlikely to confuse existing message handling software.
* </p>
* @see <a href="http://www.ietf.org/rfc/rfc1522.txt">
* MIME (Multipurpose Internet Mail Extensions) Part Two:
* Message Header Extensions for Non-ASCII Text</a>
* </p>
*
* @author Apache Software Foundation
* @since 1.3
* @version $Id: RFC1522Codec.java 798428 2009-07-28 07:32:49Z ggregory $
*/
abstract class RFC1522Codec {
/**
* Separator.
*/
protected static final char SEP = '?';
/**
* Prefix
*/
protected static final String POSTFIX = "?=";
/**
* Postfix
*/
protected static final String PREFIX = "=?";
/**
* Applies an RFC 1522 compliant encoding scheme to the given string of text with the
* given charset. This method constructs the "encoded-word" header common to all the
* RFC 1522 codecs and then invokes {@link #doEncoding(byte [])} method of a concrete
* class to perform the specific enconding.
*
* @param text a string to encode
* @param charset a charset to be used
*
* @return RFC 1522 compliant "encoded-word"
*
* @throws EncoderException thrown if there is an error conidition during the Encoding
* process.
* @throws UnsupportedEncodingException thrown if charset is not supported
*
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
*/
protected String encodeText(final String text, final String charset)
throws EncoderException, UnsupportedEncodingException
{
if (text == null) {
return null;
}
StringBuffer buffer = new StringBuffer();
buffer.append(PREFIX);
buffer.append(charset);
buffer.append(SEP);
buffer.append(getEncoding());
buffer.append(SEP);
byte [] rawdata = doEncoding(text.getBytes(charset));
buffer.append(StringUtils.newStringUsAscii(rawdata));
buffer.append(POSTFIX);
return buffer.toString();
}
/**
* Applies an RFC 1522 compliant decoding scheme to the given string of text. This method
* processes the "encoded-word" header common to all the RFC 1522 codecs and then invokes
* {@link #doEncoding(byte [])} method of a concrete class to perform the specific deconding.
*
* @param text a string to decode
* @return A new decoded String or <code>null</code> if the input is <code>null</code>.
*
* @throws DecoderException thrown if there is an error conidition during the Decoding
* process.
* @throws UnsupportedEncodingException thrown if charset specified in the "encoded-word"
* header is not supported
*/
protected String decodeText(final String text)
throws DecoderException, UnsupportedEncodingException
{
if (text == null) {
return null;
}
if ((!text.startsWith(PREFIX)) || (!text.endsWith(POSTFIX))) {
throw new DecoderException("RFC 1522 violation: malformed encoded content");
}
int terminator = text.length() - 2;
int from = 2;
int to = text.indexOf(SEP, from);
if (to == terminator) {
throw new DecoderException("RFC 1522 violation: charset token not found");
}
String charset = text.substring(from, to);
if (charset.equals("")) {
throw new DecoderException("RFC 1522 violation: charset not specified");
}
from = to + 1;
to = text.indexOf(SEP, from);
if (to == terminator) {
throw new DecoderException("RFC 1522 violation: encoding token not found");
}
String encoding = text.substring(from, to);
if (!getEncoding().equalsIgnoreCase(encoding)) {
throw new DecoderException("This codec cannot decode " +
encoding + " encoded content");
}
from = to + 1;
to = text.indexOf(SEP, from);
byte[] data = StringUtils.getBytesUsAscii(text.substring(from, to));
data = doDecoding(data);
return new String(data, charset);
}
/**
* Returns the codec name (referred to as encoding in the RFC 1522)
*
* @return name of the codec
*/
protected abstract String getEncoding();
/**
* Encodes an array of bytes using the defined encoding scheme
*
* @param bytes Data to be encoded
*
* @return A byte array containing the encoded data
*
* @throws EncoderException thrown if the Encoder encounters a failure condition
* during the encoding process.
*/
protected abstract byte[] doEncoding(byte[] bytes) throws EncoderException;
/**
* Decodes an array of bytes using the defined encoding scheme
*
* @param bytes Data to be decoded
*
* @return a byte array that contains decoded data
*
* @throws DecoderException A decoder exception is thrown if a Decoder encounters a
* failure condition during the decode process.
*/
protected abstract byte[] doDecoding(byte[] bytes) throws DecoderException;
}

Просмотреть файл

@ -0,0 +1,362 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.net;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.BitSet;
import org.mozilla.apache.commons.codec.BinaryDecoder;
import org.mozilla.apache.commons.codec.BinaryEncoder;
import org.mozilla.apache.commons.codec.DecoderException;
import org.mozilla.apache.commons.codec.EncoderException;
import org.mozilla.apache.commons.codec.CharEncoding;
import org.mozilla.apache.commons.codec.StringDecoder;
import org.mozilla.apache.commons.codec.StringEncoder;
import org.mozilla.apache.commons.codec.binary.StringUtils;
/**
* <p>Implements the 'www-form-urlencoded' encoding scheme,
* also misleadingly known as URL encoding.</p>
*
* <p>For more detailed information please refer to
* <a href="http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1">
* Chapter 17.13.4 'Form content types'</a> of the
* <a href="http://www.w3.org/TR/html4/">HTML 4.01 Specification<a></p>
*
* <p>
* This codec is meant to be a replacement for standard Java classes
* {@link java.net.URLEncoder} and {@link java.net.URLDecoder}
* on older Java platforms, as these classes in Java versions below
* 1.4 rely on the platform's default charset encoding.
* </p>
*
* @author Apache Software Foundation
* @since 1.2
* @version $Id: URLCodec.java 1079537 2011-03-08 20:56:19Z ggregory $
*/
public class URLCodec implements BinaryEncoder, BinaryDecoder, StringEncoder, StringDecoder {
/**
* Radix used in encoding and decoding.
*/
static final int RADIX = 16;
/**
* The default charset used for string decoding and encoding. Consider this field final. The next major release may
* break compatibility and make this field be final.
*/
protected String charset;
/**
* Release 1.5 made this field final.
*/
protected static final byte ESCAPE_CHAR = '%';
/**
* BitSet of www-form-url safe characters.
*/
protected static final BitSet WWW_FORM_URL = new BitSet(256);
// Static initializer for www_form_url
static {
// alpha characters
for (int i = 'a'; i <= 'z'; i++) {
WWW_FORM_URL.set(i);
}
for (int i = 'A'; i <= 'Z'; i++) {
WWW_FORM_URL.set(i);
}
// numeric characters
for (int i = '0'; i <= '9'; i++) {
WWW_FORM_URL.set(i);
}
// special chars
WWW_FORM_URL.set('-');
WWW_FORM_URL.set('_');
WWW_FORM_URL.set('.');
WWW_FORM_URL.set('*');
// blank to be replaced with +
WWW_FORM_URL.set(' ');
}
/**
* Default constructor.
*/
public URLCodec() {
this(CharEncoding.UTF_8);
}
/**
* Constructor which allows for the selection of a default charset
*
* @param charset the default string charset to use.
*/
public URLCodec(String charset) {
super();
this.charset = charset;
}
/**
* Encodes an array of bytes into an array of URL safe 7-bit characters. Unsafe characters are escaped.
*
* @param urlsafe
* bitset of characters deemed URL safe
* @param bytes
* array of bytes to convert to URL safe characters
* @return array of bytes containing URL safe characters
*/
public static final byte[] encodeUrl(BitSet urlsafe, byte[] bytes) {
if (bytes == null) {
return null;
}
if (urlsafe == null) {
urlsafe = WWW_FORM_URL;
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i];
if (b < 0) {
b = 256 + b;
}
if (urlsafe.get(b)) {
if (b == ' ') {
b = '+';
}
buffer.write(b);
} else {
buffer.write(ESCAPE_CHAR);
char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
buffer.write(hex1);
buffer.write(hex2);
}
}
return buffer.toByteArray();
}
/**
* Decodes an array of URL safe 7-bit characters into an array of
* original bytes. Escaped characters are converted back to their
* original representation.
*
* @param bytes array of URL safe characters
* @return array of original bytes
* @throws DecoderException Thrown if URL decoding is unsuccessful
*/
public static final byte[] decodeUrl(byte[] bytes) throws DecoderException {
if (bytes == null) {
return null;
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i];
if (b == '+') {
buffer.write(' ');
} else if (b == ESCAPE_CHAR) {
try {
int u = Utils.digit16(bytes[++i]);
int l = Utils.digit16(bytes[++i]);
buffer.write((char) ((u << 4) + l));
} catch (ArrayIndexOutOfBoundsException e) {
throw new DecoderException("Invalid URL encoding: ", e);
}
} else {
buffer.write(b);
}
}
return buffer.toByteArray();
}
/**
* Encodes an array of bytes into an array of URL safe 7-bit
* characters. Unsafe characters are escaped.
*
* @param bytes array of bytes to convert to URL safe characters
* @return array of bytes containing URL safe characters
*/
public byte[] encode(byte[] bytes) {
return encodeUrl(WWW_FORM_URL, bytes);
}
/**
* Decodes an array of URL safe 7-bit characters into an array of
* original bytes. Escaped characters are converted back to their
* original representation.
*
* @param bytes array of URL safe characters
* @return array of original bytes
* @throws DecoderException Thrown if URL decoding is unsuccessful
*/
public byte[] decode(byte[] bytes) throws DecoderException {
return decodeUrl(bytes);
}
/**
* Encodes a string into its URL safe form using the specified string charset. Unsafe characters are escaped.
*
* @param pString
* string to convert to a URL safe form
* @param charset
* the charset for pString
* @return URL safe string
* @throws UnsupportedEncodingException
* Thrown if charset is not supported
*/
public String encode(String pString, String charset) throws UnsupportedEncodingException {
if (pString == null) {
return null;
}
return StringUtils.newStringUsAscii(encode(pString.getBytes(charset)));
}
/**
* Encodes a string into its URL safe form using the default string
* charset. Unsafe characters are escaped.
*
* @param pString string to convert to a URL safe form
* @return URL safe string
* @throws EncoderException Thrown if URL encoding is unsuccessful
*
* @see #getDefaultCharset()
*/
public String encode(String pString) throws EncoderException {
if (pString == null) {
return null;
}
try {
return encode(pString, getDefaultCharset());
} catch (UnsupportedEncodingException e) {
throw new EncoderException(e.getMessage(), e);
}
}
/**
* Decodes a URL safe string into its original form using the
* specified encoding. Escaped characters are converted back
* to their original representation.
*
* @param pString URL safe string to convert into its original form
* @param charset the original string charset
* @return original string
* @throws DecoderException Thrown if URL decoding is unsuccessful
* @throws UnsupportedEncodingException Thrown if charset is not
* supported
*/
public String decode(String pString, String charset) throws DecoderException, UnsupportedEncodingException {
if (pString == null) {
return null;
}
return new String(decode(StringUtils.getBytesUsAscii(pString)), charset);
}
/**
* Decodes a URL safe string into its original form using the default
* string charset. Escaped characters are converted back to their
* original representation.
*
* @param pString URL safe string to convert into its original form
* @return original string
* @throws DecoderException Thrown if URL decoding is unsuccessful
*
* @see #getDefaultCharset()
*/
public String decode(String pString) throws DecoderException {
if (pString == null) {
return null;
}
try {
return decode(pString, getDefaultCharset());
} catch (UnsupportedEncodingException e) {
throw new DecoderException(e.getMessage(), e);
}
}
/**
* Encodes an object into its URL safe form. Unsafe characters are
* escaped.
*
* @param pObject string to convert to a URL safe form
* @return URL safe object
* @throws EncoderException Thrown if URL encoding is not
* applicable to objects of this type or
* if encoding is unsuccessful
*/
public Object encode(Object pObject) throws EncoderException {
if (pObject == null) {
return null;
} else if (pObject instanceof byte[]) {
return encode((byte[])pObject);
} else if (pObject instanceof String) {
return encode((String)pObject);
} else {
throw new EncoderException("Objects of type " +
pObject.getClass().getName() + " cannot be URL encoded");
}
}
/**
* Decodes a URL safe object into its original form. Escaped characters are converted back to their original
* representation.
*
* @param pObject
* URL safe object to convert into its original form
* @return original object
* @throws DecoderException
* Thrown if the argument is not a <code>String</code> or <code>byte[]</code>. Thrown if a failure condition is
* encountered during the decode process.
*/
public Object decode(Object pObject) throws DecoderException {
if (pObject == null) {
return null;
} else if (pObject instanceof byte[]) {
return decode((byte[]) pObject);
} else if (pObject instanceof String) {
return decode((String) pObject);
} else {
throw new DecoderException("Objects of type " + pObject.getClass().getName() + " cannot be URL decoded");
}
}
/**
* The <code>String</code> encoding used for decoding and encoding.
*
* @return Returns the encoding.
*
* @deprecated Use {@link #getDefaultCharset()}, will be removed in 2.0.
*/
public String getEncoding() {
return this.charset;
}
/**
* The default charset used for string decoding and encoding.
*
* @return the default string charset.
*/
public String getDefaultCharset() {
return this.charset;
}
}

Просмотреть файл

@ -0,0 +1,50 @@
// Mozilla has modified this file - see http://hg.mozilla.org/ for details.
/*
* 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.
*/
package org.mozilla.apache.commons.codec.net;
import org.mozilla.apache.commons.codec.DecoderException;
/**
* Utility methods for this package.
*
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @version $Id: Utils.java 798611 2009-07-28 17:10:44Z ggregory $
* @since 1.4
*/
class Utils {
/**
* Returns the numeric value of the character <code>b</code> in radix 16.
*
* @param b
* The byte to be converted.
* @return The numeric value represented by the character in radix 16.
*
* @throws DecoderException
* Thrown when the byte is not valid per {@link Character#digit(char,int)}
*/
static int digit16(byte b) throws DecoderException {
int i = Character.digit((char) b, 16);
if (i == -1) {
throw new DecoderException("Invalid URL encoding: not a valid digit (radix " + URLCodec.RADIX + "): " + b);
}
return i;
}
}

Просмотреть файл

@ -0,0 +1,23 @@
<!--
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.
-->
<html>
<body>
<p>
Network related encoding and decoding.
</p>
</body>
</html>

Просмотреть файл

@ -0,0 +1,29 @@
<!--
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.
-->
<!-- $Id: overview.html 561548 2007-07-31 21:16:26Z mbenson $ -->
<html>
<body>
<p>
This document is the API specification for the Apache Commons Codec Library, version 1.3.
</p>
<p>
This library requires a JRE version of 1.2.2 or greater.
The hypertext links originating from this document point to Sun's version 1.3 API as the 1.2.2 API documentation
is no longer on-line.
</p>
</body>
</html>

Просмотреть файл

@ -0,0 +1,100 @@
<!--
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.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<p>Interfaces and classes used by
the various implementations in the sub-packages.</p>
<p>Definitive implementations of commonly used encoders and decoders.</p>
<p>Codec is currently comprised of a modest set of utilities and a
simple framework for String encoding and decoding in three categories:
Binary Encoders, Language Encoders, and Network Encoders. </p>
<h4><a name="Common Encoders">Binary Encoders</a></h4>
<table border="1" width="100%" cellspacing="2" cellpadding="3">
<tbody>
<tr>
<td>
<a href="binary/Base64.html">
org.apache.commons.codec.binary.Base64</a>
</td>
<td>
Provides Base64 content-transfer-encoding as defined in
<a href="http://www.ietf.org/rfc/rfc2045.txt"> RFC 2045</a>
</td>
<td>Production</td>
</tr>
<tr>
<td>
<a href="binary/Hex.html">
org.apache.commons.codec.binary.Hex</a>
</td>
<td>
Converts an array of bytes into an array of characters
representing the hexidecimal values of each byte in order
</td>
<td>Production</td>
</tr>
</tbody>
</table>
<h4>
<a name="Language Encoders">Language Encoders</a>
</h4>
<p>
Codec contains a number of commonly used language and phonetic
encoders
</p>
<table border="1" width="100%" cellspacing="2" cellpadding="3">
<tbody>
<tr>
<td>
<a href="#">org.apache.commons.codec.language.Soundex</a>
</td>
<td>Implementation of the Soundex algorithm.</td>
<td>Production</td>
</tr>
<tr>
<td>
<a href="#">org.apache.commons.codec.language.Metaphone</a>
</td>
<td>Implementation of the Metaphone algorithm.</td>
<td>Production</td>
</tr>
</tbody>
</table>
<h4><a name="Network_Encoders">Network Encoders</a></h4>
<h4> </h4>
<p> Codec contains network related encoders </p>
<table border="1" width="100%" cellspacing="2" cellpadding="3">
<tbody>
<tr>
<td>
<a href="#">org.apache.commons.codec.net.URLCodec</a>
</td>
<td>Implements the 'www-form-urlencoded' encoding scheme.</td>
<td>Production</td>
</tr>
</tbody>
</table>
<br>
</body>
</html>

Просмотреть файл

@ -0,0 +1,50 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.io.IOException;
/**
* Signals that the connection has been closed unexpectedly.
*
* @since 4.0
*/
public class ConnectionClosedException extends IOException {
private static final long serialVersionUID = 617550366255636674L;
/**
* Creates a new ConnectionClosedException with the specified detail message.
*
* @param message The exception detail message
*/
public ConnectionClosedException(final String message) {
super(message);
}
}

Просмотреть файл

@ -0,0 +1,70 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import ch.boye.httpclientandroidlib.protocol.HttpContext;
/**
* Interface for deciding whether a connection can be re-used for
* subsequent requests and should be kept alive.
* <p>
* Implementations of this interface must be thread-safe. Access to shared
* data must be synchronized as methods of this interface may be executed
* from multiple threads.
*
* @since 4.0
*/
public interface ConnectionReuseStrategy {
/**
* Decides whether a connection can be kept open after a request.
* If this method returns <code>false</code>, the caller MUST
* close the connection to correctly comply with the HTTP protocol.
* If it returns <code>true</code>, the caller SHOULD attempt to
* keep the connection open for reuse with another request.
* <br/>
* One can use the HTTP context to retrieve additional objects that
* may be relevant for the keep-alive strategy: the actual HTTP
* connection, the original HTTP request, target host if known,
* number of times the connection has been reused already and so on.
* <br/>
* If the connection is already closed, <code>false</code> is returned.
* The stale connection check MUST NOT be triggered by a
* connection reuse strategy.
*
* @param response
* The last response received over that connection.
* @param context the context in which the connection is being
* used.
*
* @return <code>true</code> if the connection is allowed to be reused, or
* <code>false</code> if it MUST NOT be reused
*/
boolean keepAlive(HttpResponse response, HttpContext context);
}

Просмотреть файл

@ -0,0 +1,60 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
/**
* An HTTP header which is already formatted.
* For example when headers are received, the original formatting
* can be preserved. This allows for the header to be sent without
* another formatting step.
*
* @since 4.0
*/
public interface FormattedHeader extends Header {
/**
* Obtains the buffer with the formatted header.
* The returned buffer MUST NOT be modified.
*
* @return the formatted header, in a buffer that must not be modified
*/
CharArrayBuffer getBuffer();
/**
* Obtains the start of the header value in the {@link #getBuffer buffer}.
* By accessing the value in the buffer, creation of a temporary string
* can be avoided.
*
* @return index of the first character of the header value
* in the buffer returned by {@link #getBuffer getBuffer}.
*/
int getValuePos();
}

Просмотреть файл

@ -0,0 +1,74 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
/**
* Represents an HTTP header field.
*
* <p>The HTTP header fields follow the same generic format as
* that given in Section 3.1 of RFC 822. Each header field consists
* of a name followed by a colon (":") and the field value. Field names
* are case-insensitive. The field value MAY be preceded by any amount
* of LWS, though a single SP is preferred.
*
*<pre>
* message-header = field-name ":" [ field-value ]
* field-name = token
* field-value = *( field-content | LWS )
* field-content = &lt;the OCTETs making up the field-value
* and consisting of either *TEXT or combinations
* of token, separators, and quoted-string&gt;
*</pre>
*
* @since 4.0
*/
public interface Header {
/**
* Get the name of the Header.
*
* @return the name of the Header, never {@code null}
*/
String getName();
/**
* Get the value of the Header.
*
* @return the value of the Header, may be {@code null}
*/
String getValue();
/**
* Parses the value.
*
* @return an array of {@link HeaderElement} entries, may be empty, but is never {@code null}
* @throws ParseException
*/
HeaderElement[] getElements() throws ParseException;
}

Просмотреть файл

@ -0,0 +1,108 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
/**
* One element of an HTTP {@link Header header} value consisting of
* a name / value pair and a number of optional name / value parameters.
* <p>
* Some HTTP headers (such as the set-cookie header) have values that
* can be decomposed into multiple elements. Such headers must be in the
* following form:
* </p>
* <pre>
* header = [ element ] *( "," [ element ] )
* element = name [ "=" [ value ] ] *( ";" [ param ] )
* param = name [ "=" [ value ] ]
*
* name = token
* value = ( token | quoted-string )
*
* token = 1*&lt;any char except "=", ",", ";", &lt;"&gt; and
* white space&gt;
* quoted-string = &lt;"&gt; *( text | quoted-char ) &lt;"&gt;
* text = any char except &lt;"&gt;
* quoted-char = "\" char
* </pre>
* <p>
* Any amount of white space is allowed between any part of the
* header, element or param and is ignored. A missing value in any
* element or param will be stored as the empty {@link String};
* if the "=" is also missing <var>null</var> will be stored instead.
*
* @since 4.0
*/
public interface HeaderElement {
/**
* Returns header element name.
*
* @return header element name
*/
String getName();
/**
* Returns header element value.
*
* @return header element value
*/
String getValue();
/**
* Returns an array of name / value pairs.
*
* @return array of name / value pairs
*/
NameValuePair[] getParameters();
/**
* Returns the first parameter with the given name.
*
* @param name parameter name
*
* @return name / value pair
*/
NameValuePair getParameterByName(String name);
/**
* Returns the total count of parameters.
*
* @return parameter count
*/
int getParameterCount();
/**
* Returns parameter with the given index.
*
* @param index
* @return name / value pair
*/
NameValuePair getParameter(int index);
}

Просмотреть файл

@ -0,0 +1,57 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.util.Iterator;
/**
* A type-safe iterator for {@link HeaderElement} objects.
*
* @since 4.0
*/
public interface HeaderElementIterator extends Iterator {
/**
* Indicates whether there is another header element in this
* iteration.
*
* @return <code>true</code> if there is another header element,
* <code>false</code> otherwise
*/
boolean hasNext();
/**
* Obtains the next header element from this iteration.
* This method should only be called while {@link #hasNext hasNext}
* is true.
*
* @return the next header element in this iteration
*/
HeaderElement nextElement();
}

Просмотреть файл

@ -0,0 +1,56 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.util.Iterator;
/**
* A type-safe iterator for {@link Header} objects.
*
* @since 4.0
*/
public interface HeaderIterator extends Iterator {
/**
* Indicates whether there is another header in this iteration.
*
* @return <code>true</code> if there is another header,
* <code>false</code> otherwise
*/
boolean hasNext();
/**
* Obtains the next header from this iteration.
* This method should only be called while {@link #hasNext hasNext}
* is true.
*
* @return the next header in this iteration
*/
Header nextHeader();
}

Просмотреть файл

@ -0,0 +1,102 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.io.IOException;
/**
* A client-side HTTP connection, which can be used for sending
* requests and receiving responses.
*
* @since 4.0
*/
public interface HttpClientConnection extends HttpConnection {
/**
* Checks if response data is available from the connection. May wait for
* the specified time until some data becomes available. Note that some
* implementations may completely ignore the timeout parameter.
*
* @param timeout the maximum time in milliseconds to wait for data
* @return true if data is available; false if there was no data available
* even after waiting for <code>timeout</code> milliseconds.
* @throws IOException if an error happens on the connection
*/
boolean isResponseAvailable(int timeout)
throws IOException;
/**
* Sends the request line and all headers over the connection.
* @param request the request whose headers to send.
* @throws HttpException in case of HTTP protocol violation
* @throws IOException in case of an I/O error
*/
void sendRequestHeader(HttpRequest request)
throws HttpException, IOException;
/**
* Sends the request entity over the connection.
* @param request the request whose entity to send.
* @throws HttpException in case of HTTP protocol violation
* @throws IOException in case of an I/O error
*/
void sendRequestEntity(HttpEntityEnclosingRequest request)
throws HttpException, IOException;
/**
* Receives the request line and headers of the next response available from
* this connection. The caller should examine the HttpResponse object to
* find out if it should try to receive a response entity as well.
*
* @return a new HttpResponse object with status line and headers
* initialized.
* @throws HttpException in case of HTTP protocol violation
* @throws IOException in case of an I/O error
*/
HttpResponse receiveResponseHeader()
throws HttpException, IOException;
/**
* Receives the next response entity available from this connection and
* attaches it to an existing HttpResponse object.
*
* @param response the response to attach the entity to
* @throws HttpException in case of HTTP protocol violation
* @throws IOException in case of an I/O error
*/
void receiveResponseEntity(HttpResponse response)
throws HttpException, IOException;
/**
* Writes out all pending buffered data over the open connection.
*
* @throws IOException in case of an I/O error
*/
void flush() throws IOException;
}

Просмотреть файл

@ -0,0 +1,103 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.io.IOException;
/**
* A generic HTTP connection, useful on client and server side.
*
* @since 4.0
*/
public interface HttpConnection {
/**
* Closes this connection gracefully.
* This method will attempt to flush the internal output
* buffer prior to closing the underlying socket.
* This method MUST NOT be called from a different thread to force
* shutdown of the connection. Use {@link #shutdown shutdown} instead.
*/
public void close() throws IOException;
/**
* Checks if this connection is open.
* @return true if it is open, false if it is closed.
*/
public boolean isOpen();
/**
* Checks whether this connection has gone down.
* Network connections may get closed during some time of inactivity
* for several reasons. The next time a read is attempted on such a
* connection it will throw an IOException.
* This method tries to alleviate this inconvenience by trying to
* find out if a connection is still usable. Implementations may do
* that by attempting a read with a very small timeout. Thus this
* method may block for a small amount of time before returning a result.
* It is therefore an <i>expensive</i> operation.
*
* @return <code>true</code> if attempts to use this connection are
* likely to succeed, or <code>false</code> if they are likely
* to fail and this connection should be closed
*/
public boolean isStale();
/**
* Sets the socket timeout value.
*
* @param timeout timeout value in milliseconds
*/
void setSocketTimeout(int timeout);
/**
* Returns the socket timeout value.
*
* @return positive value in milliseconds if a timeout is set,
* <code>0</code> if timeout is disabled or <code>-1</code> if
* timeout is undefined.
*/
int getSocketTimeout();
/**
* Force-closes this connection.
* This is the only method of a connection which may be called
* from a different thread to terminate the connection.
* This method will not attempt to flush the transmitter's
* internal buffer prior to closing the underlying socket.
*/
public void shutdown() throws IOException;
/**
* Returns a collection of connection metrics.
*
* @return HttpConnectionMetrics
*/
HttpConnectionMetrics getMetrics();
}

Просмотреть файл

@ -0,0 +1,77 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
/**
* The point of access to the statistics of an {@link HttpConnection}.
*
* @since 4.0
*/
public interface HttpConnectionMetrics {
/**
* Returns the number of requests transferred over the connection,
* 0 if not available.
*/
long getRequestCount();
/**
* Returns the number of responses transferred over the connection,
* 0 if not available.
*/
long getResponseCount();
/**
* Returns the number of bytes transferred over the connection,
* 0 if not available.
*/
long getSentBytesCount();
/**
* Returns the number of bytes transferred over the connection,
* 0 if not available.
*/
long getReceivedBytesCount();
/**
* Return the value for the specified metric.
*
*@param metricName the name of the metric to query.
*
*@return the object representing the metric requested,
* <code>null</code> if the metric cannot not found.
*/
Object getMetric(String metricName);
/**
* Resets the counts
*
*/
void reset();
}

Просмотреть файл

@ -0,0 +1,198 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* An entity that can be sent or received with an HTTP message.
* Entities can be found in some
* {@link HttpEntityEnclosingRequest requests} and in
* {@link HttpResponse responses}, where they are optional.
* <p>
* There are three distinct types of entities in HttpCore,
* depending on where their {@link #getContent content} originates:
* <ul>
* <li><b>streamed</b>: The content is received from a stream, or
* generated on the fly. In particular, this category includes
* entities being received from a {@link HttpConnection connection}.
* {@link #isStreaming Streamed} entities are generally not
* {@link #isRepeatable repeatable}.
* </li>
* <li><b>self-contained</b>: The content is in memory or obtained by
* means that are independent from a connection or other entity.
* Self-contained entities are generally {@link #isRepeatable repeatable}.
* </li>
* <li><b>wrapping</b>: The content is obtained from another entity.
* </li>
* </ul>
* This distinction is important for connection management with incoming
* entities. For entities that are created by an application and only sent
* using the HTTP components framework, the difference between streamed
* and self-contained is of little importance. In that case, it is suggested
* to consider non-repeatable entities as streamed, and those that are
* repeatable (without a huge effort) as self-contained.
*
* @since 4.0
*/
public interface HttpEntity {
/**
* Tells if the entity is capable of producing its data more than once.
* A repeatable entity's getContent() and writeTo(OutputStream) methods
* can be called more than once whereas a non-repeatable entity's can not.
* @return true if the entity is repeatable, false otherwise.
*/
boolean isRepeatable();
/**
* Tells about chunked encoding for this entity.
* The primary purpose of this method is to indicate whether
* chunked encoding should be used when the entity is sent.
* For entities that are received, it can also indicate whether
* the entity was received with chunked encoding.
* <br/>
* The behavior of wrapping entities is implementation dependent,
* but should respect the primary purpose.
*
* @return <code>true</code> if chunked encoding is preferred for this
* entity, or <code>false</code> if it is not
*/
boolean isChunked();
/**
* Tells the length of the content, if known.
*
* @return the number of bytes of the content, or
* a negative number if unknown. If the content length is known
* but exceeds {@link java.lang.Long#MAX_VALUE Long.MAX_VALUE},
* a negative number is returned.
*/
long getContentLength();
/**
* Obtains the Content-Type header, if known.
* This is the header that should be used when sending the entity,
* or the one that was received with the entity. It can include a
* charset attribute.
*
* @return the Content-Type header for this entity, or
* <code>null</code> if the content type is unknown
*/
Header getContentType();
/**
* Obtains the Content-Encoding header, if known.
* This is the header that should be used when sending the entity,
* or the one that was received with the entity.
* Wrapping entities that modify the content encoding should
* adjust this header accordingly.
*
* @return the Content-Encoding header for this entity, or
* <code>null</code> if the content encoding is unknown
*/
Header getContentEncoding();
/**
* Returns a content stream of the entity.
* {@link #isRepeatable Repeatable} entities are expected
* to create a new instance of {@link InputStream} for each invocation
* of this method and therefore can be consumed multiple times.
* Entities that are not {@link #isRepeatable repeatable} are expected
* to return the same {@link InputStream} instance and therefore
* may not be consumed more than once.
* <p>
* IMPORTANT: Please note all entity implementations must ensure that
* all allocated resources are properly deallocated after
* the {@link InputStream#close()} method is invoked.
*
* @return content stream of the entity.
*
* @throws IOException if the stream could not be created
* @throws IllegalStateException
* if content stream cannot be created.
*
* @see #isRepeatable()
*/
InputStream getContent() throws IOException, IllegalStateException;
/**
* Writes the entity content out to the output stream.
* <p>
* <p>
* IMPORTANT: Please note all entity implementations must ensure that
* all allocated resources are properly deallocated when this method
* returns.
*
* @param outstream the output stream to write entity content to
*
* @throws IOException if an I/O error occurs
*/
void writeTo(OutputStream outstream) throws IOException;
/**
* Tells whether this entity depends on an underlying stream.
* Streamed entities that read data directly from the socket should
* return <code>true</code>. Self-contained entities should return
* <code>false</code>. Wrapping entities should delegate this call
* to the wrapped entity.
*
* @return <code>true</code> if the entity content is streamed,
* <code>false</code> otherwise
*/
boolean isStreaming(); // don't expect an exception here
/**
* This method is deprecated since version 4.1. Please use standard
* java convention to ensure resource deallocation by calling
* {@link InputStream#close()} on the input stream returned by
* {@link #getContent()}
* <p>
* This method is called to indicate that the content of this entity
* is no longer required. All entity implementations are expected to
* release all allocated resources as a result of this method
* invocation. Content streaming entities are also expected to
* dispose of the remaining content, if any. Wrapping entities should
* delegate this call to the wrapped entity.
* <p>
* This method is of particular importance for entities being
* received from a {@link HttpConnection connection}. The entity
* needs to be consumed completely in order to re-use the connection
* with keep-alive.
*
* @throws IOException if an I/O error occurs.
*
* @deprecated Use {@link ch.boye.httpclientandroidlib.util.EntityUtils#consume(HttpEntity)}
*
* @see #getContent() and #writeTo(OutputStream)
*/
void consumeContent() throws IOException;
}

Просмотреть файл

@ -0,0 +1,62 @@
/*
* $Header: $
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
/**
* A request with an entity.
*
* @since 4.0
*/
public interface HttpEntityEnclosingRequest extends HttpRequest {
/**
* Tells if this request should use the expect-continue handshake.
* The expect continue handshake gives the server a chance to decide
* whether to accept the entity enclosing request before the possibly
* lengthy entity is sent across the wire.
* @return true if the expect continue handshake should be used, false if
* not.
*/
boolean expectContinue();
/**
* Associates the entity with this request.
*
* @param entity the entity to send.
*/
void setEntity(HttpEntity entity);
/**
* Returns the entity associated with this request.
*
* @return entity
*/
HttpEntity getEntity();
}

Просмотреть файл

@ -0,0 +1,69 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import ch.boye.httpclientandroidlib.util.ExceptionUtils;
/**
* Signals that an HTTP exception has occurred.
*
* @since 4.0
*/
public class HttpException extends Exception {
private static final long serialVersionUID = -5437299376222011036L;
/**
* Creates a new HttpException with a <tt>null</tt> detail message.
*/
public HttpException() {
super();
}
/**
* Creates a new HttpException with the specified detail message.
*
* @param message the exception detail message
*/
public HttpException(final String message) {
super(message);
}
/**
* Creates a new HttpException with the specified detail message and cause.
*
* @param message the exception detail message
* @param cause the <tt>Throwable</tt> that caused this exception, or <tt>null</tt>
* if the cause is unavailable, unknown, or not a <tt>Throwable</tt>
*/
public HttpException(final String message, final Throwable cause) {
super(message);
ExceptionUtils.initCause(this, cause);
}
}

Просмотреть файл

@ -0,0 +1,206 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
/**
* Constants enumerating the HTTP headers. All headers defined in RFC1945 (HTTP/1.0), RFC2616 (HTTP/1.1), and RFC2518
* (WebDAV) are listed.
*
* @since 4.1
*/
public final class HttpHeaders {
private HttpHeaders() {
}
/** RFC 2616 (HTTP/1.1) Section 14.1 */
public static final String ACCEPT = "Accept";
/** RFC 2616 (HTTP/1.1) Section 14.2 */
public static final String ACCEPT_CHARSET = "Accept-Charset";
/** RFC 2616 (HTTP/1.1) Section 14.3 */
public static final String ACCEPT_ENCODING = "Accept-Encoding";
/** RFC 2616 (HTTP/1.1) Section 14.4 */
public static final String ACCEPT_LANGUAGE = "Accept-Language";
/** RFC 2616 (HTTP/1.1) Section 14.5 */
public static final String ACCEPT_RANGES = "Accept-Ranges";
/** RFC 2616 (HTTP/1.1) Section 14.6 */
public static final String AGE = "Age";
/** RFC 1945 (HTTP/1.0) Section 10.1, RFC 2616 (HTTP/1.1) Section 14.7 */
public static final String ALLOW = "Allow";
/** RFC 1945 (HTTP/1.0) Section 10.2, RFC 2616 (HTTP/1.1) Section 14.8 */
public static final String AUTHORIZATION = "Authorization";
/** RFC 2616 (HTTP/1.1) Section 14.9 */
public static final String CACHE_CONTROL = "Cache-Control";
/** RFC 2616 (HTTP/1.1) Section 14.10 */
public static final String CONNECTION = "Connection";
/** RFC 1945 (HTTP/1.0) Section 10.3, RFC 2616 (HTTP/1.1) Section 14.11 */
public static final String CONTENT_ENCODING = "Content-Encoding";
/** RFC 2616 (HTTP/1.1) Section 14.12 */
public static final String CONTENT_LANGUAGE = "Content-Language";
/** RFC 1945 (HTTP/1.0) Section 10.4, RFC 2616 (HTTP/1.1) Section 14.13 */
public static final String CONTENT_LENGTH = "Content-Length";
/** RFC 2616 (HTTP/1.1) Section 14.14 */
public static final String CONTENT_LOCATION = "Content-Location";
/** RFC 2616 (HTTP/1.1) Section 14.15 */
public static final String CONTENT_MD5 = "Content-MD5";
/** RFC 2616 (HTTP/1.1) Section 14.16 */
public static final String CONTENT_RANGE = "Content-Range";
/** RFC 1945 (HTTP/1.0) Section 10.5, RFC 2616 (HTTP/1.1) Section 14.17 */
public static final String CONTENT_TYPE = "Content-Type";
/** RFC 1945 (HTTP/1.0) Section 10.6, RFC 2616 (HTTP/1.1) Section 14.18 */
public static final String DATE = "Date";
/** RFC 2518 (WevDAV) Section 9.1 */
public static final String DAV = "Dav";
/** RFC 2518 (WevDAV) Section 9.2 */
public static final String DEPTH = "Depth";
/** RFC 2518 (WevDAV) Section 9.3 */
public static final String DESTINATION = "Destination";
/** RFC 2616 (HTTP/1.1) Section 14.19 */
public static final String ETAG = "ETag";
/** RFC 2616 (HTTP/1.1) Section 14.20 */
public static final String EXPECT = "Expect";
/** RFC 1945 (HTTP/1.0) Section 10.7, RFC 2616 (HTTP/1.1) Section 14.21 */
public static final String EXPIRES = "Expires";
/** RFC 1945 (HTTP/1.0) Section 10.8, RFC 2616 (HTTP/1.1) Section 14.22 */
public static final String FROM = "From";
/** RFC 2616 (HTTP/1.1) Section 14.23 */
public static final String HOST = "Host";
/** RFC 2518 (WevDAV) Section 9.4 */
public static final String IF = "If";
/** RFC 2616 (HTTP/1.1) Section 14.24 */
public static final String IF_MATCH = "If-Match";
/** RFC 1945 (HTTP/1.0) Section 10.9, RFC 2616 (HTTP/1.1) Section 14.25 */
public static final String IF_MODIFIED_SINCE = "If-Modified-Since";
/** RFC 2616 (HTTP/1.1) Section 14.26 */
public static final String IF_NONE_MATCH = "If-None-Match";
/** RFC 2616 (HTTP/1.1) Section 14.27 */
public static final String IF_RANGE = "If-Range";
/** RFC 2616 (HTTP/1.1) Section 14.28 */
public static final String IF_UNMODIFIED_SINCE = "If-Unmodified-Since";
/** RFC 1945 (HTTP/1.0) Section 10.10, RFC 2616 (HTTP/1.1) Section 14.29 */
public static final String LAST_MODIFIED = "Last-Modified";
/** RFC 1945 (HTTP/1.0) Section 10.11, RFC 2616 (HTTP/1.1) Section 14.30 */
public static final String LOCATION = "Location";
/** RFC 2518 (WevDAV) Section 9.5 */
public static final String LOCK_TOKEN = "Lock-Token";
/** RFC 2616 (HTTP/1.1) Section 14.31 */
public static final String MAX_FORWARDS = "Max-Forwards";
/** RFC 2518 (WevDAV) Section 9.6 */
public static final String OVERWRITE = "Overwrite";
/** RFC 1945 (HTTP/1.0) Section 10.12, RFC 2616 (HTTP/1.1) Section 14.32 */
public static final String PRAGMA = "Pragma";
/** RFC 2616 (HTTP/1.1) Section 14.33 */
public static final String PROXY_AUTHENTICATE = "Proxy-Authenticate";
/** RFC 2616 (HTTP/1.1) Section 14.34 */
public static final String PROXY_AUTHORIZATION = "Proxy-Authorization";
/** RFC 2616 (HTTP/1.1) Section 14.35 */
public static final String RANGE = "Range";
/** RFC 1945 (HTTP/1.0) Section 10.13, RFC 2616 (HTTP/1.1) Section 14.36 */
public static final String REFERER = "Referer";
/** RFC 2616 (HTTP/1.1) Section 14.37 */
public static final String RETRY_AFTER = "Retry-After";
/** RFC 1945 (HTTP/1.0) Section 10.14, RFC 2616 (HTTP/1.1) Section 14.38 */
public static final String SERVER = "Server";
/** RFC 2518 (WevDAV) Section 9.7 */
public static final String STATUS_URI = "Status-URI";
/** RFC 2616 (HTTP/1.1) Section 14.39 */
public static final String TE = "TE";
/** RFC 2518 (WevDAV) Section 9.8 */
public static final String TIMEOUT = "Timeout";
/** RFC 2616 (HTTP/1.1) Section 14.40 */
public static final String TRAILER = "Trailer";
/** RFC 2616 (HTTP/1.1) Section 14.41 */
public static final String TRANSFER_ENCODING = "Transfer-Encoding";
/** RFC 2616 (HTTP/1.1) Section 14.42 */
public static final String UPGRADE = "Upgrade";
/** RFC 1945 (HTTP/1.0) Section 10.15, RFC 2616 (HTTP/1.1) Section 14.43 */
public static final String USER_AGENT = "User-Agent";
/** RFC 2616 (HTTP/1.1) Section 14.44 */
public static final String VARY = "Vary";
/** RFC 2616 (HTTP/1.1) Section 14.45 */
public static final String VIA = "Via";
/** RFC 2616 (HTTP/1.1) Section 14.46 */
public static final String WARNING = "Warning";
/** RFC 1945 (HTTP/1.0) Section 10.16, RFC 2616 (HTTP/1.1) Section 14.47 */
public static final String WWW_AUTHENTICATE = "WWW-Authenticate";
}

Просмотреть файл

@ -0,0 +1,216 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.io.Serializable;
import java.util.Locale;
import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
import ch.boye.httpclientandroidlib.util.LangUtils;
/**
* Holds all of the variables needed to describe an HTTP connection to a host.
* This includes remote host name, port and scheme.
*
*
* @since 4.0
*/
//@Immutable
public final class HttpHost implements Cloneable, Serializable {
private static final long serialVersionUID = -7529410654042457626L;
/** The default scheme is "http". */
public static final String DEFAULT_SCHEME_NAME = "http";
/** The host to use. */
protected final String hostname;
/** The lowercase host, for {@link #equals} and {@link #hashCode}. */
protected final String lcHostname;
/** The port to use. */
protected final int port;
/** The scheme (lowercased) */
protected final String schemeName;
/**
* Creates a new {@link HttpHost HttpHost}, specifying all values.
* Constructor for HttpHost.
*
* @param hostname the hostname (IP or DNS name)
* @param port the port number.
* <code>-1</code> indicates the scheme default port.
* @param scheme the name of the scheme.
* <code>null</code> indicates the
* {@link #DEFAULT_SCHEME_NAME default scheme}
*/
public HttpHost(final String hostname, int port, final String scheme) {
super();
if (hostname == null) {
throw new IllegalArgumentException("Host name may not be null");
}
this.hostname = hostname;
this.lcHostname = hostname.toLowerCase(Locale.ENGLISH);
if (scheme != null) {
this.schemeName = scheme.toLowerCase(Locale.ENGLISH);
} else {
this.schemeName = DEFAULT_SCHEME_NAME;
}
this.port = port;
}
/**
* Creates a new {@link HttpHost HttpHost}, with default scheme.
*
* @param hostname the hostname (IP or DNS name)
* @param port the port number.
* <code>-1</code> indicates the scheme default port.
*/
public HttpHost(final String hostname, int port) {
this(hostname, port, null);
}
/**
* Creates a new {@link HttpHost HttpHost}, with default scheme and port.
*
* @param hostname the hostname (IP or DNS name)
*/
public HttpHost(final String hostname) {
this(hostname, -1, null);
}
/**
* Copy constructor for {@link HttpHost HttpHost}.
*
* @param httphost the HTTP host to copy details from
*/
public HttpHost (final HttpHost httphost) {
this(httphost.hostname, httphost.port, httphost.schemeName);
}
/**
* Returns the host name.
*
* @return the host name (IP or DNS name)
*/
public String getHostName() {
return this.hostname;
}
/**
* Returns the port.
*
* @return the host port, or <code>-1</code> if not set
*/
public int getPort() {
return this.port;
}
/**
* Returns the scheme name.
*
* @return the scheme name
*/
public String getSchemeName() {
return this.schemeName;
}
/**
* Return the host URI, as a string.
*
* @return the host URI
*/
public String toURI() {
CharArrayBuffer buffer = new CharArrayBuffer(32);
buffer.append(this.schemeName);
buffer.append("://");
buffer.append(this.hostname);
if (this.port != -1) {
buffer.append(':');
buffer.append(Integer.toString(this.port));
}
return buffer.toString();
}
/**
* Obtains the host string, without scheme prefix.
*
* @return the host string, for example <code>localhost:8080</code>
*/
public String toHostString() {
if (this.port != -1) {
//the highest port number is 65535, which is length 6 with the addition of the colon
CharArrayBuffer buffer = new CharArrayBuffer(this.hostname.length() + 6);
buffer.append(this.hostname);
buffer.append(":");
buffer.append(Integer.toString(this.port));
return buffer.toString();
} else {
return this.hostname;
}
}
public String toString() {
return toURI();
}
public boolean equals(final Object obj) {
if (this == obj) return true;
if (obj instanceof HttpHost) {
HttpHost that = (HttpHost) obj;
return this.lcHostname.equals(that.lcHostname)
&& this.port == that.port
&& this.schemeName.equals(that.schemeName);
} else {
return false;
}
}
/**
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
int hash = LangUtils.HASH_SEED;
hash = LangUtils.hashCode(hash, this.lcHostname);
hash = LangUtils.hashCode(hash, this.port);
hash = LangUtils.hashCode(hash, this.schemeName);
return hash;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}

Просмотреть файл

@ -0,0 +1,47 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.net.InetAddress;
/**
* An HTTP connection over the Internet Protocol (IP).
*
* @since 4.0
*/
public interface HttpInetConnection extends HttpConnection {
InetAddress getLocalAddress();
int getLocalPort();
InetAddress getRemoteAddress();
int getRemotePort();
}

Просмотреть файл

@ -0,0 +1,201 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import ch.boye.httpclientandroidlib.params.HttpParams;
/**
* HTTP messages consist of requests from client to server and responses
* from server to client.
* <pre>
* HTTP-message = Request | Response ; HTTP/1.1 messages
* </pre>
* <p>
* HTTP messages use the generic message format of RFC 822 for
* transferring entities (the payload of the message). Both types
* of message consist of a start-line, zero or more header fields
* (also known as "headers"), an empty line (i.e., a line with nothing
* preceding the CRLF) indicating the end of the header fields,
* and possibly a message-body.
* </p>
* <pre>
* generic-message = start-line
* *(message-header CRLF)
* CRLF
* [ message-body ]
* start-line = Request-Line | Status-Line
* </pre>
*
* @since 4.0
*/
public interface HttpMessage {
/**
* Returns the protocol version this message is compatible with.
*/
ProtocolVersion getProtocolVersion();
/**
* Checks if a certain header is present in this message. Header values are
* ignored.
*
* @param name the header name to check for.
* @return true if at least one header with this name is present.
*/
boolean containsHeader(String name);
/**
* Returns all the headers with a specified name of this message. Header values
* are ignored. Headers are orderd in the sequence they will be sent over a
* connection.
*
* @param name the name of the headers to return.
* @return the headers whose name property equals <code>name</code>.
*/
Header[] getHeaders(String name);
/**
* Returns the first header with a specified name of this message. Header
* values are ignored. If there is more than one matching header in the
* message the first element of {@link #getHeaders(String)} is returned.
* If there is no matching header in the message <code>null</code> is
* returned.
*
* @param name the name of the header to return.
* @return the first header whose name property equals <code>name</code>
* or <code>null</code> if no such header could be found.
*/
Header getFirstHeader(String name);
/**
* Returns the last header with a specified name of this message. Header values
* are ignored. If there is more than one matching header in the message the
* last element of {@link #getHeaders(String)} is returned. If there is no
* matching header in the message <code>null</code> is returned.
*
* @param name the name of the header to return.
* @return the last header whose name property equals <code>name</code>.
* or <code>null</code> if no such header could be found.
*/
Header getLastHeader(String name);
/**
* Returns all the headers of this message. Headers are orderd in the sequence
* they will be sent over a connection.
*
* @return all the headers of this message
*/
Header[] getAllHeaders();
/**
* Adds a header to this message. The header will be appended to the end of
* the list.
*
* @param header the header to append.
*/
void addHeader(Header header);
/**
* Adds a header to this message. The header will be appended to the end of
* the list.
*
* @param name the name of the header.
* @param value the value of the header.
*/
void addHeader(String name, String value);
/**
* Overwrites the first header with the same name. The new header will be appended to
* the end of the list, if no header with the given name can be found.
*
* @param header the header to set.
*/
void setHeader(Header header);
/**
* Overwrites the first header with the same name. The new header will be appended to
* the end of the list, if no header with the given name can be found.
*
* @param name the name of the header.
* @param value the value of the header.
*/
void setHeader(String name, String value);
/**
* Overwrites all the headers in the message.
*
* @param headers the array of headers to set.
*/
void setHeaders(Header[] headers);
/**
* Removes a header from this message.
*
* @param header the header to remove.
*/
void removeHeader(Header header);
/**
* Removes all headers with a certain name from this message.
*
* @param name The name of the headers to remove.
*/
void removeHeaders(String name);
/**
* Returns an iterator of all the headers.
*
* @return Iterator that returns Header objects in the sequence they are
* sent over a connection.
*/
HeaderIterator headerIterator();
/**
* Returns an iterator of the headers with a given name.
*
* @param name the name of the headers over which to iterate, or
* <code>null</code> for all headers
*
* @return Iterator that returns Header objects with the argument name
* in the sequence they are sent over a connection.
*/
HeaderIterator headerIterator(String name);
/**
* Returns the parameters effective for this message as set by
* {@link #setParams(HttpParams)}.
*/
HttpParams getParams();
/**
* Provides parameters to be used for the processing of this message.
* @param params the parameters
*/
void setParams(HttpParams params);
}

Просмотреть файл

@ -0,0 +1,53 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
/**
* A request message from a client to a server includes, within the
* first line of that message, the method to be applied to the resource,
* the identifier of the resource, and the protocol version in use.
* <pre>
* Request = Request-Line
* *(( general-header
* | request-header
* | entity-header ) CRLF)
* CRLF
* [ message-body ]
* </pre>
*
* @since 4.0
*/
public interface HttpRequest extends HttpMessage {
/**
* Returns the request line of this request.
* @return the request line.
*/
RequestLine getRequestLine();
}

Просмотреть файл

@ -0,0 +1,43 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
/**
* A factory for {@link HttpRequest HttpRequest} objects.
*
* @since 4.0
*/
public interface HttpRequestFactory {
HttpRequest newHttpRequest(RequestLine requestline)
throws MethodNotSupportedException;
HttpRequest newHttpRequest(String method, String uri)
throws MethodNotSupportedException;
}

Просмотреть файл

@ -0,0 +1,68 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.io.IOException;
import ch.boye.httpclientandroidlib.protocol.HttpContext;
/**
* HTTP protocol interceptor is a routine that implements a specific aspect of
* the HTTP protocol. Usually protocol interceptors are expected to act upon
* one specific header or a group of related headers of the incoming message
* or populate the outgoing message with one specific header or a group of
* related headers.
* <p>
* Protocol Interceptors can also manipulate content entities enclosed with messages.
* Usually this is accomplished by using the 'Decorator' pattern where a wrapper
* entity class is used to decorate the original entity.
* <p>
* Protocol interceptors must be implemented as thread-safe. Similarly to
* servlets, protocol interceptors should not use instance variables unless
* access to those variables is synchronized.
*
* @since 4.0
*/
public interface HttpRequestInterceptor {
/**
* Processes a request.
* On the client side, this step is performed before the request is
* sent to the server. On the server side, this step is performed
* on incoming messages before the message body is evaluated.
*
* @param request the request to preprocess
* @param context the context for the request
*
* @throws HttpException in case of an HTTP protocol violation
* @throws IOException in case of an I/O error
*/
void process(HttpRequest request, HttpContext context)
throws HttpException, IOException;
}

Просмотреть файл

@ -0,0 +1,160 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.util.Locale;
/**
* After receiving and interpreting a request message, a server responds
* with an HTTP response message.
* <pre>
* Response = Status-Line
* *(( general-header
* | response-header
* | entity-header ) CRLF)
* CRLF
* [ message-body ]
* </pre>
*
* @since 4.0
*/
public interface HttpResponse extends HttpMessage {
/**
* Obtains the status line of this response.
* The status line can be set using one of the
* {@link #setStatusLine setStatusLine} methods,
* or it can be initialized in a constructor.
*
* @return the status line, or <code>null</code> if not yet set
*/
StatusLine getStatusLine();
/**
* Sets the status line of this response.
*
* @param statusline the status line of this response
*/
void setStatusLine(StatusLine statusline);
/**
* Sets the status line of this response.
* The reason phrase will be determined based on the current
* {@link #getLocale locale}.
*
* @param ver the HTTP version
* @param code the status code
*/
void setStatusLine(ProtocolVersion ver, int code);
/**
* Sets the status line of this response with a reason phrase.
*
* @param ver the HTTP version
* @param code the status code
* @param reason the reason phrase, or <code>null</code> to omit
*/
void setStatusLine(ProtocolVersion ver, int code, String reason);
/**
* Updates the status line of this response with a new status code.
* The status line can only be updated if it is available. It must
* have been set either explicitly or in a constructor.
* <br/>
* The reason phrase will be updated according to the new status code,
* based on the current {@link #getLocale locale}. It can be set
* explicitly using {@link #setReasonPhrase setReasonPhrase}.
*
* @param code the HTTP status code.
*
* @throws IllegalStateException
* if the status line has not be set
*
* @see HttpStatus
* @see #setStatusLine(StatusLine)
* @see #setStatusLine(ProtocolVersion,int)
*/
void setStatusCode(int code)
throws IllegalStateException;
/**
* Updates the status line of this response with a new reason phrase.
* The status line can only be updated if it is available. It must
* have been set either explicitly or in a constructor.
*
* @param reason the new reason phrase as a single-line string, or
* <code>null</code> to unset the reason phrase
*
* @throws IllegalStateException
* if the status line has not be set
*
* @see #setStatusLine(StatusLine)
* @see #setStatusLine(ProtocolVersion,int)
*/
void setReasonPhrase(String reason)
throws IllegalStateException;
/**
* Obtains the message entity of this response, if any.
* The entity is provided by calling {@link #setEntity setEntity}.
*
* @return the response entity, or
* <code>null</code> if there is none
*/
HttpEntity getEntity();
/**
* Associates a response entity with this response.
*
* @param entity the entity to associate with this response, or
* <code>null</code> to unset
*/
void setEntity(HttpEntity entity);
/**
* Obtains the locale of this response.
* The locale is used to determine the reason phrase
* for the {@link #setStatusCode status code}.
* It can be changed using {@link #setLocale setLocale}.
*
* @return the locale of this response, never <code>null</code>
*/
Locale getLocale();
/**
* Changes the locale of this response.
* If there is a status line, it's reason phrase will be updated
* according to the status code and new locale.
*
* @param loc the new locale
*
* @see #getLocale getLocale
* @see #setStatusCode setStatusCode
*/
void setLocale(Locale loc);
}

Просмотреть файл

@ -0,0 +1,67 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import ch.boye.httpclientandroidlib.protocol.HttpContext;
/**
* A factory for {@link HttpResponse HttpResponse} objects.
*
* @since 4.0
*/
public interface HttpResponseFactory {
/**
* Creates a new response from status line elements.
*
* @param ver the protocol version
* @param status the status code
* @param context the context from which to determine the locale
* for looking up a reason phrase to the status code, or
* <code>null</code> to use the default locale
*
* @return the new response with an initialized status line
*/
HttpResponse newHttpResponse(ProtocolVersion ver, int status,
HttpContext context);
/**
* Creates a new response from a status line.
*
* @param statusline the status line
* @param context the context from which to determine the locale
* for looking up a reason phrase if the status code
* is updated, or
* <code>null</code> to use the default locale
*
* @return the new response with the argument status line
*/
HttpResponse newHttpResponse(StatusLine statusline,
HttpContext context);
}

Просмотреть файл

@ -0,0 +1,68 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.io.IOException;
import ch.boye.httpclientandroidlib.protocol.HttpContext;
/**
* HTTP protocol interceptor is a routine that implements a specific aspect of
* the HTTP protocol. Usually protocol interceptors are expected to act upon
* one specific header or a group of related headers of the incoming message
* or populate the outgoing message with one specific header or a group of
* related headers. Protocol
* <p>
* Interceptors can also manipulate content entities enclosed with messages.
* Usually this is accomplished by using the 'Decorator' pattern where a wrapper
* entity class is used to decorate the original entity.
* <p>
* Protocol interceptors must be implemented as thread-safe. Similarly to
* servlets, protocol interceptors should not use instance variables unless
* access to those variables is synchronized.
*
* @since 4.0
*/
public interface HttpResponseInterceptor {
/**
* Processes a response.
* On the server side, this step is performed before the response is
* sent to the client. On the client side, this step is performed
* on incoming messages before the message body is evaluated.
*
* @param response the response to postprocess
* @param context the context for the request
*
* @throws HttpException in case of an HTTP protocol violation
* @throws IOException in case of an I/O error
*/
void process(HttpResponse response, HttpContext context)
throws HttpException, IOException;
}

Просмотреть файл

@ -0,0 +1,88 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.io.IOException;
/**
* A server-side HTTP connection, which can be used for receiving
* requests and sending responses.
*
* @since 4.0
*/
public interface HttpServerConnection extends HttpConnection {
/**
* Receives the request line and all headers available from this connection.
* The caller should examine the returned request and decide if to receive a
* request entity as well.
*
* @return a new HttpRequest object whose request line and headers are
* initialized.
* @throws HttpException in case of HTTP protocol violation
* @throws IOException in case of an I/O error
*/
HttpRequest receiveRequestHeader()
throws HttpException, IOException;
/**
* Receives the next request entity available from this connection and attaches it to
* an existing request.
* @param request the request to attach the entity to.
* @throws HttpException in case of HTTP protocol violation
* @throws IOException in case of an I/O error
*/
void receiveRequestEntity(HttpEntityEnclosingRequest request)
throws HttpException, IOException;
/**
* Sends the response line and headers of a response over this connection.
* @param response the response whose headers to send.
* @throws HttpException in case of HTTP protocol violation
* @throws IOException in case of an I/O error
*/
void sendResponseHeader(HttpResponse response)
throws HttpException, IOException;
/**
* Sends the response entity of a response over this connection.
* @param response the response whose entity to send.
* @throws HttpException in case of HTTP protocol violation
* @throws IOException in case of an I/O error
*/
void sendResponseEntity(HttpResponse response)
throws HttpException, IOException;
/**
* Sends all pending buffered data over this connection.
* @throws IOException in case of an I/O error
*/
void flush()
throws IOException;
}

Просмотреть файл

@ -0,0 +1,175 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
/**
* Constants enumerating the HTTP status codes.
* All status codes defined in RFC1945 (HTTP/1.0), RFC2616 (HTTP/1.1), and
* RFC2518 (WebDAV) are listed.
*
* @see StatusLine
*
* @since 4.0
*/
public interface HttpStatus {
// --- 1xx Informational ---
/** <tt>100 Continue</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_CONTINUE = 100;
/** <tt>101 Switching Protocols</tt> (HTTP/1.1 - RFC 2616)*/
public static final int SC_SWITCHING_PROTOCOLS = 101;
/** <tt>102 Processing</tt> (WebDAV - RFC 2518) */
public static final int SC_PROCESSING = 102;
// --- 2xx Success ---
/** <tt>200 OK</tt> (HTTP/1.0 - RFC 1945) */
public static final int SC_OK = 200;
/** <tt>201 Created</tt> (HTTP/1.0 - RFC 1945) */
public static final int SC_CREATED = 201;
/** <tt>202 Accepted</tt> (HTTP/1.0 - RFC 1945) */
public static final int SC_ACCEPTED = 202;
/** <tt>203 Non Authoritative Information</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;
/** <tt>204 No Content</tt> (HTTP/1.0 - RFC 1945) */
public static final int SC_NO_CONTENT = 204;
/** <tt>205 Reset Content</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_RESET_CONTENT = 205;
/** <tt>206 Partial Content</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_PARTIAL_CONTENT = 206;
/**
* <tt>207 Multi-Status</tt> (WebDAV - RFC 2518) or <tt>207 Partial Update
* OK</tt> (HTTP/1.1 - draft-ietf-http-v11-spec-rev-01?)
*/
public static final int SC_MULTI_STATUS = 207;
// --- 3xx Redirection ---
/** <tt>300 Mutliple Choices</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_MULTIPLE_CHOICES = 300;
/** <tt>301 Moved Permanently</tt> (HTTP/1.0 - RFC 1945) */
public static final int SC_MOVED_PERMANENTLY = 301;
/** <tt>302 Moved Temporarily</tt> (Sometimes <tt>Found</tt>) (HTTP/1.0 - RFC 1945) */
public static final int SC_MOVED_TEMPORARILY = 302;
/** <tt>303 See Other</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_SEE_OTHER = 303;
/** <tt>304 Not Modified</tt> (HTTP/1.0 - RFC 1945) */
public static final int SC_NOT_MODIFIED = 304;
/** <tt>305 Use Proxy</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_USE_PROXY = 305;
/** <tt>307 Temporary Redirect</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_TEMPORARY_REDIRECT = 307;
// --- 4xx Client Error ---
/** <tt>400 Bad Request</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_BAD_REQUEST = 400;
/** <tt>401 Unauthorized</tt> (HTTP/1.0 - RFC 1945) */
public static final int SC_UNAUTHORIZED = 401;
/** <tt>402 Payment Required</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_PAYMENT_REQUIRED = 402;
/** <tt>403 Forbidden</tt> (HTTP/1.0 - RFC 1945) */
public static final int SC_FORBIDDEN = 403;
/** <tt>404 Not Found</tt> (HTTP/1.0 - RFC 1945) */
public static final int SC_NOT_FOUND = 404;
/** <tt>405 Method Not Allowed</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_METHOD_NOT_ALLOWED = 405;
/** <tt>406 Not Acceptable</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_NOT_ACCEPTABLE = 406;
/** <tt>407 Proxy Authentication Required</tt> (HTTP/1.1 - RFC 2616)*/
public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
/** <tt>408 Request Timeout</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_REQUEST_TIMEOUT = 408;
/** <tt>409 Conflict</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_CONFLICT = 409;
/** <tt>410 Gone</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_GONE = 410;
/** <tt>411 Length Required</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_LENGTH_REQUIRED = 411;
/** <tt>412 Precondition Failed</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_PRECONDITION_FAILED = 412;
/** <tt>413 Request Entity Too Large</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_REQUEST_TOO_LONG = 413;
/** <tt>414 Request-URI Too Long</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_REQUEST_URI_TOO_LONG = 414;
/** <tt>415 Unsupported Media Type</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
/** <tt>416 Requested Range Not Satisfiable</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
/** <tt>417 Expectation Failed</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_EXPECTATION_FAILED = 417;
/**
* Static constant for a 418 error.
* <tt>418 Unprocessable Entity</tt> (WebDAV drafts?)
* or <tt>418 Reauthentication Required</tt> (HTTP/1.1 drafts?)
*/
// not used
// public static final int SC_UNPROCESSABLE_ENTITY = 418;
/**
* Static constant for a 419 error.
* <tt>419 Insufficient Space on Resource</tt>
* (WebDAV - draft-ietf-webdav-protocol-05?)
* or <tt>419 Proxy Reauthentication Required</tt>
* (HTTP/1.1 drafts?)
*/
public static final int SC_INSUFFICIENT_SPACE_ON_RESOURCE = 419;
/**
* Static constant for a 420 error.
* <tt>420 Method Failure</tt>
* (WebDAV - draft-ietf-webdav-protocol-05?)
*/
public static final int SC_METHOD_FAILURE = 420;
/** <tt>422 Unprocessable Entity</tt> (WebDAV - RFC 2518) */
public static final int SC_UNPROCESSABLE_ENTITY = 422;
/** <tt>423 Locked</tt> (WebDAV - RFC 2518) */
public static final int SC_LOCKED = 423;
/** <tt>424 Failed Dependency</tt> (WebDAV - RFC 2518) */
public static final int SC_FAILED_DEPENDENCY = 424;
// --- 5xx Server Error ---
/** <tt>500 Server Error</tt> (HTTP/1.0 - RFC 1945) */
public static final int SC_INTERNAL_SERVER_ERROR = 500;
/** <tt>501 Not Implemented</tt> (HTTP/1.0 - RFC 1945) */
public static final int SC_NOT_IMPLEMENTED = 501;
/** <tt>502 Bad Gateway</tt> (HTTP/1.0 - RFC 1945) */
public static final int SC_BAD_GATEWAY = 502;
/** <tt>503 Service Unavailable</tt> (HTTP/1.0 - RFC 1945) */
public static final int SC_SERVICE_UNAVAILABLE = 503;
/** <tt>504 Gateway Timeout</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_GATEWAY_TIMEOUT = 504;
/** <tt>505 HTTP Version Not Supported</tt> (HTTP/1.1 - RFC 2616) */
public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
/** <tt>507 Insufficient Storage</tt> (WebDAV - RFC 2518) */
public static final int SC_INSUFFICIENT_STORAGE = 507;
}

Просмотреть файл

@ -0,0 +1,106 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.io.Serializable;
/**
* Represents an HTTP version. HTTP uses a "major.minor" numbering
* scheme to indicate versions of the protocol.
* <p>
* The version of an HTTP message is indicated by an HTTP-Version field
* in the first line of the message.
* </p>
* <pre>
* HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT
* </pre>
*
* @since 4.0
*/
public final class HttpVersion extends ProtocolVersion
implements Serializable {
private static final long serialVersionUID = -5856653513894415344L;
/** The protocol name. */
public static final String HTTP = "HTTP";
/** HTTP protocol version 0.9 */
public static final HttpVersion HTTP_0_9 = new HttpVersion(0, 9);
/** HTTP protocol version 1.0 */
public static final HttpVersion HTTP_1_0 = new HttpVersion(1, 0);
/** HTTP protocol version 1.1 */
public static final HttpVersion HTTP_1_1 = new HttpVersion(1, 1);
/**
* Create an HTTP protocol version designator.
*
* @param major the major version number of the HTTP protocol
* @param minor the minor version number of the HTTP protocol
*
* @throws IllegalArgumentException if either major or minor version number is negative
*/
public HttpVersion(int major, int minor) {
super(HTTP, major, minor);
}
/**
* Obtains a specific HTTP version.
*
* @param major the major version
* @param minor the minor version
*
* @return an instance of {@link HttpVersion} with the argument version
*/
public ProtocolVersion forVersion(int major, int minor) {
if ((major == this.major) && (minor == this.minor)) {
return this;
}
if (major == 1) {
if (minor == 0) {
return HTTP_1_0;
}
if (minor == 1) {
return HTTP_1_1;
}
}
if ((major == 0) && (minor == 9)) {
return HTTP_0_9;
}
// argument checking is done in the constructor
return new HttpVersion(major, minor);
}
}

Просмотреть файл

@ -0,0 +1,57 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.io.IOException;
/**
* Signals a malformed chunked stream.
*
* @since 4.0
*/
public class MalformedChunkCodingException extends IOException {
private static final long serialVersionUID = 2158560246948994524L;
/**
* Creates a MalformedChunkCodingException without a detail message.
*/
public MalformedChunkCodingException() {
super();
}
/**
* Creates a MalformedChunkCodingException with the specified detail message.
*
* @param message The exception detail message
*/
public MalformedChunkCodingException(final String message) {
super(message);
}
}

Просмотреть файл

@ -0,0 +1,59 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
/**
* Signals that an HTTP method is not supported.
*
* @since 4.0
*/
public class MethodNotSupportedException extends HttpException {
private static final long serialVersionUID = 3365359036840171201L;
/**
* Creates a new MethodNotSupportedException with the specified detail message.
*
* @param message The exception detail message
*/
public MethodNotSupportedException(final String message) {
super(message);
}
/**
* Creates a new MethodNotSupportedException with the specified detail message and cause.
*
* @param message the exception detail message
* @param cause the <tt>Throwable</tt> that caused this exception, or <tt>null</tt>
* if the cause is unavailable, unknown, or not a <tt>Throwable</tt>
*/
public MethodNotSupportedException(final String message, final Throwable cause) {
super(message, cause);
}
}

Просмотреть файл

@ -0,0 +1,47 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
/**
* A name / value pair parameter used as an element of HTTP messages.
* <pre>
* parameter = attribute "=" value
* attribute = token
* value = token | quoted-string
* </pre>
*
*
* @since 4.0
*/
public interface NameValuePair {
String getName();
String getValue();
}

Просмотреть файл

@ -0,0 +1,50 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.io.IOException;
/**
* Signals that the target server failed to respond with a valid HTTP response.
*
* @since 4.0
*/
public class NoHttpResponseException extends IOException {
private static final long serialVersionUID = -7658940387386078766L;
/**
* Creates a new NoHttpResponseException with the specified detail message.
*
* @param message exception message
*/
public NoHttpResponseException(String message) {
super(message);
}
}

Просмотреть файл

@ -0,0 +1,61 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
/**
* Signals a parse error.
* Parse errors when receiving a message will typically trigger
* {@link ProtocolException}. Parse errors that do not occur during
* protocol execution may be handled differently.
* This is an unchecked exception, since there are cases where
* the data to be parsed has been generated and is therefore
* known to be parseable.
*
* @since 4.0
*/
public class ParseException extends RuntimeException {
private static final long serialVersionUID = -7288819855864183578L;
/**
* Creates a {@link ParseException} without details.
*/
public ParseException() {
super();
}
/**
* Creates a {@link ParseException} with a detail message.
*
* @param message the exception detail message, or <code>null</code>
*/
public ParseException(String message) {
super(message);
}
}

Просмотреть файл

@ -0,0 +1,67 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
/**
* Signals that an HTTP protocol violation has occurred.
* For example a malformed status line or headers, a missing message body, etc.
*
*
* @since 4.0
*/
public class ProtocolException extends HttpException {
private static final long serialVersionUID = -2143571074341228994L;
/**
* Creates a new ProtocolException with a <tt>null</tt> detail message.
*/
public ProtocolException() {
super();
}
/**
* Creates a new ProtocolException with the specified detail message.
*
* @param message The exception detail message
*/
public ProtocolException(String message) {
super(message);
}
/**
* Creates a new ProtocolException with the specified detail message and cause.
*
* @param message the exception detail message
* @param cause the <tt>Throwable</tt> that caused this exception, or <tt>null</tt>
* if the cause is unavailable, unknown, or not a <tt>Throwable</tt>
*/
public ProtocolException(String message, Throwable cause) {
super(message, cause);
}
}

Просмотреть файл

@ -0,0 +1,276 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.io.Serializable;
import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
/**
* Represents a protocol version. The "major.minor" numbering
* scheme is used to indicate versions of the protocol.
* <p>
* This class defines a protocol version as a combination of
* protocol name, major version number, and minor version number.
* Note that {@link #equals} and {@link #hashCode} are defined as
* final here, they cannot be overridden in derived classes.
* </p>
*
* @since 4.0
*/
public class ProtocolVersion implements Serializable, Cloneable {
private static final long serialVersionUID = 8950662842175091068L;
/** Name of the protocol. */
protected final String protocol;
/** Major version number of the protocol */
protected final int major;
/** Minor version number of the protocol */
protected final int minor;
/**
* Create a protocol version designator.
*
* @param protocol the name of the protocol, for example "HTTP"
* @param major the major version number of the protocol
* @param minor the minor version number of the protocol
*/
public ProtocolVersion(String protocol, int major, int minor) {
if (protocol == null) {
throw new IllegalArgumentException
("Protocol name must not be null.");
}
if (major < 0) {
throw new IllegalArgumentException
("Protocol major version number must not be negative.");
}
if (minor < 0) {
throw new IllegalArgumentException
("Protocol minor version number may not be negative");
}
this.protocol = protocol;
this.major = major;
this.minor = minor;
}
/**
* Returns the name of the protocol.
*
* @return the protocol name
*/
public final String getProtocol() {
return protocol;
}
/**
* Returns the major version number of the protocol.
*
* @return the major version number.
*/
public final int getMajor() {
return major;
}
/**
* Returns the minor version number of the HTTP protocol.
*
* @return the minor version number.
*/
public final int getMinor() {
return minor;
}
/**
* Obtains a specific version of this protocol.
* This can be used by derived classes to instantiate themselves instead
* of the base class, and to define constants for commonly used versions.
* <br/>
* The default implementation in this class returns <code>this</code>
* if the version matches, and creates a new {@link ProtocolVersion}
* otherwise.
*
* @param major the major version
* @param minor the minor version
*
* @return a protocol version with the same protocol name
* and the argument version
*/
public ProtocolVersion forVersion(int major, int minor) {
if ((major == this.major) && (minor == this.minor)) {
return this;
}
// argument checking is done in the constructor
return new ProtocolVersion(this.protocol, major, minor);
}
/**
* Obtains a hash code consistent with {@link #equals}.
*
* @return the hashcode of this protocol version
*/
public final int hashCode() {
return this.protocol.hashCode() ^ (this.major * 100000) ^ this.minor;
}
/**
* Checks equality of this protocol version with an object.
* The object is equal if it is a protocl version with the same
* protocol name, major version number, and minor version number.
* The specific class of the object is <i>not</i> relevant,
* instances of derived classes with identical attributes are
* equal to instances of the base class and vice versa.
*
* @param obj the object to compare with
*
* @return <code>true</code> if the argument is the same protocol version,
* <code>false</code> otherwise
*/
public final boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof ProtocolVersion)) {
return false;
}
ProtocolVersion that = (ProtocolVersion) obj;
return ((this.protocol.equals(that.protocol)) &&
(this.major == that.major) &&
(this.minor == that.minor));
}
/**
* Checks whether this protocol can be compared to another one.
* Only protocol versions with the same protocol name can be
* {@link #compareToVersion compared}.
*
* @param that the protocol version to consider
*
* @return <code>true</code> if {@link #compareToVersion compareToVersion}
* can be called with the argument, <code>false</code> otherwise
*/
public boolean isComparable(ProtocolVersion that) {
return (that != null) && this.protocol.equals(that.protocol);
}
/**
* Compares this protocol version with another one.
* Only protocol versions with the same protocol name can be compared.
* This method does <i>not</i> define a total ordering, as it would be
* required for {@link java.lang.Comparable}.
*
* @param that the protocl version to compare with
*
* @return a negative integer, zero, or a positive integer
* as this version is less than, equal to, or greater than
* the argument version.
*
* @throws IllegalArgumentException
* if the argument has a different protocol name than this object,
* or if the argument is <code>null</code>
*/
public int compareToVersion(ProtocolVersion that) {
if (that == null) {
throw new IllegalArgumentException
("Protocol version must not be null.");
}
if (!this.protocol.equals(that.protocol)) {
throw new IllegalArgumentException
("Versions for different protocols cannot be compared. " +
this + " " + that);
}
int delta = getMajor() - that.getMajor();
if (delta == 0) {
delta = getMinor() - that.getMinor();
}
return delta;
}
/**
* Tests if this protocol version is greater or equal to the given one.
*
* @param version the version against which to check this version
*
* @return <code>true</code> if this protocol version is
* {@link #isComparable comparable} to the argument
* and {@link #compareToVersion compares} as greater or equal,
* <code>false</code> otherwise
*/
public final boolean greaterEquals(ProtocolVersion version) {
return isComparable(version) && (compareToVersion(version) >= 0);
}
/**
* Tests if this protocol version is less or equal to the given one.
*
* @param version the version against which to check this version
*
* @return <code>true</code> if this protocol version is
* {@link #isComparable comparable} to the argument
* and {@link #compareToVersion compares} as less or equal,
* <code>false</code> otherwise
*/
public final boolean lessEquals(ProtocolVersion version) {
return isComparable(version) && (compareToVersion(version) <= 0);
}
/**
* Converts this protocol version to a string.
*
* @return a protocol version string, like "HTTP/1.1"
*/
public String toString() {
CharArrayBuffer buffer = new CharArrayBuffer(16);
buffer.append(this.protocol);
buffer.append('/');
buffer.append(Integer.toString(this.major));
buffer.append('.');
buffer.append(Integer.toString(this.minor));
return buffer.toString();
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}

Просмотреть файл

@ -0,0 +1 @@
These files are managed in the android-sync repo. Do not modify directly, or your changes will be lost.

Просмотреть файл

@ -0,0 +1,51 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.util.Locale;
/**
* Interface for obtaining reason phrases for HTTP status codes.
*
* @since 4.0
*/
public interface ReasonPhraseCatalog {
/**
* Obtains the reason phrase for a status code.
* The optional context allows for catalogs that detect
* the language for the reason phrase.
*
* @param status the status code, in the range 100-599
* @param loc the preferred locale for the reason phrase
*
* @return the reason phrase, or <code>null</code> if unknown
*/
public String getReason(int status, Locale loc);
}

Просмотреть файл

@ -0,0 +1,49 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
/**
* The Request-Line begins with a method token, followed by the
* Request-URI and the protocol version, and ending with CRLF. The
* elements are separated by SP characters. No CR or LF is allowed
* except in the final CRLF sequence.
* <pre>
* Request-Line = Method SP Request-URI SP HTTP-Version CRLF
* </pre>
*
* @since 4.0
*/
public interface RequestLine {
String getMethod();
ProtocolVersion getProtocolVersion();
String getUri();
}

Просмотреть файл

@ -0,0 +1,52 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
/**
* The first line of a Response message is the Status-Line, consisting
* of the protocol version followed by a numeric status code and its
* associated textual phrase, with each element separated by SP
* characters. No CR or LF is allowed except in the final CRLF sequence.
* <pre>
* Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
* </pre>
*
* @see HttpStatus
* @version $Id: StatusLine.java 937295 2010-04-23 13:44:00Z olegk $
*
* @since 4.0
*/
public interface StatusLine {
ProtocolVersion getProtocolVersion();
int getStatusCode();
String getReasonPhrase();
}

Просмотреть файл

@ -0,0 +1,59 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import java.util.Iterator;
/**
* An iterator for {@link String} tokens.
* This interface is designed as a complement to
* {@link HeaderElementIterator}, in cases where the items
* are plain strings rather than full header elements.
*
* @since 4.0
*/
public interface TokenIterator extends Iterator {
/**
* Indicates whether there is another token in this iteration.
*
* @return <code>true</code> if there is another token,
* <code>false</code> otherwise
*/
boolean hasNext();
/**
* Obtains the next token from this iteration.
* This method should only be called while {@link #hasNext hasNext}
* is true.
*
* @return the next token in this iteration
*/
String nextToken();
}

Просмотреть файл

@ -0,0 +1,48 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
/**
* Signals a truncated chunk in a chunked stream.
*
* @since 4.1
*/
public class TruncatedChunkException extends MalformedChunkCodingException {
private static final long serialVersionUID = -23506263930279460L;
/**
* Creates a TruncatedChunkException with the specified detail message.
*
* @param message The exception detail message
*/
public TruncatedChunkException(final String message) {
super(message);
}
}

Просмотреть файл

@ -0,0 +1,58 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib;
import ch.boye.httpclientandroidlib.ProtocolException;
/**
* Signals an unsupported version of the HTTP protocol.
*
* @since 4.0
*/
public class UnsupportedHttpVersionException extends ProtocolException {
private static final long serialVersionUID = -1348448090193107031L;
/**
* Creates an exception without a detail message.
*/
public UnsupportedHttpVersionException() {
super();
}
/**
* Creates an exception with the specified detail message.
*
* @param message The exception detail message
*/
public UnsupportedHttpVersionException(final String message) {
super(message);
}
}

Просмотреть файл

@ -0,0 +1,85 @@
package ch.boye.httpclientandroidlib.androidextra;
import android.util.Log;
public class HttpClientAndroidLog {
private String logTag;
private boolean debugEnabled;
private boolean errorEnabled;
private boolean warnEnabled;
private boolean infoEnabled;
public HttpClientAndroidLog(Object tag) {
logTag=tag.toString();
debugEnabled=false;
errorEnabled=false;
warnEnabled=false;
infoEnabled=false;
}
public void enableDebug(boolean enable) {
debugEnabled=enable;
}
public boolean isDebugEnabled() {
return debugEnabled;
}
public void debug(Object message) {
Log.d(logTag, message.toString());
}
public void debug(Object message, Throwable t) {
Log.d(logTag, message.toString(), t);
}
public void enableError(boolean enable) {
errorEnabled=enable;
}
public boolean isErrorEnabled() {
return errorEnabled;
}
public void error(Object message) {
Log.e(logTag, message.toString());
}
public void error(Object message, Throwable t) {
Log.e(logTag, message.toString(), t);
}
public void enableWarn(boolean enable) {
warnEnabled=enable;
}
public boolean isWarnEnabled() {
return warnEnabled;
}
public void warn(Object message) {
Log.w(logTag, message.toString());
}
public void warn(Object message, Throwable t) {
Log.w(logTag, message.toString(), t);
}
public void enableInfo(boolean enable) {
infoEnabled=enable;
}
public boolean isInfoEnabled() {
return infoEnabled;
}
public void info(Object message) {
Log.i(logTag, message.toString());
}
public void info(Object message, Throwable t) {
Log.i(logTag, message.toString(), t);
}
}

Просмотреть файл

@ -0,0 +1,75 @@
/*
* ====================================================================
*
* 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 ch.boye.httpclientandroidlib.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* The field or method to which this annotation is applied can only be accessed
* when holding a particular lock, which may be a built-in (synchronization) lock,
* or may be an explicit java.util.concurrent.Lock.
*
* The argument determines which lock guards the annotated field or method:
* <ul>
* <li>
* <code>this</code> : The intrinsic lock of the object in whose class the field is defined.
* </li>
* <li>
* <code>class-name.this</code> : For inner classes, it may be necessary to disambiguate 'this';
* the <em>class-name.this</em> designation allows you to specify which 'this' reference is intended
* </li>
* <li>
* <code>itself</code> : For reference fields only; the object to which the field refers.
* </li>
* <li>
* <code>field-name</code> : The lock object is referenced by the (instance or static) field
* specified by <em>field-name</em>.
* </li>
* <li>
* <code>class-name.field-name</code> : The lock object is reference by the static field specified
* by <em>class-name.field-name</em>.
* </li>
* <li>
* <code>method-name()</code> : The lock object is returned by calling the named nil-ary method.
* </li>
* <li>
* <code>class-name.class</code> : The Class object for the specified class should be used as the lock object.
* </li>
* <p>
* Based on code developed by Brian Goetz and Tim Peierls and concepts
* published in 'Java Concurrency in Practice' by Brian Goetz, Tim Peierls,
* Joshua Bloch, Joseph Bowbeer, David Holmes and Doug Lea.
*/
@Documented
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.CLASS) // The original version used RUNTIME
public @interface GuardedBy {
String value();
}

Просмотреть файл

@ -0,0 +1,58 @@
/*
* ====================================================================
*
* 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 ch.boye.httpclientandroidlib.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* The class to which this annotation is applied is immutable. This means that
* its state cannot be seen to change by callers, which implies that
* <ul>
* <li> all public fields are final, </li>
* <li> all public final reference fields refer to other immutable objects, and </li>
* <li> constructors and methods do not publish references to any internal state
* which is potentially mutable by the implementation. </li>
* </ul>
* Immutable objects may still have internal mutable state for purposes of performance
* optimization; some state variables may be lazily computed, so long as they are computed
* from immutable state and that callers cannot tell the difference.
* <p>
* Immutable objects are inherently thread-safe; they may be passed between threads or
* published without synchronization.
* <p>
* Based on code developed by Brian Goetz and Tim Peierls and concepts
* published in 'Java Concurrency in Practice' by Brian Goetz, Tim Peierls,
* Joshua Bloch, Joseph Bowbeer, David Holmes and Doug Lea.
*/
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS) // The original version used RUNTIME
public @interface Immutable {
}

Просмотреть файл

@ -0,0 +1,49 @@
/*
* ====================================================================
*
* 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 ch.boye.httpclientandroidlib.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* The class to which this annotation is applied is not thread-safe.
* This annotation primarily exists for clarifying the non-thread-safety of a class
* that might otherwise be assumed to be thread-safe, despite the fact that it is a bad
* idea to assume a class is thread-safe without good reason.
* @see ThreadSafe
* <p>
* Based on code developed by Brian Goetz and Tim Peierls and concepts
* published in 'Java Concurrency in Practice' by Brian Goetz, Tim Peierls,
* Joshua Bloch, Joseph Bowbeer, David Holmes and Doug Lea.
*/
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS) // The original version used RUNTIME
public @interface NotThreadSafe {
}

Просмотреть файл

@ -0,0 +1,50 @@
/*
* ====================================================================
*
* 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 ch.boye.httpclientandroidlib.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* The class to which this annotation is applied is thread-safe. This means that
* no sequences of accesses (reads and writes to public fields, calls to public methods)
* may put the object into an invalid state, regardless of the interleaving of those actions
* by the runtime, and without requiring any additional synchronization or coordination on the
* part of the caller.
* @see NotThreadSafe
* <p>
* Based on code developed by Brian Goetz and Tim Peierls and concepts
* published in 'Java Concurrency in Practice' by Brian Goetz, Tim Peierls,
* Joshua Bloch, Joseph Bowbeer, David Holmes and Doug Lea.
*/
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS) // The original version used RUNTIME
public @interface ThreadSafe {
}

Просмотреть файл

@ -0,0 +1,64 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib.auth;
import ch.boye.httpclientandroidlib.annotation.Immutable;
/**
* Constants and static helpers related to the HTTP authentication.
*
*
* @since 4.0
*/
@Immutable
public final class AUTH {
/**
* The www authenticate challange header.
*/
public static final String WWW_AUTH = "WWW-Authenticate";
/**
* The www authenticate response header.
*/
public static final String WWW_AUTH_RESP = "Authorization";
/**
* The proxy authenticate challange header.
*/
public static final String PROXY_AUTH = "Proxy-Authenticate";
/**
* The proxy authenticate response header.
*/
public static final String PROXY_AUTH_RESP = "Proxy-Authorization";
private AUTH() {
}
}

Просмотреть файл

@ -0,0 +1,130 @@
/*
* ====================================================================
*
* 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 ch.boye.httpclientandroidlib.auth;
import ch.boye.httpclientandroidlib.Header;
import ch.boye.httpclientandroidlib.HttpRequest;
/**
* This interface represents an abstract challenge-response oriented
* authentication scheme.
* <p>
* An authentication scheme should be able to support the following
* functions:
* <ul>
* <li>Parse and process the challenge sent by the target server
* in response to request for a protected resource
* <li>Provide its textual designation
* <li>Provide its parameters, if available
* <li>Provide the realm this authentication scheme is applicable to,
* if available
* <li>Generate authorization string for the given set of credentials
* and the HTTP request in response to the authorization challenge.
* </ul>
* <p>
* Authentication schemes may be stateful involving a series of
* challenge-response exchanges.
* <p>
* IMPORTANT: implementations of this interface MUST also implement {@link ContextAwareAuthScheme}
* interface in order to remain API compatible with newer versions of HttpClient.
*
* @since 4.0
*/
public interface AuthScheme {
/**
* Processes the given challenge token. Some authentication schemes
* may involve multiple challenge-response exchanges. Such schemes must be able
* to maintain the state information when dealing with sequential challenges
*
* @param header the challenge header
*/
void processChallenge(final Header header) throws MalformedChallengeException;
/**
* Returns textual designation of the given authentication scheme.
*
* @return the name of the given authentication scheme
*/
String getSchemeName();
/**
* Returns authentication parameter with the given name, if available.
*
* @param name The name of the parameter to be returned
*
* @return the parameter with the given name
*/
String getParameter(final String name);
/**
* Returns authentication realm. If the concept of an authentication
* realm is not applicable to the given authentication scheme, returns
* <code>null</code>.
*
* @return the authentication realm
*/
String getRealm();
/**
* Tests if the authentication scheme is provides authorization on a per
* connection basis instead of usual per request basis
*
* @return <tt>true</tt> if the scheme is connection based, <tt>false</tt>
* if the scheme is request based.
*/
boolean isConnectionBased();
/**
* Authentication process may involve a series of challenge-response exchanges.
* This method tests if the authorization process has been completed, either
* successfully or unsuccessfully, that is, all the required authorization
* challenges have been processed in their entirety.
*
* @return <tt>true</tt> if the authentication process has been completed,
* <tt>false</tt> otherwise.
*/
boolean isComplete();
/**
* Produces an authorization string for the given set of {@link Credentials}.
*
* @param credentials The set of credentials to be used for athentication
* @param request The request being authenticated
* @throws AuthenticationException if authorization string cannot
* be generated due to an authentication failure
*
* @return the authorization string
*
* @deprecated Use {@link ContextAwareAuthScheme#authenticate(Credentials, HttpRequest, ch.boye.httpclientandroidlib.protocol.HttpContext)}
*/
@Deprecated
Header authenticate(Credentials credentials, HttpRequest request)
throws AuthenticationException;
}

Просмотреть файл

@ -0,0 +1,48 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib.auth;
import ch.boye.httpclientandroidlib.params.HttpParams;
/**
* Factory for {@link AuthScheme} implementations.
*
* @since 4.0
*/
public interface AuthSchemeFactory {
/**
* Creates an instance of {@link AuthScheme} using given HTTP parameters.
*
* @param params HTTP parameters.
*
* @return auth scheme.
*/
AuthScheme newInstance(HttpParams params);
}

Просмотреть файл

@ -0,0 +1,144 @@
/*
* ====================================================================
*
* 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 ch.boye.httpclientandroidlib.auth;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
import ch.boye.httpclientandroidlib.params.HttpParams;
/**
* Authentication scheme registry that can be used to obtain the corresponding
* authentication scheme implementation for a given type of authorization challenge.
*
* @since 4.0
*/
@ThreadSafe
public final class AuthSchemeRegistry {
private final ConcurrentHashMap<String,AuthSchemeFactory> registeredSchemes;
public AuthSchemeRegistry() {
super();
this.registeredSchemes = new ConcurrentHashMap<String,AuthSchemeFactory>();
}
/**
* Registers a {@link AuthSchemeFactory} with the given identifier. If a factory with the
* given name already exists it will be overridden. This name is the same one used to
* retrieve the {@link AuthScheme authentication scheme} from {@link #getAuthScheme}.
*
* <p>
* Please note that custom authentication preferences, if used, need to be updated accordingly
* for the new {@link AuthScheme authentication scheme} to take effect.
* </p>
*
* @param name the identifier for this scheme
* @param factory the {@link AuthSchemeFactory} class to register
*
* @see #getAuthScheme
*/
public void register(
final String name,
final AuthSchemeFactory factory) {
if (name == null) {
throw new IllegalArgumentException("Name may not be null");
}
if (factory == null) {
throw new IllegalArgumentException("Authentication scheme factory may not be null");
}
registeredSchemes.put(name.toLowerCase(Locale.ENGLISH), factory);
}
/**
* Unregisters the class implementing an {@link AuthScheme authentication scheme} with
* the given name.
*
* @param name the identifier of the class to unregister
*/
public void unregister(final String name) {
if (name == null) {
throw new IllegalArgumentException("Name may not be null");
}
registeredSchemes.remove(name.toLowerCase(Locale.ENGLISH));
}
/**
* Gets the {@link AuthScheme authentication scheme} with the given name.
*
* @param name the {@link AuthScheme authentication scheme} identifier
* @param params the {@link HttpParams HTTP parameters} for the authentication
* scheme.
*
* @return {@link AuthScheme authentication scheme}
*
* @throws IllegalStateException if a scheme with the given name cannot be found
*/
public AuthScheme getAuthScheme(final String name, final HttpParams params)
throws IllegalStateException {
if (name == null) {
throw new IllegalArgumentException("Name may not be null");
}
AuthSchemeFactory factory = registeredSchemes.get(name.toLowerCase(Locale.ENGLISH));
if (factory != null) {
return factory.newInstance(params);
} else {
throw new IllegalStateException("Unsupported authentication scheme: " + name);
}
}
/**
* Obtains a list containing the names of all registered {@link AuthScheme authentication
* schemes}
*
* @return list of registered scheme names
*/
public List<String> getSchemeNames() {
return new ArrayList<String>(registeredSchemes.keySet());
}
/**
* Populates the internal collection of registered {@link AuthScheme authentication schemes}
* with the content of the map passed as a parameter.
*
* @param map authentication schemes
*/
public void setItems(final Map<String, AuthSchemeFactory> map) {
if (map == null) {
return;
}
registeredSchemes.clear();
registeredSchemes.putAll(map);
}
}

Просмотреть файл

@ -0,0 +1,289 @@
/*
* ====================================================================
*
* 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 ch.boye.httpclientandroidlib.auth;
import java.util.Locale;
import ch.boye.httpclientandroidlib.annotation.Immutable;
import ch.boye.httpclientandroidlib.util.LangUtils;
/**
* The class represents an authentication scope consisting of a host name,
* a port number, a realm name and an authentication scheme name which
* {@link Credentials Credentials} apply to.
*
*
* @since 4.0
*/
@Immutable
public class AuthScope {
/**
* The <tt>null</tt> value represents any host. In the future versions of
* HttpClient the use of this parameter will be discontinued.
*/
public static final String ANY_HOST = null;
/**
* The <tt>-1</tt> value represents any port.
*/
public static final int ANY_PORT = -1;
/**
* The <tt>null</tt> value represents any realm.
*/
public static final String ANY_REALM = null;
/**
* The <tt>null</tt> value represents any authentication scheme.
*/
public static final String ANY_SCHEME = null;
/**
* Default scope matching any host, port, realm and authentication scheme.
* In the future versions of HttpClient the use of this parameter will be
* discontinued.
*/
public static final AuthScope ANY = new AuthScope(ANY_HOST, ANY_PORT, ANY_REALM, ANY_SCHEME);
/** The authentication scheme the credentials apply to. */
private final String scheme;
/** The realm the credentials apply to. */
private final String realm;
/** The host the credentials apply to. */
private final String host;
/** The port the credentials apply to. */
private final int port;
/** Creates a new credentials scope for the given
* <tt>host</tt>, <tt>port</tt>, <tt>realm</tt>, and
* <tt>authentication scheme</tt>.
*
* @param host the host the credentials apply to. May be set
* to <tt>null</tt> if credentials are applicable to
* any host.
* @param port the port the credentials apply to. May be set
* to negative value if credentials are applicable to
* any port.
* @param realm the realm the credentials apply to. May be set
* to <tt>null</tt> if credentials are applicable to
* any realm.
* @param scheme the authentication scheme the credentials apply to.
* May be set to <tt>null</tt> if credentials are applicable to
* any authentication scheme.
*/
public AuthScope(final String host, int port,
final String realm, final String scheme)
{
this.host = (host == null) ? ANY_HOST: host.toLowerCase(Locale.ENGLISH);
this.port = (port < 0) ? ANY_PORT: port;
this.realm = (realm == null) ? ANY_REALM: realm;
this.scheme = (scheme == null) ? ANY_SCHEME: scheme.toUpperCase(Locale.ENGLISH);
}
/** Creates a new credentials scope for the given
* <tt>host</tt>, <tt>port</tt>, <tt>realm</tt>, and any
* authentication scheme.
*
* @param host the host the credentials apply to. May be set
* to <tt>null</tt> if credentials are applicable to
* any host.
* @param port the port the credentials apply to. May be set
* to negative value if credentials are applicable to
* any port.
* @param realm the realm the credentials apply to. May be set
* to <tt>null</tt> if credentials are applicable to
* any realm.
*/
public AuthScope(final String host, int port, final String realm) {
this(host, port, realm, ANY_SCHEME);
}
/** Creates a new credentials scope for the given
* <tt>host</tt>, <tt>port</tt>, any realm name, and any
* authentication scheme.
*
* @param host the host the credentials apply to. May be set
* to <tt>null</tt> if credentials are applicable to
* any host.
* @param port the port the credentials apply to. May be set
* to negative value if credentials are applicable to
* any port.
*/
public AuthScope(final String host, int port) {
this(host, port, ANY_REALM, ANY_SCHEME);
}
/**
* Creates a copy of the given credentials scope.
*/
public AuthScope(final AuthScope authscope) {
super();
if (authscope == null) {
throw new IllegalArgumentException("Scope may not be null");
}
this.host = authscope.getHost();
this.port = authscope.getPort();
this.realm = authscope.getRealm();
this.scheme = authscope.getScheme();
}
/**
* @return the host
*/
public String getHost() {
return this.host;
}
/**
* @return the port
*/
public int getPort() {
return this.port;
}
/**
* @return the realm name
*/
public String getRealm() {
return this.realm;
}
/**
* @return the scheme type
*/
public String getScheme() {
return this.scheme;
}
/**
* Tests if the authentication scopes match.
*
* @return the match factor. Negative value signifies no match.
* Non-negative signifies a match. The greater the returned value
* the closer the match.
*/
public int match(final AuthScope that) {
int factor = 0;
if (LangUtils.equals(this.scheme, that.scheme)) {
factor += 1;
} else {
if (this.scheme != ANY_SCHEME && that.scheme != ANY_SCHEME) {
return -1;
}
}
if (LangUtils.equals(this.realm, that.realm)) {
factor += 2;
} else {
if (this.realm != ANY_REALM && that.realm != ANY_REALM) {
return -1;
}
}
if (this.port == that.port) {
factor += 4;
} else {
if (this.port != ANY_PORT && that.port != ANY_PORT) {
return -1;
}
}
if (LangUtils.equals(this.host, that.host)) {
factor += 8;
} else {
if (this.host != ANY_HOST && that.host != ANY_HOST) {
return -1;
}
}
return factor;
}
/**
* @see java.lang.Object#equals(Object)
*/
@Override
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (o == this) {
return true;
}
if (!(o instanceof AuthScope)) {
return super.equals(o);
}
AuthScope that = (AuthScope) o;
return
LangUtils.equals(this.host, that.host)
&& this.port == that.port
&& LangUtils.equals(this.realm, that.realm)
&& LangUtils.equals(this.scheme, that.scheme);
}
/**
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
if (this.scheme != null) {
buffer.append(this.scheme.toUpperCase(Locale.ENGLISH));
buffer.append(' ');
}
if (this.realm != null) {
buffer.append('\'');
buffer.append(this.realm);
buffer.append('\'');
} else {
buffer.append("<any realm>");
}
if (this.host != null) {
buffer.append('@');
buffer.append(this.host);
if (this.port >= 0) {
buffer.append(':');
buffer.append(this.port);
}
}
return buffer.toString();
}
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
int hash = LangUtils.HASH_SEED;
hash = LangUtils.hashCode(hash, this.host);
hash = LangUtils.hashCode(hash, this.port);
hash = LangUtils.hashCode(hash, this.realm);
hash = LangUtils.hashCode(hash, this.scheme);
return hash;
}
}

Просмотреть файл

@ -0,0 +1,145 @@
/*
* ====================================================================
*
* 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 ch.boye.httpclientandroidlib.auth;
import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
/**
* This class provides detailed information about the state of the
* authentication process.
*
*
* @since 4.0
*/
@NotThreadSafe
public class AuthState {
/** Actual authentication scheme */
private AuthScheme authScheme;
/** Actual authentication scope */
private AuthScope authScope;
/** Credentials selected for authentication */
private Credentials credentials;
/**
* Default constructor.
*
*/
public AuthState() {
super();
}
/**
* Invalidates the authentication state by resetting its parameters.
*/
public void invalidate() {
this.authScheme = null;
this.authScope = null;
this.credentials = null;
}
public boolean isValid() {
return this.authScheme != null;
}
/**
* Assigns the given {@link AuthScheme authentication scheme}.
*
* @param authScheme the {@link AuthScheme authentication scheme}
*/
public void setAuthScheme(final AuthScheme authScheme) {
if (authScheme == null) {
invalidate();
return;
}
this.authScheme = authScheme;
}
/**
* Returns the {@link AuthScheme authentication scheme}.
*
* @return {@link AuthScheme authentication scheme}
*/
public AuthScheme getAuthScheme() {
return this.authScheme;
}
/**
* Returns user {@link Credentials} selected for authentication if available
*
* @return user credentials if available, <code>null</code otherwise
*/
public Credentials getCredentials() {
return this.credentials;
}
/**
* Sets user {@link Credentials} to be used for authentication
*
* @param credentials User credentials
*/
public void setCredentials(final Credentials credentials) {
this.credentials = credentials;
}
/**
* Returns actual {@link AuthScope} if available
*
* @return actual authentication scope if available, <code>null</code otherwise
*/
public AuthScope getAuthScope() {
return this.authScope;
}
/**
* Sets actual {@link AuthScope}.
*
* @param authScope Authentication scope
*/
public void setAuthScope(final AuthScope authScope) {
this.authScope = authScope;
}
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append("auth scope [");
buffer.append(this.authScope);
buffer.append("]; credentials set [");
buffer.append(this.credentials != null ? "true" : "false");
buffer.append("]");
return buffer.toString();
}
}

Просмотреть файл

@ -0,0 +1,71 @@
/*
* ====================================================================
*
* 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 ch.boye.httpclientandroidlib.auth;
import ch.boye.httpclientandroidlib.annotation.Immutable;
import ch.boye.httpclientandroidlib.ProtocolException;
/**
* Signals a failure in authentication process
*
*
* @since 4.0
*/
@Immutable
public class AuthenticationException extends ProtocolException {
private static final long serialVersionUID = -6794031905674764776L;
/**
* Creates a new AuthenticationException with a <tt>null</tt> detail message.
*/
public AuthenticationException() {
super();
}
/**
* Creates a new AuthenticationException with the specified message.
*
* @param message the exception detail message
*/
public AuthenticationException(String message) {
super(message);
}
/**
* Creates a new AuthenticationException with the specified detail message and cause.
*
* @param message the exception detail message
* @param cause the <tt>Throwable</tt> that caused this exception, or <tt>null</tt>
* if the cause is unavailable, unknown, or not a <tt>Throwable</tt>
*/
public AuthenticationException(String message, Throwable cause) {
super(message, cause);
}
}

Просмотреть файл

@ -0,0 +1,89 @@
/*
* ====================================================================
*
* 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 ch.boye.httpclientandroidlib.auth;
import java.io.Serializable;
import java.security.Principal;
import ch.boye.httpclientandroidlib.annotation.Immutable;
import ch.boye.httpclientandroidlib.util.LangUtils;
/**
* Basic user principal used for HTTP authentication
*
* @since 4.0
*/
@Immutable
public final class BasicUserPrincipal implements Principal, Serializable {
private static final long serialVersionUID = -2266305184969850467L;
private final String username;
public BasicUserPrincipal(final String username) {
super();
if (username == null) {
throw new IllegalArgumentException("User name may not be null");
}
this.username = username;
}
public String getName() {
return this.username;
}
@Override
public int hashCode() {
int hash = LangUtils.HASH_SEED;
hash = LangUtils.hashCode(hash, this.username);
return hash;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o instanceof BasicUserPrincipal) {
BasicUserPrincipal that = (BasicUserPrincipal) o;
if (LangUtils.equals(this.username, that.username)) {
return true;
}
}
return false;
}
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append("[principal: ");
buffer.append(this.username);
buffer.append("]");
return buffer.toString();
}
}

Некоторые файлы не были показаны из-за слишком большого количества измененных файлов Показать больше