14
14
import java .util .Map ;
15
15
16
16
/**
17
- * The base class of a http response we receive from parse server. It can be implemented by
18
- * different http library such as Apache http, Android URLConnection, Square OKHttp and so on .
17
+ * The http response we receive from parse server. Instances of this class are not immutable. The
18
+ * response body may be consumed only once. The other fields are immutable .
19
19
*/
20
20
/** package */ class ParseHttpResponse {
21
21
22
+ /**
23
+ * Base builder for {@code ParseHttpResponse}.
24
+ */
22
25
/* package */ static abstract class Init <T extends Init <T >> {
23
26
private int statusCode ;
24
27
private InputStream content ;
30
33
/* package */ abstract T self ();
31
34
32
35
public Init () {
36
+ this .totalSize = -1 ;
33
37
this .headers = new HashMap <>();
34
38
}
35
39
@@ -64,7 +68,7 @@ public T addHeaders(Map<String, String> headers) {
64
68
}
65
69
66
70
public T addHeader (String key , String value ) {
67
- this . headers .put (key , value );
71
+ headers .put (key , value );
68
72
return self ();
69
73
}
70
74
@@ -74,18 +78,28 @@ public T setContentType(String contentType) {
74
78
}
75
79
}
76
80
81
+ /**
82
+ * Builder of {@code ParseHttpResponse}.
83
+ */
77
84
public static class Builder extends Init <Builder > {
78
85
79
86
@ Override
80
87
/* package */ Builder self () {
81
88
return this ;
82
89
}
83
90
84
- /* package */ Builder () {
91
+ public Builder () {
85
92
super ();
86
93
}
87
94
88
- /* package */ Builder (ParseHttpResponse response ) {
95
+ /**
96
+ * Makes a new {@code ParseHttpResponse} {@code Builder} based on the input
97
+ * {@code ParseHttpResponse}.
98
+ *
99
+ * @param response
100
+ * The {@code ParseHttpResponse} where the {@code Builder}'s values come from.
101
+ */
102
+ public Builder (ParseHttpResponse response ) {
89
103
super ();
90
104
this .setStatusCode (response .getStatusCode ());
91
105
this .setContent (response .getContent ());
@@ -100,12 +114,12 @@ public ParseHttpResponse build() {
100
114
}
101
115
}
102
116
103
- /* package */ final int statusCode ;
104
- /* package */ final InputStream content ;
105
- /* package */ final long totalSize ;
106
- /* package */ final String reasonPhrase ;
107
- /* package */ final Map <String , String > headers ;
108
- /* package */ final String contentType ;
117
+ private final int statusCode ;
118
+ private final InputStream content ;
119
+ private final long totalSize ;
120
+ private final String reasonPhrase ;
121
+ private final Map <String , String > headers ;
122
+ private final String contentType ;
109
123
110
124
/* package */ ParseHttpResponse (Init <?> builder ) {
111
125
this .statusCode = builder .statusCode ;
@@ -116,18 +130,26 @@ public ParseHttpResponse build() {
116
130
this .contentType = builder .contentType ;
117
131
}
118
132
119
- public Builder newBuilder () {
120
- return new Builder (this );
121
- }
122
-
123
133
public int getStatusCode () {
124
134
return statusCode ;
125
135
}
126
136
137
+ /**
138
+ * Returns the content of the {@code ParseHttpResponse}'s body. The {@link InputStream} can only
139
+ * be read once and can't be reset.
140
+ *
141
+ * @return The {@link InputStream} of the {@code ParseHttpResponse}'s body.
142
+ */
127
143
public InputStream getContent () {
128
144
return content ;
129
145
}
130
146
147
+ /**
148
+ * Returns the size of the {@code ParseHttpResponse}'s body. -1 if the size of the
149
+ * {@code ParseHttpResponse}'s body is unknown.
150
+ *
151
+ * @return The size of the {@code ParseHttpResponse}'s body.
152
+ */
131
153
public long getTotalSize () {
132
154
return totalSize ;
133
155
}
@@ -141,7 +163,7 @@ public String getContentType() {
141
163
}
142
164
143
165
public String getHeader (String name ) {
144
- return headers == null ? null : headers .get (name );
166
+ return headers .get (name );
145
167
}
146
168
147
169
public Map <String , String > getAllHeaders () {
0 commit comments