-
-
Save seyedsahil/d55c89dae08839279a1f6034595275f2 to your computer and use it in GitHub Desktop.
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
package org.sydlabz; | |
import java.io.IOException; | |
import java.nio.Buffer; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.SeekableByteChannel; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
/** | |
* @author Seyed Sahil | |
*/ | |
public class Main { | |
public static void main(String[] args) throws IOException { | |
Path otpFilePath = Paths.get("otp.txt"); | |
// Allocating a byte buffer of capacity 12, by default capacity and limit will be same. | |
ByteBuffer otpBuffer = ByteBuffer.allocate(12); | |
printBuffer("After allocation", otpBuffer); | |
try (SeekableByteChannel otpFileChannel = Files.newByteChannel(otpFilePath)) { | |
System.out.println("Stage 1"); | |
// Reading data from file to buffer. This will change the position of buffer. | |
int numberOfBytes = otpFileChannel.read(otpBuffer); | |
System.out.println("Number of bytes read from file is '" + numberOfBytes + "'"); | |
printBuffer("After loading data", otpBuffer); | |
// Rewind is required to read data from buffer. This will reset the position back to zero. | |
otpBuffer.rewind(); | |
printBuffer("After rewind", otpBuffer); | |
// Reading 6 digits from the buffer for generating the OTP. This will change the position of buffer. | |
byte[] nextOtp = new byte[otpBuffer.capacity() / 2]; | |
otpBuffer.get(nextOtp); | |
System.out.println("New OTP '" + new String(nextOtp) + "' created."); | |
printBuffer("After reading OTP", otpBuffer); | |
System.out.println("Stage 2"); | |
// Flip will change the limit of buffer to position of buffer and set position of buffer back to zero. | |
otpBuffer.flip(); | |
printBuffer("After flip", otpBuffer); | |
otpBuffer.get(nextOtp); | |
System.out.println("New OTP '" + new String(nextOtp) + "' created."); | |
printBuffer("After reading OTP", otpBuffer); | |
// The following three lines of code wll throw exception, because we are trying to read beyond the limits. | |
// otpBuffer.get(nextOtp); | |
// System.out.println("New OTP '" + new String(nextOtp) + "' created."); | |
// printBuffer("After reading OTP", otpBuffer); | |
System.out.println("Stage 3"); | |
// Setting the limit back to capacity and this let us read the remaining buffer. | |
otpBuffer.limit(otpBuffer.capacity()); | |
printBuffer("After increasing limit", otpBuffer); | |
otpBuffer.get(nextOtp); | |
System.out.println("New OTP '" + new String(nextOtp) + "' created."); | |
printBuffer("After reading OTP", otpBuffer); | |
System.out.println("Stage 4"); | |
// Here the number of bytes read from file will be zero here. | |
// This is because the position of buffer is at limit which is 12. | |
numberOfBytes = otpFileChannel.read(otpBuffer); | |
System.out.println("After loading data to buffer, caret position in file is '" + numberOfBytes + "'"); | |
printBuffer("After new file read", otpBuffer); | |
// Rewind the buffer to set position bvack to zero | |
otpBuffer.rewind(); | |
printBuffer("After rewind", otpBuffer); | |
otpBuffer.get(nextOtp); | |
System.out.println("New OTP '" + new String(nextOtp) + "' created."); | |
printBuffer("After reading OTP", otpBuffer); | |
// Flip the limits and position. | |
otpBuffer.flip(); | |
printBuffer("After flip", otpBuffer); | |
// Now the limit is half of the capacity of buffer, the number of bytes read should be 6. | |
numberOfBytes = otpFileChannel.read(otpBuffer); | |
System.out.println("Number of bytes read from file is '" + numberOfBytes + "'"); | |
printBuffer("After new file read", otpBuffer); | |
// We should get a fresh OTP here. | |
otpBuffer.rewind(); | |
otpBuffer.get(nextOtp); | |
System.out.println("New OTP '" + new String(nextOtp) + "' created."); | |
printBuffer("After reading OTP", otpBuffer); | |
} | |
} | |
private static void printBuffer(String message, Buffer buffer) { | |
System.out.println(message + " - Buffer[capacity=" + buffer.capacity() + ", limit=" + buffer.limit() + ", " + | |
"position=" + buffer.position() + "]"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment