Guest User

Untitled

a guest
Jun 5th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. /**
  2.  * @author Dalton (Palidino)
  3.  */
  4. package com.palidino.nio;
  5.  
  6. import java.nio.ByteBuffer;
  7.  
  8. public class WriteEvent {
  9.     private ByteBuffer buffer;
  10.     private WriteEventHandler handler;
  11.  
  12.     public WriteEvent(byte[] original, int offset, int length, WriteEventHandler handler) {
  13.         if (original == null) {
  14.             throw new NullPointerException("original can't be null");
  15.         }
  16.         if (offset < 0 || length < 0) {
  17.             throw new NegativeArraySizeException("offset and length must be greater than 0");
  18.         }
  19.         if (offset > original.length || length + offset > original.length) {
  20.             throw new ArrayIndexOutOfBoundsException("length + offset can't be greater than original.length");
  21.         }
  22.         if (original.length == 0 || length == 0) {
  23.             throw new IllegalArgumentException("length must be greater than 0");
  24.         }
  25.         buffer = ByteBuffer.allocateDirect(length);
  26.         buffer.put(original, offset, length);
  27.         buffer.flip();
  28.         buffer = buffer.asReadOnlyBuffer();
  29.         this.handler = handler;
  30.     }
  31.  
  32.     public WriteEvent(ByteBuffer original, WriteEventHandler handler) {
  33.         buffer = ByteBuffer.allocateDirect(original.capacity());
  34.         ByteBuffer readOnlyCopy = original.asReadOnlyBuffer();
  35.         readOnlyCopy.flip();
  36.         buffer.put(readOnlyCopy);
  37.         buffer.flip();
  38.         buffer = buffer.asReadOnlyBuffer();
  39.         this.handler = handler;
  40.     }
  41.  
  42.     ByteBuffer getBuffer() {
  43.         return buffer;
  44.     }
  45.  
  46.     WriteEventHandler getHandler() {
  47.         return handler;
  48.     }
  49. }
Add Comment
Please, Sign In to add comment