Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8eec1e9b16ba7e7e546177ad228cc66a513f3e92 (plain) (blame)
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*******************************************************************************
 * Copyright (c) 1999, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.http;

import java.io.UnsupportedEncodingException;

public class URI {
	/**
	 * Takes an encoded string and decodes it
	 * @param input encoded String
	 * @param charset The charset to use convert escaped characters
	 * @return Decoded String.
	 */
	public static String decode(String input, String charset) {
		if (input == null) {
			return null;
		}

		return decode(input, 0, input.length(), charset);
	}

	/**
	 * Takes an encoded string and decodes it
	 * @param input encoded String (must not be null)
	 * @param begin the beginning index, inclusive.
	 * @param end   the ending index, exclusive.
	 * @param charset The charset to use convert escaped characters
	 * @return Decoded String.
	 */
	public static String decode(String input, int begin, int end, String charset) {
		if (input == null) {
			return (null);
		}

		int index = input.indexOf('%', begin);
		if ((index == -1) || (index >= end)) {
			return input.substring(begin, end).replace('+', ' ');
		}

		int size = end - begin;
		StringBuffer result = new StringBuffer(size);
		byte[] bytes = new byte[size];
		int length = 0;

		for (int i = begin; i < end; i++) {
			char c = input.charAt(i);

			if (c == '%') {
				if (i + 2 >= end) {
					throw new IllegalArgumentException();
				}

				i++;
				int digit = Character.digit(input.charAt(i), 16);
				if (digit == -1) {
					throw new IllegalArgumentException();
				}
				int value = (digit << 4);

				i++;
				digit = Character.digit(input.charAt(i), 16);
				if (digit == -1) {
					throw new IllegalArgumentException();
				}
				value |= digit;

				bytes[length] = (byte) value;
				length++;
			} else {
				if (length > 0) {
					result.append(convert(bytes, 0, length, charset));

					length = 0;
				}

				if (c == '+') {
					c = ' ';
				}

				result.append(c);
			}
		}

		if (length > 0) {
			result.append(convert(bytes, 0, length, charset));

			length = 0;
		}

		return result.toString();
	}

	/**
	 * Convert bytes to a String using the supplied charset.
	 * @param input Array of bytes to convert.
	 * @param offset the beginning index, inclusive.
	 * @param length number of bytes to convert.
	 * @param charset The charset to use convert the bytes.
	 * @return String containing converted bytes.
	 */
	public static String convert(byte[] input, int offset, int length, String charset) {
		if (charset != null) {
			try {
				return new String(input, offset, length, charset);
			} catch (UnsupportedEncodingException e) {
				/* if the supplied charset is invalid,
				 * fall through to use 8859_1.
				 */
			}
		}

		try {
			return new String(input, offset, length, "8859_1"); //$NON-NLS-1$
		} catch (UnsupportedEncodingException e) {
			/* in the unlikely event 8859_1 is not present */
			return new String(input, offset, length);
		}
	}
}

Back to the top