-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate to java InteroperabilityUtils
- Loading branch information
1 parent
ae1bb49
commit ca4522f
Showing
2 changed files
with
125 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/test/scala/tlschannel/util/InteroperabilityUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package tlschannel.util; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.Socket; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.ByteChannel; | ||
import javax.net.ssl.SSLSocket; | ||
import org.junit.jupiter.api.Assertions; | ||
import tlschannel.TlsChannel; | ||
|
||
public class InteroperabilityUtils { | ||
|
||
public interface Reader { | ||
int read(byte[] array, int offset, int length) throws IOException; | ||
|
||
void close() throws IOException; | ||
} | ||
|
||
public interface Writer { | ||
void renegotiate() throws IOException; | ||
|
||
void write(byte[] array, int offset, int length) throws IOException; | ||
|
||
void close() throws IOException; | ||
} | ||
|
||
public static class SocketReader implements Reader { | ||
|
||
private final Socket socket; | ||
private final InputStream is; | ||
|
||
public SocketReader(Socket socket) throws IOException { | ||
this.socket = socket; | ||
this.is = socket.getInputStream(); | ||
} | ||
|
||
@Override | ||
public int read(byte[] array, int offset, int length) throws IOException { | ||
return is.read(array, offset, length); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
socket.close(); | ||
} | ||
} | ||
|
||
public static class ByteChannelReader implements Reader { | ||
|
||
private final ByteChannel socket; | ||
|
||
public ByteChannelReader(ByteChannel socket) { | ||
this.socket = socket; | ||
} | ||
|
||
@Override | ||
public int read(byte[] array, int offset, int length) throws IOException { | ||
return socket.read(ByteBuffer.wrap(array, offset, length)); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
socket.close(); | ||
} | ||
} | ||
|
||
public static class SSLSocketWriter implements Writer { | ||
|
||
private final SSLSocket socket; | ||
private final OutputStream os; | ||
|
||
public SSLSocketWriter(SSLSocket socket) throws IOException { | ||
this.socket = socket; | ||
this.os = socket.getOutputStream(); | ||
} | ||
|
||
@Override | ||
public void write(byte[] array, int offset, int length) throws IOException { | ||
os.write(array, offset, length); | ||
} | ||
|
||
@Override | ||
public void renegotiate() throws IOException { | ||
socket.startHandshake(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
socket.close(); | ||
} | ||
} | ||
|
||
public static class TlsChannelWriter implements Writer { | ||
|
||
private final TlsChannel socket; | ||
|
||
public TlsChannelWriter(TlsChannel socket) { | ||
this.socket = socket; | ||
} | ||
|
||
@Override | ||
public void write(byte[] array, int offset, int length) throws IOException { | ||
ByteBuffer buffer = ByteBuffer.wrap(array, offset, length); | ||
while (buffer.remaining() > 0) { | ||
int c = socket.write(buffer); | ||
Assertions.assertNotEquals(0, c, "blocking write cannot return 0"); | ||
} | ||
} | ||
|
||
@Override | ||
public void renegotiate() throws IOException { | ||
socket.renegotiate(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
socket.close(); | ||
} | ||
} | ||
} |