Skip to content

Commit e839f94

Browse files
cgdeckernick-someone
authored andcommitted
Revert a1e9a0b
Causing some internal failures ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=316509747
1 parent a1e9a0b commit e839f94

File tree

6 files changed

+24
-202
lines changed

6 files changed

+24
-202
lines changed

android/guava-tests/test/com/google/common/io/ByteStreamsTest.java

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.io.ByteArrayOutputStream;
2424
import java.io.EOFException;
2525
import java.io.File;
26-
import java.io.FileInputStream;
2726
import java.io.FileOutputStream;
2827
import java.io.FilterInputStream;
2928
import java.io.IOException;
@@ -42,7 +41,7 @@
4241
*/
4342
public class ByteStreamsTest extends IoTestCase {
4443

45-
public void testCopy_channel() throws IOException {
44+
public void testCopyChannel() throws IOException {
4645
byte[] expected = newPreFilledByteArray(100);
4746
ByteArrayOutputStream out = new ByteArrayOutputStream();
4847
WritableByteChannel outChannel = Channels.newChannel(out);
@@ -52,7 +51,7 @@ public void testCopy_channel() throws IOException {
5251
assertThat(out.toByteArray()).isEqualTo(expected);
5352
}
5453

55-
public void testCopy_channel_fromFile() throws IOException {
54+
public void testCopyFileChannel() throws IOException {
5655
final int chunkSize = 14407; // Random prime, unlikely to match any internal chunk size
5756
ByteArrayOutputStream out = new ByteArrayOutputStream();
5857
WritableByteChannel outChannel = Channels.newChannel(out);
@@ -73,68 +72,6 @@ public void testCopy_channel_fromFile() throws IOException {
7372
}
7473
}
7574

76-
public void testCopy_stream() throws IOException {
77-
byte[] expected = newPreFilledByteArray(100);
78-
ByteArrayOutputStream out = new ByteArrayOutputStream();
79-
80-
ByteStreams.copy(new ByteArrayInputStream(expected), out);
81-
82-
assertThat(out.toByteArray()).isEqualTo(expected);
83-
}
84-
85-
public void testCopy_stream_files_emptyDestination() throws IOException {
86-
byte[] expected = new byte[] {0, 1, 2};
87-
File inputFile = createTempFile(expected);
88-
File outputFile = createTempFile();
89-
90-
try (FileInputStream inputStream = new FileInputStream(inputFile);
91-
FileOutputStream outputStream = new FileOutputStream(outputFile)) {
92-
ByteStreams.copy(inputStream, outputStream);
93-
}
94-
95-
assertThat(Files.asByteSource(outputFile).read()).isEqualTo(expected);
96-
}
97-
98-
public void testCopy_stream_files_appendDestination() throws IOException {
99-
File inputFile = createTempFile(new byte[] {3, 4, 5});
100-
File outputFile = createTempFile(new byte[] {0, 1, 2});
101-
102-
try (FileInputStream inputStream = new FileInputStream(inputFile);
103-
FileOutputStream outputStream = new FileOutputStream(outputFile, /* append= */ true)) {
104-
ByteStreams.copy(inputStream, outputStream);
105-
}
106-
107-
assertThat(Files.asByteSource(outputFile).read()).isEqualTo(new byte[] {0, 1, 2, 3, 4, 5});
108-
}
109-
110-
public void testCopy_stream_files_additionalWrites_emptyDestination() throws IOException {
111-
File inputFile = createTempFile(new byte[] {0, 1, 2});
112-
File outputFile = createTempFile();
113-
114-
try (FileInputStream inputStream = new FileInputStream(inputFile);
115-
FileOutputStream outputStream = new FileOutputStream(outputFile)) {
116-
outputStream.write(new byte[] {0, 0});
117-
ByteStreams.copy(inputStream, outputStream);
118-
outputStream.write(new byte[] {2, 2});
119-
}
120-
121-
assertThat(Files.asByteSource(outputFile).read()).isEqualTo(new byte[] {0, 0, 0, 1, 2, 2, 2});
122-
}
123-
124-
public void testCopy_stream_files_additionalWrites_appendDestination() throws IOException {
125-
File inputFile = createTempFile(new byte[] {0, 1, 2});
126-
File outputFile = createTempFile(new byte[] {0});
127-
128-
try (FileInputStream inputStream = new FileInputStream(inputFile);
129-
FileOutputStream outputStream = new FileOutputStream(outputFile, /* append= */ true)) {
130-
outputStream.write(new byte[] {0});
131-
ByteStreams.copy(inputStream, outputStream);
132-
outputStream.write(new byte[] {2, 2});
133-
}
134-
135-
assertThat(Files.asByteSource(outputFile).read()).isEqualTo(new byte[] {0, 0, 0, 1, 2, 2, 2});
136-
}
137-
13875
public void testReadFully() throws IOException {
13976
byte[] b = new byte[10];
14077

android/guava-tests/test/com/google/common/io/IoTestCase.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,6 @@ protected final File createTempFile() throws IOException {
139139
return File.createTempFile("test", null, getTempDir());
140140
}
141141

142-
/**
143-
* Creates a new temp file in the temp directory returned by {@link #getTempDir()}. The file will
144-
* be deleted in the tear-down for this test.
145-
*
146-
* @param content which should be written to the file
147-
*/
148-
protected final File createTempFile(byte[] content) throws IOException {
149-
File file = File.createTempFile("test", null, getTempDir());
150-
Files.write(content, file);
151-
return file;
152-
}
153-
154142
/** Returns a byte array of length size that has values 0 .. size - 1. */
155143
static byte[] newPreFilledByteArray(int size) {
156144
return newPreFilledByteArray(0, size);

android/guava/src/com/google/common/io/ByteStreams.java

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import java.io.DataOutput;
3131
import java.io.DataOutputStream;
3232
import java.io.EOFException;
33-
import java.io.FileInputStream;
34-
import java.io.FileOutputStream;
3533
import java.io.FilterInputStream;
3634
import java.io.IOException;
3735
import java.io.InputStream;
@@ -105,15 +103,6 @@ private ByteStreams() {}
105103
public static long copy(InputStream from, OutputStream to) throws IOException {
106104
checkNotNull(from);
107105
checkNotNull(to);
108-
109-
// Use java.nio.channels in case we're copying from file to file.
110-
// Copying through channels happens ideally in the kernel space and therefore faster.
111-
if (from instanceof FileInputStream && to instanceof FileOutputStream) {
112-
FileChannel fromChannel = ((FileInputStream) from).getChannel();
113-
FileChannel toChannel = ((FileOutputStream) to).getChannel();
114-
return copyFileChannel(fromChannel, toChannel);
115-
}
116-
117106
byte[] buf = createBuffer();
118107
long total = 0;
119108
while (true) {
@@ -141,7 +130,16 @@ public static long copy(ReadableByteChannel from, WritableByteChannel to) throws
141130
checkNotNull(from);
142131
checkNotNull(to);
143132
if (from instanceof FileChannel) {
144-
return copyFileChannel((FileChannel) from, to);
133+
FileChannel sourceChannel = (FileChannel) from;
134+
long oldPosition = sourceChannel.position();
135+
long position = oldPosition;
136+
long copied;
137+
do {
138+
copied = sourceChannel.transferTo(position, ZERO_COPY_CHUNK_SIZE, to);
139+
position += copied;
140+
sourceChannel.position(position);
141+
} while (copied > 0 || position < sourceChannel.size());
142+
return position - oldPosition;
145143
}
146144

147145
ByteBuffer buf = ByteBuffer.wrap(createBuffer());
@@ -156,18 +154,6 @@ public static long copy(ReadableByteChannel from, WritableByteChannel to) throws
156154
return total;
157155
}
158156

159-
private static long copyFileChannel(FileChannel from, WritableByteChannel to) throws IOException {
160-
long oldPosition = from.position();
161-
long position = oldPosition;
162-
long copied;
163-
do {
164-
copied = from.transferTo(position, ZERO_COPY_CHUNK_SIZE, to);
165-
position += copied;
166-
from.position(position);
167-
} while (copied > 0 || position < from.size());
168-
return position - oldPosition;
169-
}
170-
171157
/** Max array length on JVM. */
172158
private static final int MAX_ARRAY_LEN = Integer.MAX_VALUE - 8;
173159

guava-tests/test/com/google/common/io/ByteStreamsTest.java

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.io.ByteArrayOutputStream;
2424
import java.io.EOFException;
2525
import java.io.File;
26-
import java.io.FileInputStream;
2726
import java.io.FileOutputStream;
2827
import java.io.FilterInputStream;
2928
import java.io.IOException;
@@ -42,7 +41,7 @@
4241
*/
4342
public class ByteStreamsTest extends IoTestCase {
4443

45-
public void testCopy_channel() throws IOException {
44+
public void testCopyChannel() throws IOException {
4645
byte[] expected = newPreFilledByteArray(100);
4746
ByteArrayOutputStream out = new ByteArrayOutputStream();
4847
WritableByteChannel outChannel = Channels.newChannel(out);
@@ -52,7 +51,7 @@ public void testCopy_channel() throws IOException {
5251
assertThat(out.toByteArray()).isEqualTo(expected);
5352
}
5453

55-
public void testCopy_channel_fromFile() throws IOException {
54+
public void testCopyFileChannel() throws IOException {
5655
final int chunkSize = 14407; // Random prime, unlikely to match any internal chunk size
5756
ByteArrayOutputStream out = new ByteArrayOutputStream();
5857
WritableByteChannel outChannel = Channels.newChannel(out);
@@ -73,68 +72,6 @@ public void testCopy_channel_fromFile() throws IOException {
7372
}
7473
}
7574

76-
public void testCopy_stream() throws IOException {
77-
byte[] expected = newPreFilledByteArray(100);
78-
ByteArrayOutputStream out = new ByteArrayOutputStream();
79-
80-
ByteStreams.copy(new ByteArrayInputStream(expected), out);
81-
82-
assertThat(out.toByteArray()).isEqualTo(expected);
83-
}
84-
85-
public void testCopy_stream_files_emptyDestination() throws IOException {
86-
byte[] expected = new byte[] {0, 1, 2};
87-
File inputFile = createTempFile(expected);
88-
File outputFile = createTempFile();
89-
90-
try (FileInputStream inputStream = new FileInputStream(inputFile);
91-
FileOutputStream outputStream = new FileOutputStream(outputFile)) {
92-
ByteStreams.copy(inputStream, outputStream);
93-
}
94-
95-
assertThat(Files.asByteSource(outputFile).read()).isEqualTo(expected);
96-
}
97-
98-
public void testCopy_stream_files_appendDestination() throws IOException {
99-
File inputFile = createTempFile(new byte[] {3, 4, 5});
100-
File outputFile = createTempFile(new byte[] {0, 1, 2});
101-
102-
try (FileInputStream inputStream = new FileInputStream(inputFile);
103-
FileOutputStream outputStream = new FileOutputStream(outputFile, /* append= */ true)) {
104-
ByteStreams.copy(inputStream, outputStream);
105-
}
106-
107-
assertThat(Files.asByteSource(outputFile).read()).isEqualTo(new byte[] {0, 1, 2, 3, 4, 5});
108-
}
109-
110-
public void testCopy_stream_files_additionalWrites_emptyDestination() throws IOException {
111-
File inputFile = createTempFile(new byte[] {0, 1, 2});
112-
File outputFile = createTempFile();
113-
114-
try (FileInputStream inputStream = new FileInputStream(inputFile);
115-
FileOutputStream outputStream = new FileOutputStream(outputFile)) {
116-
outputStream.write(new byte[] {0, 0});
117-
ByteStreams.copy(inputStream, outputStream);
118-
outputStream.write(new byte[] {2, 2});
119-
}
120-
121-
assertThat(Files.asByteSource(outputFile).read()).isEqualTo(new byte[] {0, 0, 0, 1, 2, 2, 2});
122-
}
123-
124-
public void testCopy_stream_files_additionalWrites_appendDestination() throws IOException {
125-
File inputFile = createTempFile(new byte[] {0, 1, 2});
126-
File outputFile = createTempFile(new byte[] {0});
127-
128-
try (FileInputStream inputStream = new FileInputStream(inputFile);
129-
FileOutputStream outputStream = new FileOutputStream(outputFile, /* append= */ true)) {
130-
outputStream.write(new byte[] {0});
131-
ByteStreams.copy(inputStream, outputStream);
132-
outputStream.write(new byte[] {2, 2});
133-
}
134-
135-
assertThat(Files.asByteSource(outputFile).read()).isEqualTo(new byte[] {0, 0, 0, 1, 2, 2, 2});
136-
}
137-
13875
public void testReadFully() throws IOException {
13976
byte[] b = new byte[10];
14077

guava-tests/test/com/google/common/io/IoTestCase.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,6 @@ protected final File createTempFile() throws IOException {
139139
return File.createTempFile("test", null, getTempDir());
140140
}
141141

142-
/**
143-
* Creates a new temp file in the temp directory returned by {@link #getTempDir()}. The file will
144-
* be deleted in the tear-down for this test.
145-
*
146-
* @param content which should be written to the file
147-
*/
148-
protected final File createTempFile(byte[] content) throws IOException {
149-
File file = File.createTempFile("test", null, getTempDir());
150-
Files.write(content, file);
151-
return file;
152-
}
153-
154142
/** Returns a byte array of length size that has values 0 .. size - 1. */
155143
static byte[] newPreFilledByteArray(int size) {
156144
return newPreFilledByteArray(0, size);

guava/src/com/google/common/io/ByteStreams.java

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import java.io.DataOutput;
3131
import java.io.DataOutputStream;
3232
import java.io.EOFException;
33-
import java.io.FileInputStream;
34-
import java.io.FileOutputStream;
3533
import java.io.FilterInputStream;
3634
import java.io.IOException;
3735
import java.io.InputStream;
@@ -105,15 +103,6 @@ private ByteStreams() {}
105103
public static long copy(InputStream from, OutputStream to) throws IOException {
106104
checkNotNull(from);
107105
checkNotNull(to);
108-
109-
// Use java.nio.channels in case we're copying from file to file.
110-
// Copying through channels happens ideally in the kernel space and therefore faster.
111-
if (from instanceof FileInputStream && to instanceof FileOutputStream) {
112-
FileChannel fromChannel = ((FileInputStream) from).getChannel();
113-
FileChannel toChannel = ((FileOutputStream) to).getChannel();
114-
return copyFileChannel(fromChannel, toChannel);
115-
}
116-
117106
byte[] buf = createBuffer();
118107
long total = 0;
119108
while (true) {
@@ -141,7 +130,16 @@ public static long copy(ReadableByteChannel from, WritableByteChannel to) throws
141130
checkNotNull(from);
142131
checkNotNull(to);
143132
if (from instanceof FileChannel) {
144-
return copyFileChannel((FileChannel) from, to);
133+
FileChannel sourceChannel = (FileChannel) from;
134+
long oldPosition = sourceChannel.position();
135+
long position = oldPosition;
136+
long copied;
137+
do {
138+
copied = sourceChannel.transferTo(position, ZERO_COPY_CHUNK_SIZE, to);
139+
position += copied;
140+
sourceChannel.position(position);
141+
} while (copied > 0 || position < sourceChannel.size());
142+
return position - oldPosition;
145143
}
146144

147145
ByteBuffer buf = ByteBuffer.wrap(createBuffer());
@@ -156,18 +154,6 @@ public static long copy(ReadableByteChannel from, WritableByteChannel to) throws
156154
return total;
157155
}
158156

159-
private static long copyFileChannel(FileChannel from, WritableByteChannel to) throws IOException {
160-
long oldPosition = from.position();
161-
long position = oldPosition;
162-
long copied;
163-
do {
164-
copied = from.transferTo(position, ZERO_COPY_CHUNK_SIZE, to);
165-
position += copied;
166-
from.position(position);
167-
} while (copied > 0 || position < from.size());
168-
return position - oldPosition;
169-
}
170-
171157
/** Max array length on JVM. */
172158
private static final int MAX_ARRAY_LEN = Integer.MAX_VALUE - 8;
173159

0 commit comments

Comments
 (0)