Factor creation of RawInput instances out to RawInputUtil

There are several places where a new RawInput is created. Factor these
out to static methods in a new RawInputUtil class in gerrit-common.

Inspired-by: David Ostrovsky <david@ostrovsky.org>
Change-Id: I10b099e269427b0f887bfb41bbb3f10f46cf2620
This commit is contained in:
David Pursehouse
2016-03-17 14:26:38 +09:00
parent 30938ca111
commit 998cacb548
8 changed files with 113 additions and 94 deletions

View File

@@ -28,9 +28,7 @@ import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class RestSession extends HttpSession {
@@ -113,30 +111,4 @@ public class RestSession extends HttpSession {
public RestResponse delete(String endPoint) throws IOException {
return execute(Request.Delete(url + "/a" + endPoint));
}
public static RawInput newRawInput(String content) {
return newRawInput(content.getBytes(UTF_8));
}
public static RawInput newRawInput(final byte[] bytes) {
Preconditions.checkNotNull(bytes);
Preconditions.checkArgument(bytes.length > 0);
return new RawInput() {
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(bytes);
}
@Override
public String getContentType() {
return "application/octet-stream";
}
@Override
public long getContentLength() {
return bytes.length;
}
};
}
}