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

allow to write to PathURLConnection #160

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions jimfs/src/main/java/com/google/common/jimfs/PathURLConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -60,6 +61,7 @@ final class PathURLConnection extends URLConnection {
private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";

private InputStream stream;
private OutputStream outStream;
private ImmutableListMultimap<String, String> headers = ImmutableListMultimap.of();

PathURLConnection(URL url) {
Expand Down Expand Up @@ -122,6 +124,16 @@ public InputStream getInputStream() throws IOException {
return stream;
}

@Override
public OutputStream getOutputStream() throws IOException {
if (outStream == null) {
Path path = Paths.get(toUri(url));
Files.createDirectories(path.getParent());
outStream = Files.newOutputStream(path);
}
return outStream;
}

@SuppressWarnings("unchecked") // safe by specification of ListMultimap.asMap()
@Override
public Map<String, List<String>> getHeaderFields() {
Expand Down
29 changes: 29 additions & 0 deletions jimfs/src/test/java/com/google/common/jimfs/UrlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import static com.google.common.base.StandardSystemProperty.LINE_SEPARATOR;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Range;
import com.google.common.io.Resources;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
Expand Down Expand Up @@ -61,6 +63,33 @@ public void readFromUrl() throws IOException {
.isEqualTo("Hello World" + LINE_SEPARATOR.value());
}

@Test
public void writeToUrl() throws IOException {
String content = "Hello World" + LINE_SEPARATOR.value();
URL url = path.toUri().toURL();
URLConnection connection = url.openConnection();
try (OutputStream os = connection.getOutputStream()) {
os.write(content.getBytes(UTF_8));
}
assertThat(Resources.asCharSource(url, UTF_8).read())
.isEqualTo("Hello World" + LINE_SEPARATOR.value());
}

@Test
public void writeToUrlSameOutputStream() throws IOException {
//similar to sockets, expect same OutputStream (see AbstractPlainSocketImpl)
URL url = path.toUri().toURL();
URLConnection connection = url.openConnection();
OutputStream os = connection.getOutputStream();
os.write("ABC".getBytes(UTF_8));
OutputStream os2 = connection.getOutputStream();
os2.write("DEF".getBytes(UTF_8));
os2.close();
assertEquals(os, os2);
assertThat(Resources.asCharSource(url, UTF_8).read())
.isEqualTo("ABCDEF");
}

@Test
public void readDirectoryContents() throws IOException {
Files.createDirectory(path);
Expand Down