Skip to content

Commit

Permalink
nio
Browse files Browse the repository at this point in the history
  • Loading branch information
javahongxi committed Aug 15, 2019
1 parent 6dc02a4 commit 6d6b6a2
Show file tree
Hide file tree
Showing 14 changed files with 386 additions and 1 deletion.
40 changes: 40 additions & 0 deletions whatsmars-common/src/test/java/org/hongxi/java/nio/BufferTest.java
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());
}
}
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();
}
}
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);
}
}
}
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();
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.nio.channels.FileChannel;

/**
* MappedByteBuffer
* Created by javahongxi on 2018/1/2.
*/
public class MappedFileTest {
Expand All @@ -16,7 +17,6 @@ static public void main(String args[]) throws Exception {
FileChannel channel = raf.getChannel();

MappedByteBuffer mbb = channel.map(FileChannel.MapMode.READ_WRITE, start, size);

mbb.put(0, (byte) 97);
mbb.put(1023, (byte) 122);

Expand Down
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();
}
}
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);
}
}
}
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());
}
}
}
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();
}
}
Loading

0 comments on commit 6d6b6a2

Please sign in to comment.