changeset 670:165666be2059

NcDataUtils
author Devel 1
date Mon, 20 Nov 2017 09:01:45 +0100
parents 03684e7b9912
children a38680f29c27
files stress-tester/src/main/java/com/passus/st/reader/nc/NcDataUtils.java
diffstat 1 files changed, 124 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataUtils.java	Fri Nov 17 15:45:13 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataUtils.java	Mon Nov 20 09:01:45 2017 +0100
@@ -5,6 +5,7 @@
 import com.passus.data.ByteBuffUtils;
 import com.passus.data.ByteString;
 import com.passus.data.DataUtils;
+import java.nio.ByteBuffer;
 
 /**
  *
@@ -16,15 +17,15 @@
 
     public static final byte VERSION_MINOR = 0;
 
-    public static final int LONG_LENGTH = 0x80;
+    public static final int VLC_INT_TRESHOLD = 0x80;
 
-    public static final int SHORT_LENGTH_MAX = 0x7F;
+    public static final long VLC_LONG_TRESHOLD = 0x8000;
 
     private NcDataUtils() {
     }
 
     public static ByteString readByteString(ByteBuff buffer) {
-        int len = readLength(buffer);
+        int len = readIntVLC(buffer);
         return readByteString(buffer, len);
     }
 
@@ -47,31 +48,137 @@
         return buffer.toByteString(startIndex, endIndex);
     }
 
-    public static int readLength(ByteBuff buffer) {
-        int readed = 1;
-        int length = DataUtils.readInt(buffer, 1);
+    // TODO: można upchnąć więcej niż 127 w 1 bajcie
+    public static int readIntVLC(ByteBuff buffer) {
+//        int readed = 1;
+        int value = DataUtils.readInt(buffer, 1);
 
-        if ((length & LONG_LENGTH) != 0) {
-            length &= ~LONG_LENGTH;
-            if (length == 0 || length > 4) {
+        if ((value & VLC_INT_TRESHOLD) != 0) {
+            value &= ~VLC_INT_TRESHOLD;
+            if (value == 0 || value > 4) {
                 throw new IllegalArgumentException("Invalid length byte value.");
             }
 
-            readed += length;
-            length = DataUtils.readInt(buffer, length);
+//            readed += value;
+            value = DataUtils.readInt(buffer, value);
+        }
+
+//        buffer.skipBytes(readed);
+        return value;
+    }
+
+    public static int writeIntVLC(ByteBuff buffer, int value) {
+        if (value < VLC_INT_TRESHOLD) {
+            buffer.append((byte) value);
+            return 1;
+        } else {
+            int bytes = DataUtils.countBytes(value);
+            byte[] arr = new byte[bytes];
+            DataUtils.putInt(arr, 0, bytes, value);
+            buffer.append(bytes);
+            buffer.append(arr);
+            return bytes + 1;
+        }
+    }
+
+    public static long readLongVLC(ByteBuff buffer) {
+        int readed = 1;
+        long value = DataUtils.readInt(buffer, 2);
+
+        if ((value & VLC_LONG_TRESHOLD) != 0) {
+            value &= VLC_LONG_TRESHOLD;
+            if (value == 0 || value > 8) {
+                throw new IllegalArgumentException("Invalid length byte value.");
+            }
+
+            readed += value;
+            value = DataUtils.readLong(buffer, (int) value);
         }
 
         buffer.skipBytes(readed);
-        return length;
+        return value;
     }
 
-    public static int writeLength(ByteBuff buffer, long length) {
-        if (length <= SHORT_LENGTH_MAX) {
-            buffer.append((byte) length);
+    public static int writeLongVLC(ByteBuff buffer, long value) {
+        if (value < VLC_LONG_TRESHOLD) {
+            buffer.append((byte) (value >> 8));
+            buffer.append((byte) value);
+            return 2;
+        } else {
+            int bytes = DataUtils.countBytes(value);
+            byte[] arr = new byte[bytes];
+            DataUtils.putLong(arr, 0, bytes, value);
+            buffer.append((byte) (bytes >> 8));
+            buffer.append((byte) bytes);
+            buffer.append(arr);
+            return bytes + 2;
+        }
+    }
+
+    public static int readIntVLC(ByteBuffer buffer) {
+        int value = buffer.get();
+
+        if ((value & VLC_INT_TRESHOLD) != 0) {
+            value &= ~VLC_INT_TRESHOLD;
+            if (value == 0 || value > 4) {
+                throw new IllegalArgumentException("Invalid length byte value.");
+            }
+
+            byte[] dst = new byte[value];
+            buffer.get(dst);
+            value = DataUtils.getInt(dst, 0, value);
+        }
+
+        return value;
+    }
+
+    public static int writeIntVLC(ByteBuffer buffer, int value) {
+        if (value < VLC_INT_TRESHOLD) {
+            buffer.put((byte) value);
             return 1;
         } else {
-            throw new RuntimeException("Not supported yet.");
-            //DataUtils.writeLongToArray(length, arr, LONG_LENGTH);
+            int bytes = DataUtils.countBytes(value);
+            byte[] arr = new byte[bytes];
+            DataUtils.putInt(arr, 0, bytes, value);
+            buffer.put((byte) bytes);
+            buffer.put(arr);
+            return bytes + 1;
         }
     }
+
+    public static long readLongVLC(ByteBuffer buffer) {
+        int b1 = buffer.get();
+        int b2 = buffer.get();
+        long value = (b1 << 8) + b2;
+
+        if ((value & VLC_LONG_TRESHOLD) != 0) {
+            value &= VLC_LONG_TRESHOLD;
+            if (value == 0 || value > 8) {
+                throw new IllegalArgumentException("Invalid length byte value.");
+            }
+
+            byte[] dst = new byte[(int) value];
+            buffer.get(dst);
+            value = DataUtils.getInt(dst, 0, (int) value);
+        }
+
+        return value;
+    }
+
+    public static int writeLongVLC(ByteBuffer buffer, long value) {
+        if (value < VLC_LONG_TRESHOLD) {
+            buffer.put((byte) (value >> 8));
+            buffer.put((byte) value);
+            return 2;
+        } else {
+            int bytes = DataUtils.countBytes(value);
+            byte[] arr = new byte[bytes];
+            DataUtils.putLong(arr, 0, bytes, value);
+            buffer.put((byte) (bytes >> 8));
+            buffer.put((byte) bytes);
+            buffer.put(arr);
+            return bytes + 2;
+        }
+    }
+
 }