Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpUrl in Kotlin #4745

Merged
merged 3 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion okhttp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,71 @@ task japicmp(type: me.champeau.gradle.japicmp.JapicmpTask, dependsOn: 'jar') {
ignoreMissingClasses = true
includeSynthetic = true
methodExcludes = [
'okhttp3.TlsVersion#javaName()', // Became 'final' in 4.0.0.
// Became 'final' in 4.0.0:
'okhttp3.TlsVersion#javaName()',
'okhttp3.FormBody#encodedName()',
'okhttp3.FormBody#encodedValue()',
'okhttp3.FormBody#name()',
'okhttp3.FormBody#size()',
'okhttp3.FormBody#value()',
'okhttp3.FormBody$Builder#add()',
'okhttp3.FormBody$Builder#addEncoded()',
'okhttp3.FormBody$Builder#build()',
'okhttp3.HttpUrl#encodedFragment()',
'okhttp3.HttpUrl#encodedPassword()',
'okhttp3.HttpUrl#encodedPath()',
'okhttp3.HttpUrl#encodedPathSegments()',
'okhttp3.HttpUrl#encodedQuery()',
'okhttp3.HttpUrl#encodedUsername()',
'okhttp3.HttpUrl#fragment()',
'okhttp3.HttpUrl#host()',
'okhttp3.HttpUrl#isHttps()',
'okhttp3.HttpUrl#newBuilder()',
'okhttp3.HttpUrl#newBuilder()',
'okhttp3.HttpUrl#password()',
'okhttp3.HttpUrl#pathSegments()',
'okhttp3.HttpUrl#pathSize()',
'okhttp3.HttpUrl#port()',
'okhttp3.HttpUrl#query()',
'okhttp3.HttpUrl#queryParameter()',
'okhttp3.HttpUrl#queryParameterName()',
'okhttp3.HttpUrl#queryParameterNames()',
'okhttp3.HttpUrl#queryParameterValue()',
'okhttp3.HttpUrl#queryParameterValues()',
'okhttp3.HttpUrl#querySize()',
'okhttp3.HttpUrl#redact()',
'okhttp3.HttpUrl#resolve()',
'okhttp3.HttpUrl#scheme()',
'okhttp3.HttpUrl#topPrivateDomain()',
'okhttp3.HttpUrl#uri()',
'okhttp3.HttpUrl#url()',
'okhttp3.HttpUrl#username()',
'okhttp3.HttpUrl$Builder#addEncodedPathSegment()',
'okhttp3.HttpUrl$Builder#addEncodedPathSegments()',
'okhttp3.HttpUrl$Builder#addEncodedQueryParameter()',
'okhttp3.HttpUrl$Builder#addPathSegment()',
'okhttp3.HttpUrl$Builder#addPathSegments()',
'okhttp3.HttpUrl$Builder#addQueryParameter()',
'okhttp3.HttpUrl$Builder#build()',
'okhttp3.HttpUrl$Builder#encodedFragment()',
'okhttp3.HttpUrl$Builder#encodedPassword()',
'okhttp3.HttpUrl$Builder#encodedPath()',
'okhttp3.HttpUrl$Builder#encodedQuery()',
'okhttp3.HttpUrl$Builder#encodedUsername()',
'okhttp3.HttpUrl$Builder#fragment()',
'okhttp3.HttpUrl$Builder#host()',
'okhttp3.HttpUrl$Builder#password()',
'okhttp3.HttpUrl$Builder#port()',
'okhttp3.HttpUrl$Builder#query()',
'okhttp3.HttpUrl$Builder#removeAllEncodedQueryParameters()',
'okhttp3.HttpUrl$Builder#removeAllQueryParameters()',
'okhttp3.HttpUrl$Builder#removePathSegment()',
'okhttp3.HttpUrl$Builder#scheme()',
'okhttp3.HttpUrl$Builder#setEncodedPathSegment()',
'okhttp3.HttpUrl$Builder#setEncodedQueryParameter()',
'okhttp3.HttpUrl$Builder#setPathSegment()',
'okhttp3.HttpUrl$Builder#setQueryParameter()',
'okhttp3.HttpUrl$Builder#username()',
]
}
check.dependsOn(japicmp)
145 changes: 57 additions & 88 deletions okhttp/src/main/java/okhttp3/FormBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,128 +13,97 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3;
package okhttp3

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
import okhttp3.internal.Util;
import okio.Buffer;
import okio.BufferedSink;
import okhttp3.HttpUrl.Companion.FORM_ENCODE_SET
import okhttp3.HttpUrl.Companion.percentDecode
import okhttp3.internal.Util
import okio.Buffer
import okio.BufferedSink
import java.io.IOException
import java.nio.charset.Charset

import static okhttp3.HttpUrl.FORM_ENCODE_SET;
import static okhttp3.HttpUrl.percentDecode;
class FormBody internal constructor(
encodedNames: List<String>,
encodedValues: List<String>
) : RequestBody() {
private val encodedNames: List<String> = Util.immutableList(encodedNames)
private val encodedValues: List<String> = Util.immutableList(encodedValues)

public final class FormBody extends RequestBody {
private static final MediaType CONTENT_TYPE = MediaType.get("application/x-www-form-urlencoded");
/** The number of key-value pairs in this form-encoded body. */
fun size(): Int = encodedNames.size

private final List<String> encodedNames;
private final List<String> encodedValues;
fun encodedName(index: Int) = encodedNames[index]

FormBody(List<String> encodedNames, List<String> encodedValues) {
this.encodedNames = Util.immutableList(encodedNames);
this.encodedValues = Util.immutableList(encodedValues);
}
fun name(index: Int) = percentDecode(encodedName(index), true)

/** The number of key-value pairs in this form-encoded body. */
public int size() {
return encodedNames.size();
}
fun encodedValue(index: Int) = encodedValues[index]

public String encodedName(int index) {
return encodedNames.get(index);
}

public String name(int index) {
return percentDecode(encodedName(index), true);
}

public String encodedValue(int index) {
return encodedValues.get(index);
}

public String value(int index) {
return percentDecode(encodedValue(index), true);
}
fun value(index: Int) = percentDecode(encodedValue(index), true)

@Override public MediaType contentType() {
return CONTENT_TYPE;
}
override fun contentType() = CONTENT_TYPE

@Override public long contentLength() {
return writeOrCountBytes(null, true);
}
override fun contentLength() = writeOrCountBytes(null, true)

@Override public void writeTo(BufferedSink sink) throws IOException {
writeOrCountBytes(sink, false);
@Throws(IOException::class)
override fun writeTo(sink: BufferedSink) {
writeOrCountBytes(sink, false)
}

/**
* Either writes this request to {@code sink} or measures its content length. We have one method
* Either writes this request to `sink` or measures its content length. We have one method
* do double-duty to make sure the counting and content are consistent, particularly when it comes
* to awkward operations like measuring the encoded length of header strings, or the
* length-in-digits of an encoded integer.
*/
private long writeOrCountBytes(@Nullable BufferedSink sink, boolean countBytes) {
long byteCount = 0L;
private fun writeOrCountBytes(sink: BufferedSink?, countBytes: Boolean): Long {
var byteCount = 0L

Buffer buffer;
val buffer: Buffer
swankjesse marked this conversation as resolved.
Show resolved Hide resolved
if (countBytes) {
buffer = new Buffer();
buffer = Buffer()
} else {
buffer = sink.buffer();
buffer = sink!!.buffer()
}

for (int i = 0, size = encodedNames.size(); i < size; i++) {
if (i > 0) buffer.writeByte('&');
buffer.writeUtf8(encodedNames.get(i));
buffer.writeByte('=');
buffer.writeUtf8(encodedValues.get(i));
var i = 0
val size = encodedNames.size
while (i < size) {
if (i > 0) buffer.writeByte('&'.toInt())
buffer.writeUtf8(encodedNames[i])
buffer.writeByte('='.toInt())
buffer.writeUtf8(encodedValues[i])
i++
}

if (countBytes) {
byteCount = buffer.size();
buffer.clear();
byteCount = buffer.size()
buffer.clear()
}

return byteCount;
return byteCount
}

public static final class Builder {
private final List<String> names = new ArrayList<>();
private final List<String> values = new ArrayList<>();
private final @Nullable Charset charset;
class Builder @JvmOverloads constructor(private val charset: Charset? = null) {
private val names = mutableListOf<String>()
private val values = mutableListOf<String>()

public Builder() {
this(null);
fun add(name: String, value: String): Builder {
names.add(HttpUrl.canonicalize(name, FORM_ENCODE_SET, false, false, true, true, charset))
values.add(HttpUrl.canonicalize(value, FORM_ENCODE_SET, false, false, true, true, charset))
return this
}

public Builder(@Nullable Charset charset) {
this.charset = charset;
}

public Builder add(String name, String value) {
if (name == null) throw new NullPointerException("name == null");
if (value == null) throw new NullPointerException("value == null");

names.add(HttpUrl.canonicalize(name, FORM_ENCODE_SET, false, false, true, true, charset));
values.add(HttpUrl.canonicalize(value, FORM_ENCODE_SET, false, false, true, true, charset));
return this;
fun addEncoded(name: String, value: String): Builder {
names.add(HttpUrl.canonicalize(name, FORM_ENCODE_SET, true, false, true, true, charset))
values.add(HttpUrl.canonicalize(value, FORM_ENCODE_SET, true, false, true, true, charset))
return this
}

public Builder addEncoded(String name, String value) {
if (name == null) throw new NullPointerException("name == null");
if (value == null) throw new NullPointerException("value == null");

names.add(HttpUrl.canonicalize(name, FORM_ENCODE_SET, true, false, true, true, charset));
values.add(HttpUrl.canonicalize(value, FORM_ENCODE_SET, true, false, true, true, charset));
return this;
}
fun build(): FormBody = FormBody(names, values)
}

public FormBody build() {
return new FormBody(names, values);
}
companion object {
private val CONTENT_TYPE: MediaType = MediaType.get("application/x-www-form-urlencoded")
}
}
Loading