aboutsummaryrefslogtreecommitdiff
path: root/okio/src/jvmTest/java/okio/NioTest.java
blob: aec177331754be80b4167631eb37515d76a86428 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (C) 2018 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package okio;

import java.io.File;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.StandardOpenOption;
import kotlin.text.Charsets;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;

/** Test interop between our beloved Okio and java.nio. */
public final class NioTest {
  @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();

  @Test public void sourceIsOpen() throws Exception {
    BufferedSource source = Okio.buffer((Source) new Buffer());
    assertTrue(source.isOpen());
    source.close();
    assertFalse(source.isOpen());
  }

  @Test public void sinkIsOpen() throws Exception {
    BufferedSink sink = Okio.buffer((Sink) new Buffer());
    assertTrue(sink.isOpen());
    sink.close();
    assertFalse(sink.isOpen());
  }

  @Test public void writableChannelNioFile() throws Exception {
    File file = temporaryFolder.newFile();
    FileChannel fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.WRITE);
    testWritableByteChannel(fileChannel);

    BufferedSource emitted = Okio.buffer(Okio.source(file));
    assertEquals("defghijklmnopqrstuvw", emitted.readUtf8());
    emitted.close();
  }

  @Test public void writableChannelBuffer() throws Exception {
    Buffer buffer = new Buffer();
    testWritableByteChannel(buffer);
    assertEquals("defghijklmnopqrstuvw", buffer.readUtf8());
  }

  @Test public void writableChannelBufferedSink() throws Exception {
    Buffer buffer = new Buffer();
    BufferedSink bufferedSink = Okio.buffer((Sink) buffer);
    testWritableByteChannel(bufferedSink);
    assertEquals("defghijklmnopqrstuvw", buffer.readUtf8());
  }

  @Test public void readableChannelNioFile() throws Exception {
    File file = temporaryFolder.newFile();

    BufferedSink initialData = Okio.buffer(Okio.sink(file));
    initialData.writeUtf8("abcdefghijklmnopqrstuvwxyz");
    initialData.close();

    FileChannel fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ);
    testReadableByteChannel(fileChannel);
  }

  @Test public void readableChannelBuffer() throws Exception {
    Buffer buffer = new Buffer();
    buffer.writeUtf8("abcdefghijklmnopqrstuvwxyz");

    testReadableByteChannel(buffer);
  }

  @Test public void readableChannelBufferedSource() throws Exception {
    Buffer buffer = new Buffer();
    BufferedSource bufferedSource = Okio.buffer((Source) buffer);
    buffer.writeUtf8("abcdefghijklmnopqrstuvwxyz");

    testReadableByteChannel(bufferedSource);
  }

  /**
   * Does some basic writes to {@code channel}. We execute this against both Okio's channels and
   * also a standard implementation from the JDK to confirm that their behavior is consistent.
   */
  private void testWritableByteChannel(WritableByteChannel channel) throws Exception {
    assertTrue(channel.isOpen());

    ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
    byteBuffer.put("abcdefghijklmnopqrstuvwxyz".getBytes(Charsets.UTF_8));
    byteBuffer.flip();
    byteBuffer.position(3);
    byteBuffer.limit(23);

    int byteCount = channel.write(byteBuffer);
    assertEquals(20, byteCount);
    assertEquals(23, byteBuffer.position());
    assertEquals(23, byteBuffer.limit());

    channel.close();
    assertEquals(channel instanceof Buffer, channel.isOpen()); // Buffer.close() does nothing.
  }

  /**
   * Does some basic reads from {@code channel}. We execute this against both Okio's channels and
   * also a standard implementation from the JDK to confirm that their behavior is consistent.
   */
  private void testReadableByteChannel(ReadableByteChannel channel) throws Exception {
    assertTrue(channel.isOpen());

    ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
    byteBuffer.position(3);
    byteBuffer.limit(23);

    int byteCount = channel.read(byteBuffer);
    assertEquals(20, byteCount);
    assertEquals(23, byteBuffer.position());
    assertEquals(23, byteBuffer.limit());

    channel.close();
    assertEquals(channel instanceof Buffer, channel.isOpen()); // Buffer.close() does nothing.

    byteBuffer.flip();
    byteBuffer.position(3);
    byte[] data = new byte[byteBuffer.remaining()];
    byteBuffer.get(data);
    assertEquals("abcdefghijklmnopqrst", new String(data, Charsets.UTF_8));
  }
}