1 package ch
.cyberduck
.core
;
4 * Copyright (c) 2012 David Kocher. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to:
18 * dkocher@cyberduck.ch
21 import org
.apache
.commons
.lang3
.StringUtils
;
23 import java
.io
.UnsupportedEncodingException
;
24 import java
.net
.URLEncoder
;
25 import java
.util
.StringTokenizer
;
30 public final class URIEncoder
{
32 private URIEncoder() {
41 * @see java.net.URLEncoder#encode(String, String)
43 public static String
encode(final String p
) {
45 final StringBuilder b
= new StringBuilder();
46 final StringTokenizer t
= new StringTokenizer(p
, "/");
47 if(!t
.hasMoreTokens()) {
50 if(StringUtils
.startsWith(p
, String
.valueOf(Path
.DELIMITER
))) {
51 b
.append(Path
.DELIMITER
);
53 while(t
.hasMoreTokens()) {
54 b
.append(URLEncoder
.encode(t
.nextToken(), "UTF-8"));
55 if(t
.hasMoreTokens()) {
56 b
.append(Path
.DELIMITER
);
59 if(StringUtils
.endsWith(p
, String
.valueOf(Path
.DELIMITER
))) {
60 b
.append(Path
.DELIMITER
);
62 // Becuase URLEncoder uses <code>application/x-www-form-urlencoded</code> we have to replace these
63 // for proper URI percented encoding.
64 return b
.toString().replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
66 catch(UnsupportedEncodingException e
) {