-
Notifications
You must be signed in to change notification settings - Fork 615
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6dc02a4
commit 6d6b6a2
Showing
14 changed files
with
386 additions
and
1 deletion.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
whatsmars-common/src/test/java/org/hongxi/java/nio/BufferTest.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,40 @@ | ||
package org.hongxi.java.nio; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* ByteBuffer | ||
* @author shenhongxi 2019/8/15 | ||
*/ | ||
public class BufferTest { | ||
|
||
public static void main(String[] args) { | ||
/** | ||
* byte[] array = new byte[1024]; | ||
* ByteBuffer buffer = ByteBuffer.wrap(array); | ||
*/ | ||
ByteBuffer buffer = ByteBuffer.allocate(1024); | ||
|
||
buffer.put((byte) 'a'); | ||
buffer.put((byte) 'b'); | ||
buffer.put((byte) 'c'); | ||
|
||
buffer.flip(); | ||
|
||
System.out.println((char) buffer.get()); | ||
System.out.println((char) buffer.get()); | ||
System.out.println((char) buffer.get()); | ||
|
||
buffer.clear(); | ||
|
||
buffer.putInt(30); | ||
buffer.putLong(7000000000000L); | ||
buffer.putDouble(Math.PI); | ||
|
||
buffer.flip(); | ||
|
||
System.out.println(buffer.getInt()); | ||
System.out.println(buffer.getLong()); | ||
System.out.println(buffer.getDouble()); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
whatsmars-common/src/test/java/org/hongxi/java/nio/CharsetsTest.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,48 @@ | ||
package org.hongxi.java.nio; | ||
|
||
import java.io.File; | ||
import java.io.RandomAccessFile; | ||
import java.nio.ByteBuffer; | ||
import java.nio.CharBuffer; | ||
import java.nio.MappedByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.CharsetDecoder; | ||
import java.nio.charset.CharsetEncoder; | ||
|
||
/** | ||
* Charset | ||
* @author shenhongxi 2019/8/15 | ||
*/ | ||
public class CharsetsTest { | ||
|
||
public static void main(String[] args) throws Exception { | ||
String inputFile = "samplein.txt"; | ||
String outputFile = "sampleout.txt"; | ||
|
||
RandomAccessFile inf = new RandomAccessFile(inputFile, "r"); | ||
RandomAccessFile outf = new RandomAccessFile(outputFile, "rw"); | ||
long inputLength = new File(inputFile).length(); | ||
|
||
FileChannel inc = inf.getChannel(); | ||
FileChannel outc = outf.getChannel(); | ||
|
||
MappedByteBuffer inputData = | ||
inc.map(FileChannel.MapMode.READ_ONLY, 0, inputLength); | ||
|
||
Charset latin1 = Charset.forName("ISO-8859-1"); | ||
CharsetDecoder decoder = latin1.newDecoder(); | ||
CharsetEncoder encoder = latin1.newEncoder(); | ||
|
||
CharBuffer cb = decoder.decode(inputData); | ||
|
||
// Process char data here | ||
|
||
ByteBuffer outputData = encoder.encode(cb); | ||
|
||
outc.write(outputData); | ||
|
||
inf.close(); | ||
outf.close(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
whatsmars-common/src/test/java/org/hongxi/java/nio/CopyFileTest.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,45 @@ | ||
package org.hongxi.java.nio; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
|
||
/** | ||
* FileChannel | ||
* @author shenhongxi 2019/8/15 | ||
*/ | ||
public class CopyFileTest { | ||
|
||
public static void main(String[] args) throws Exception { | ||
if (args.length < 2) { | ||
System.err.println("Usage: java CopyFile infile outfile"); | ||
System.exit(1); | ||
} | ||
|
||
String infile = args[0]; | ||
String outfile = args[1]; | ||
|
||
FileInputStream fin = new FileInputStream(infile); | ||
FileOutputStream fout = new FileOutputStream(outfile); | ||
|
||
FileChannel fcin = fin.getChannel(); | ||
FileChannel fcout = fout.getChannel(); | ||
|
||
/** | ||
* faster: | ||
* ByteBuffer buffer = ByteBuffer.allocateDirect(1024); | ||
*/ | ||
ByteBuffer buffer = ByteBuffer.allocate(1024); | ||
|
||
while (true) { | ||
buffer.clear(); | ||
int r = fcin.read(buffer); | ||
if (r == -1) { | ||
break; | ||
} | ||
buffer.flip(); | ||
fcout.write(buffer); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
whatsmars-common/src/test/java/org/hongxi/java/nio/FileLockTest.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,39 @@ | ||
package org.hongxi.java.nio; | ||
|
||
import java.io.RandomAccessFile; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.channels.FileLock; | ||
|
||
/** | ||
* FileLock | ||
* @author shenhongxi 2019/8/15 | ||
*/ | ||
public class FileLockTest { | ||
private static final int start = 10; | ||
private static final int end = 20; | ||
|
||
public static void main(String[] args) throws Exception { | ||
// Get file channel | ||
RandomAccessFile raf = new RandomAccessFile("usefilelocks.txt", "rw"); | ||
FileChannel fc = raf.getChannel(); | ||
|
||
// Get lock | ||
System.out.println("trying to get lock"); | ||
FileLock lock = fc.lock(start, end, false); | ||
System.out.println("got lock!"); | ||
|
||
// Pause | ||
System.out.println("pausing"); | ||
try { | ||
Thread.sleep(3000); | ||
} catch (InterruptedException ie) { | ||
} | ||
|
||
// Release lock | ||
System.out.println("going to release lock"); | ||
lock.release(); | ||
System.out.println("released lock"); | ||
|
||
raf.close(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
whatsmars-common/src/test/java/org/hongxi/java/nio/FloatBufferTest.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,26 @@ | ||
package org.hongxi.java.nio; | ||
|
||
import java.nio.FloatBuffer; | ||
|
||
/** | ||
* FloatBuffer | ||
* @author shenhongxi 2019/8/15 | ||
*/ | ||
public class FloatBufferTest { | ||
|
||
public static void main(String[] args) { | ||
FloatBuffer buffer = FloatBuffer.allocate(10); | ||
|
||
for (int i = 0; i < buffer.capacity(); ++i) { | ||
float f = (float) Math.sin((((float) i) / 10) * (2 * Math.PI)); | ||
buffer.put(f); | ||
} | ||
|
||
buffer.flip(); | ||
|
||
while (buffer.hasRemaining()) { | ||
float f = buffer.get(); | ||
System.out.println(f); | ||
} | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
whatsmars-common/src/test/java/org/hongxi/java/nio/ReadFileTest.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,30 @@ | ||
package org.hongxi.java.nio; | ||
|
||
import java.io.FileInputStream; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
|
||
/** | ||
* FileChannel | ||
* @author shenhongxi 2019/8/15 | ||
*/ | ||
public class ReadFileTest { | ||
|
||
public static void main(String[] args) throws Exception { | ||
FileInputStream fin = new FileInputStream("readandshow.txt"); | ||
FileChannel fc = fin.getChannel(); | ||
|
||
ByteBuffer buffer = ByteBuffer.allocate(1024); | ||
fc.read(buffer); | ||
buffer.flip(); | ||
|
||
int i = 0; | ||
while (buffer.remaining() > 0) { | ||
byte b = buffer.get(); | ||
System.out.println("Character " + i + ": " + ((char) b)); | ||
i++; | ||
} | ||
|
||
fin.close(); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
whatsmars-common/src/test/java/org/hongxi/java/nio/ScatterGatherTest.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,77 @@ | ||
package org.hongxi.java.nio; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.ServerSocketChannel; | ||
import java.nio.channels.SocketChannel; | ||
|
||
/** | ||
* @author shenhongxi 2019/8/15 | ||
*/ | ||
public class ScatterGatherTest { | ||
private static final int firstHeaderLength = 2; | ||
private static final int secondHeaderLength = 4; | ||
private static final int bodyLength = 6; | ||
|
||
public static void main(String[] args) throws Exception { | ||
if (args.length != 1) { | ||
System.err.println("Usage: java UseScatterGather port"); | ||
System.exit(1); | ||
} | ||
|
||
int port = Integer.parseInt(args[0]); | ||
|
||
ServerSocketChannel ssc = ServerSocketChannel.open(); | ||
InetSocketAddress address = new InetSocketAddress(port); | ||
ssc.socket().bind(address); | ||
|
||
int messageLength = | ||
firstHeaderLength + secondHeaderLength + bodyLength; | ||
|
||
ByteBuffer[] buffers = new ByteBuffer[3]; | ||
buffers[0] = ByteBuffer.allocate(firstHeaderLength); | ||
buffers[1] = ByteBuffer.allocate(secondHeaderLength); | ||
buffers[2] = ByteBuffer.allocate(bodyLength); | ||
|
||
SocketChannel sc = ssc.accept(); | ||
|
||
while (true) { | ||
|
||
// Scatter-read into buffers | ||
int bytesRead = 0; | ||
while (bytesRead < messageLength) { | ||
long r = sc.read(buffers); | ||
bytesRead += r; | ||
|
||
System.out.println("r " + r); | ||
for (int i = 0; i < buffers.length; ++i) { | ||
ByteBuffer bb = buffers[i]; | ||
System.out.println("b " + i + " " + bb.position() + " " + bb.limit()); | ||
} | ||
} | ||
|
||
// Process message here | ||
|
||
// Flip buffers | ||
for (int i = 0; i < buffers.length; ++i) { | ||
ByteBuffer bb = buffers[i]; | ||
bb.flip(); | ||
} | ||
|
||
// Scatter-write back out | ||
long bytesWritten = 0; | ||
while (bytesWritten < messageLength) { | ||
long r = sc.write(buffers); | ||
bytesWritten += r; | ||
} | ||
|
||
// Clear buffers | ||
for (int i = 0; i < buffers.length; ++i) { | ||
ByteBuffer bb = buffers[i]; | ||
bb.clear(); | ||
} | ||
|
||
System.out.println(bytesRead + " " + bytesWritten + " " + messageLength); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
whatsmars-common/src/test/java/org/hongxi/java/nio/SliceBufferTest.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,36 @@ | ||
package org.hongxi.java.nio; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* ByteBuffer | ||
* @author shenhongxi 2019/8/15 | ||
*/ | ||
public class SliceBufferTest { | ||
|
||
public static void main(String[] args) { | ||
ByteBuffer buffer = ByteBuffer.allocate(10); | ||
|
||
for (int i = 0; i < buffer.capacity(); ++i) { | ||
buffer.put((byte) i); | ||
} | ||
|
||
buffer.position(3); | ||
buffer.limit(7); | ||
|
||
ByteBuffer slice = buffer.slice(); | ||
|
||
for (int i = 0; i < slice.capacity(); ++i) { | ||
byte b = slice.get(i); | ||
b *= 11; | ||
slice.put(i, b); | ||
} | ||
|
||
buffer.position(0); | ||
buffer.limit(buffer.capacity()); | ||
|
||
while (buffer.remaining() > 0) { | ||
System.out.println(buffer.get()); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
whatsmars-common/src/test/java/org/hongxi/java/nio/WriteFileTest.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,32 @@ | ||
package org.hongxi.java.nio; | ||
|
||
import java.io.FileOutputStream; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
|
||
/** | ||
* FileChannel | ||
* @author shenhongxi 2019/8/15 | ||
*/ | ||
public class WriteFileTest { | ||
private static final byte message[] = {83, 111, 109, 101, 32, | ||
98, 121, 116, 101, 115, 46}; | ||
|
||
public static void main(String[] args) throws Exception { | ||
FileOutputStream fout = new FileOutputStream("/Users/javahongxi/writesomebytes.txt"); | ||
|
||
FileChannel fc = fout.getChannel(); | ||
|
||
ByteBuffer buffer = ByteBuffer.allocate(1024); | ||
|
||
for (int i = 0; i < message.length; ++i) { | ||
buffer.put(message[i]); | ||
} | ||
|
||
buffer.flip(); | ||
|
||
fc.write(buffer); | ||
|
||
fout.close(); | ||
} | ||
} |
Oops, something went wrong.