changeset 671:a38680f29c27

NcDataUtils -> NcDataHelper
author Devel 2
date Mon, 20 Nov 2017 10:23:34 +0100
parents 165666be2059
children 7bc7cfb26ddf
files stress-tester/src/main/java/com/passus/st/reader/nc/NcDataHelper.java stress-tester/src/main/java/com/passus/st/reader/nc/NcDataUtils.java stress-tester/src/main/java/com/passus/st/reader/nc/NcDataUtilsTmp.java
diffstat 3 files changed, 218 insertions(+), 216 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataHelper.java	Mon Nov 20 10:23:34 2017 +0100
@@ -0,0 +1,218 @@
+package com.passus.st.reader.nc;
+
+import com.passus.commons.AsciiUtils;
+import com.passus.data.ByteBuff;
+import com.passus.data.ByteBuffUtils;
+import com.passus.data.ByteString;
+import com.passus.data.DataUtils;
+import com.passus.net.IpAddress;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+import org.apache.commons.lang3.mutable.MutableInt;
+import org.apache.commons.lang3.mutable.MutableLong;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class NcDataHelper {
+
+    public static final byte VERSION_MAJOR = 1;
+
+    public static final byte VERSION_MINOR = 0;
+
+    public static final int VLC_INT_TRESHOLD = 0x80;
+
+    public static final long VLC_LONG_TRESHOLD = 0x8000;
+
+    public static final Charset CHARSET = Charset.forName("UTF-8");
+
+    private final MutableInt mutableInt = new MutableInt();
+
+    private final MutableLong mutableLong = new MutableLong();
+
+    private static final ThreadLocal<NcDataHelper> THREAD_LOCAL = new ThreadLocal<NcDataHelper>() {
+        @Override
+        public NcDataHelper get() {
+            return new NcDataHelper();
+        }
+
+    };
+
+    public ByteString readByteString(ByteBuff buffer) {
+        int len = readIntVLC(buffer);
+        return readByteString(buffer, len);
+    }
+
+    public ByteString readByteString(ByteBuff buffer, int len) {
+        int startIndex = buffer.startIndex();
+        return buffer.toByteString(startIndex, startIndex + len);
+    }
+
+    public ByteString readByteStringNullTerminated(ByteBuff buffer) {
+        return readByteStringTerminated(buffer, AsciiUtils.NUL);
+    }
+
+    public ByteString readByteStringTerminated(ByteBuff buffer, byte delim) {
+        int startIndex = buffer.startIndex();
+        int endIndex = ByteBuffUtils.localize(buffer, startIndex, delim);
+        if (endIndex == -1) {
+            return null;
+        }
+
+        return buffer.toByteString(startIndex, endIndex);
+    }
+
+    public 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 int readIntVLC(ByteBuff buffer) {
+        int len = getIntVLC(buffer.buffer(), buffer.startIndex(), buffer.endIndex(), mutableInt);
+        buffer.skipBytes(len);
+        return mutableInt.intValue();
+    }
+
+    public int readIntVLC(ByteBuffer buffer) {
+        int len = getIntVLC(buffer.array(), buffer.position(), buffer.limit(), mutableInt);
+        buffer.position(buffer.position() + len);
+        return mutableInt.intValue();
+    }
+
+    public int getIntVLC(byte[] data, int startIndex, int endIndex, MutableInt length) {
+        int value = data[startIndex];
+        int readed = 1;
+        if ((value & VLC_INT_TRESHOLD) != 0) {
+            startIndex++;
+            value &= ~VLC_INT_TRESHOLD;
+            if (value == 0 || value > 4) {
+                throw new IllegalArgumentException("Invalid length byte value.");
+            } else if (value > endIndex - startIndex) {
+                throw new IndexOutOfBoundsException();
+            }
+
+            readed += value;
+            value = DataUtils.getInt(data, startIndex, value);
+        }
+
+        length.setValue(value);
+        return readed;
+    }
+
+    public int writeIntVLC(int value, ByteBuff buffer) {
+        int len = writeIntVLC(value, buffer.buffer(), buffer.startIndex(), buffer.endIndex());
+        buffer.skipBytes(len);
+        return len;
+    }
+
+    public int writeIntVLC(int value, ByteBuffer buffer) {
+        int len = writeIntVLC(value, buffer.array(), buffer.position(), buffer.limit());
+        buffer.position(buffer.position() + len);
+        return len;
+    }
+
+    public int writeIntVLC(int value, byte[] data) {
+        return writeIntVLC(value, data, 0, data.length);
+    }
+
+    public int writeIntVLC(int value, byte[] data, int startIndex) {
+        return writeIntVLC(value, data, startIndex, data.length);
+    }
+
+    public int writeIntVLC(int value, byte[] data, int startIndex, int endIndex) {
+        if (value < VLC_INT_TRESHOLD) {
+            data[startIndex] = (byte) value;
+            return 1;
+        } else {
+            int bytes = DataUtils.countBytes(value);
+            if (bytes > (endIndex - startIndex + 1)) {
+                throw new IndexOutOfBoundsException();
+            }
+
+            data[startIndex] = (byte) bytes;
+            startIndex++;
+            DataUtils.putInt(data, startIndex, bytes, value);
+            return bytes + 1;
+        }
+    }
+
+    public long readLongVLC(ByteBuff buffer) {
+        int len = getLongVLC(buffer.buffer(), buffer.startIndex(), buffer.endIndex(), mutableLong);
+        buffer.skipBytes(len);
+        return mutableLong.longValue();
+    }
+
+    public long readLongVLC(ByteBuffer buffer) {
+        int len = getLongVLC(buffer.array(), buffer.position(), buffer.limit(), mutableLong);
+        buffer.position(buffer.position() + len);
+        return mutableLong.longValue();
+    }
+
+    public int getLongVLC(byte[] data, int startIndex, int endIndex, MutableLong length) {
+        int b1 = data[startIndex++];
+        int b2 = data[startIndex++];
+        long value = (b1 << 8) + b2;
+        int readed = 2;
+
+        if ((value & VLC_LONG_TRESHOLD) != 0) {
+            value &= VLC_LONG_TRESHOLD;
+            if (value == 0 || value > 8) {
+                throw new IllegalArgumentException("Invalid length byte value.");
+            } else if (endIndex - startIndex < value) {
+                throw new IndexOutOfBoundsException();
+            }
+
+            readed += value;
+            value = DataUtils.getLong(data, startIndex, (int) value);
+        }
+
+        length.setValue(value);
+        return readed;
+    }
+
+    public int writeLongVLC(long value, ByteBuffer buffer) {
+        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;
+        }
+    }
+
+    public int writeStringNullTerminated(ByteBuffer buffer, String value) {
+        byte[] data = value.getBytes(CHARSET);
+        buffer.put(data);
+        buffer.put(AsciiUtils.NUL);
+        return data.length + 1;
+    }
+
+    public int writeIpAddress(ByteBuffer buffer, IpAddress ip) {
+        byte[] data = ip.getAddress();
+        buffer.put((byte) ip.getVersion());
+        buffer.put(data);
+        return data.length + 1;
+    }
+
+    public static NcDataHelper getInstance() {
+        return THREAD_LOCAL.get();
+    }
+}
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataUtils.java	Mon Nov 20 09:01:45 2017 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,184 +0,0 @@
-package com.passus.st.reader.nc;
-
-import com.passus.commons.AsciiUtils;
-import com.passus.data.ByteBuff;
-import com.passus.data.ByteBuffUtils;
-import com.passus.data.ByteString;
-import com.passus.data.DataUtils;
-import java.nio.ByteBuffer;
-
-/**
- *
- * @author Mirosław Hawrot
- */
-public class NcDataUtils {
-
-    public static final byte VERSION_MAJOR = 1;
-
-    public static final byte VERSION_MINOR = 0;
-
-    public static final int VLC_INT_TRESHOLD = 0x80;
-
-    public static final long VLC_LONG_TRESHOLD = 0x8000;
-
-    private NcDataUtils() {
-    }
-
-    public static ByteString readByteString(ByteBuff buffer) {
-        int len = readIntVLC(buffer);
-        return readByteString(buffer, len);
-    }
-
-    public static ByteString readByteString(ByteBuff buffer, int len) {
-        int startIndex = buffer.startIndex();
-        return buffer.toByteString(startIndex, startIndex + len);
-    }
-
-    public static ByteString readByteStringNullTerminated(ByteBuff buffer) {
-        return readByteStringTerminated(buffer, AsciiUtils.NUL);
-    }
-
-    public static ByteString readByteStringTerminated(ByteBuff buffer, byte delim) {
-        int startIndex = buffer.startIndex();
-        int endIndex = ByteBuffUtils.localize(buffer, startIndex, delim);
-        if (endIndex == -1) {
-            return null;
-        }
-
-        return buffer.toByteString(startIndex, endIndex);
-    }
-
-    // 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 ((value & VLC_INT_TRESHOLD) != 0) {
-            value &= ~VLC_INT_TRESHOLD;
-            if (value == 0 || value > 4) {
-                throw new IllegalArgumentException("Invalid length byte value.");
-            }
-
-//            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 value;
-    }
-
-    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 {
-            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;
-        }
-    }
-
-}
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataUtilsTmp.java	Mon Nov 20 09:01:45 2017 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,32 +0,0 @@
-package com.passus.st.reader.nc;
-
-import com.passus.commons.AsciiUtils;
-import com.passus.net.IpAddress;
-import java.nio.ByteBuffer;
-import java.nio.charset.Charset;
-
-/**
- *
- * @author Mirosław Hawrot
- *
- */
-/* @TODO Klasa tymczasowa - do usuniecia. Metody przeniesc do NcDataUtils. */
-public class NcDataUtilsTmp {
-
-    public static final Charset CHARSET = Charset.forName("UTF-8");
-
-    public static int writeStringNullTerminated(ByteBuffer buffer, String value) {
-        byte[] data = value.getBytes(CHARSET);
-        buffer.put(data);
-        buffer.put(AsciiUtils.NUL);
-        return data.length + 1;
-    }
-
-    public static int writeIpAddress(ByteBuffer buffer, IpAddress ip) {
-        byte[] data = ip.getAddress();
-        buffer.put((byte) ip.getVersion());
-        buffer.put(data);
-        return data.length + 1;
-    }
-
-}