]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.server/src/org/simantics/db/server/protocol/DataBuffer.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.server / src / org / simantics / db / server / protocol / DataBuffer.java
index ba2dbd34d2b9aa0b788d4d077787fbd66dfe40f4..deb20b08e5af6619f24d23b75e6d98ebfc03d316 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.server.protocol;\r
-\r
-import java.io.PrintStream;\r
-import java.io.UnsupportedEncodingException;\r
-import java.nio.ByteBuffer;\r
-import java.nio.ByteOrder;\r
-import java.nio.CharBuffer;\r
-import java.nio.charset.CharacterCodingException;\r
-import java.nio.charset.Charset;\r
-import java.nio.charset.CharsetDecoder;\r
-import java.nio.charset.CodingErrorAction;\r
-\r
-public class DataBuffer {\r
-    private static final Charset UTF8 = Charset.forName("UTF-8");\r
-    private boolean DEBUG = false;\r
-    private ByteBuffer buffer = null;\r
-    enum Allocation { JavaAllocation, DirectAllocation }\r
-    DataBuffer(Allocation a) {\r
-        switch (a) {\r
-        case JavaAllocation:\r
-             buffer = ByteBuffer.allocate(20);\r
-            break;\r
-        case DirectAllocation:\r
-            buffer = ByteBuffer.allocate(20);\r
-            break;\r
-        }\r
-    }\r
-    DataBuffer(byte[] bytes, int size) {\r
-        buffer = ByteBuffer.allocate(size);\r
-        buffer.put(bytes, 0, size);\r
-        buffer.rewind();\r
-    }\r
-    DataBuffer(byte[] bytes) {\r
-        buffer = ByteBuffer.wrap(bytes);\r
-        buffer.rewind();\r
-    }\r
-    public DataBuffer(ByteBuffer byteBuffer) {\r
-        buffer = byteBuffer.duplicate();\r
-        buffer.order(byteBuffer.order());\r
-    }\r
-    public DataBuffer(ByteBuffer byteBuffer, int dummy) {\r
-        buffer = byteBuffer;\r
-    }\r
-    public ByteBuffer getByteBuffer() {\r
-        return buffer;\r
-    }\r
-    private void checkCapacity(int a) {\r
-        if (buffer.capacity() - buffer.position() >= a)\r
-            return;\r
-        ByteBuffer t = buffer;\r
-        int capacity = t.capacity();\r
-        int position = t.position();\r
-        int newCapacity = capacity + Math.max(capacity/2, a);\r
-        buffer = ByteBuffer.allocate(newCapacity);\r
-        t.clear();\r
-        buffer.put(t);\r
-        buffer.position(position);\r
-        buffer.order(t.order());\r
-    }\r
-    public void clear() {\r
-        buffer.clear();\r
-    }\r
-    public void order(ByteOrder byteOrder) {\r
-        buffer.order(byteOrder);\r
-    }\r
-    public void mark() {\r
-        buffer.mark();\r
-    }\r
-    public void position(int position) {\r
-        buffer.position(position);\r
-    }\r
-    public byte[] getBytes() {\r
-        byte[] t = new byte[buffer.position()];\r
-        buffer.clear();\r
-        buffer.get(t);\r
-        return t;\r
-    }\r
-    public short get(short a) {\r
-        return buffer.getShort();\r
-    }\r
-    public int get(int a) {\r
-        return buffer.getInt();\r
-    }\r
-    public void put(int a) {\r
-        checkCapacity(4);\r
-        buffer.putInt(a);\r
-    }\r
-    public int[] get(int[] a) {\r
-        int size = buffer.getInt();\r
-        int[] t = new int[size];\r
-        buffer.asIntBuffer().get(t);\r
-        buffer.position(buffer.position() + size * 4);\r
-        return t;\r
-    }\r
-    public void put(int[] a) {\r
-        if (null == a)\r
-            a = new int[0];\r
-        checkCapacity(4 + 4*a.length);\r
-        this.put(a.length);\r
-        for (int i=0; i<a.length; ++i)\r
-            buffer.putInt(a[i]);\r
-    }\r
-    public long[] get(long[] a) {\r
-        int size = buffer.getInt();\r
-        long[] t = new long[size];\r
-        buffer.asLongBuffer().get(t, 0, size);\r
-        buffer.position(buffer.position() + 8 * size);\r
-        return t;\r
-    }\r
-    public void put(long[] a) {\r
-        checkCapacity(4 + 8*a.length);\r
-        this.put(a.length);\r
-        for (int i=0; i<a.length; ++i)\r
-            buffer.putLong(a[i]);\r
-    }\r
-    public boolean get(boolean a) {\r
-        byte b = buffer.get();\r
-        return !(0 == b);\r
-    }\r
-    public void put(boolean a) {\r
-        checkCapacity(1);\r
-        byte b = a ? (byte)0xff : (byte)0;\r
-        buffer.put(b);\r
-    }\r
-    public byte get(byte a) {\r
-        return buffer.get();\r
-    }\r
-    public void put(byte a) {\r
-        checkCapacity(1);\r
-        buffer.put(a);\r
-    }\r
-    public byte[] get(byte[] a) {\r
-        int size = buffer.getInt();\r
-        byte[] t = new byte[size];\r
-        buffer.get(t, 0, size);\r
-        return t;\r
-    }\r
-    public void put(byte[] a) {\r
-        checkCapacity(4 + a.length);\r
-        this.put(a.length);\r
-        buffer.put(a);\r
-    }\r
-    public ByteBuffer get(ByteBuffer a) {\r
-        int size = buffer.getInt();\r
-        byte[] t = new byte[size];\r
-        buffer.get(t, 0, size);\r
-        a.put(t);\r
-        return a;\r
-    }\r
-    public void put(ByteBuffer a) {\r
-        byte[] t = a.array();\r
-        checkCapacity(4 + t.length);\r
-        this.put(t.length);\r
-        buffer.put(t);\r
-    }\r
-    public static void printChars(PrintStream out, ByteBuffer buf, int pos) {\r
-        out.print("[" + buf.limit() + "]");\r
-        for(int i=pos;i<buf.limit();++i) {\r
-            int val = (int)buf.get(i);\r
-            if(val < 0)\r
-                val += 256;\r
-            char c = (char)val;\r
-            if(c >= 32 && c < 128)\r
-                out.print(c);\r
-            else if(c==0)\r
-                out.print('\u00A4');\r
-            else\r
-                out.print("(" + val + ")");\r
-        }\r
-        out.println();\r
-    }\r
-    public String get(String a) {\r
-        byte[] t = null;\r
-        t = this.get(t);\r
-        CharsetDecoder decoder = UTF8.newDecoder();\r
-        ByteBuffer bbuf = ByteBuffer.wrap(t);\r
-        CharBuffer cbuf;\r
-        String s = null;\r
-        try {\r
-            cbuf = decoder.decode(bbuf);\r
-            s = cbuf.toString();\r
-        } catch (CharacterCodingException e) {\r
-            bbuf.rewind();\r
-            if (DEBUG)\r
-                printChars(System.err, bbuf, 0);\r
-            try {\r
-                cbuf = UTF8\r
-                .newDecoder()\r
-                .onMalformedInput(CodingErrorAction.REPLACE)\r
-                .onUnmappableCharacter(CodingErrorAction.REPLACE)\r
-                .decode(bbuf);\r
-                s = cbuf.toString();\r
-            } catch (CharacterCodingException e1) {\r
-                throw new Error("not possible", e1);\r
-            }\r
-        }\r
-        return s;\r
-    }\r
-    public void put(String a) {\r
-        try {\r
-            put(a.getBytes(UTF8.name()));\r
-        } catch (UnsupportedEncodingException e) {\r
-            throw new Error("UnsupportedEncoding: " + UTF8.name());\r
-        }\r
-    }\r
-    public long get(long a) {\r
-        return buffer.getLong();\r
-    }\r
-    public void put(long a) {\r
-        checkCapacity(8);\r
-        buffer.putLong(a);\r
-    }\r
-    public float get(float f) {\r
-        return buffer.getFloat();\r
-    }\r
-    public void put(float a) {\r
-        checkCapacity(4);\r
-        buffer.putFloat(a);\r
-    }\r
-    public double get(double f) {\r
-        return buffer.getDouble();\r
-    }\r
-    public void put(double a) {\r
-        checkCapacity(8);\r
-        buffer.putDouble(a);\r
-    }\r
-    public String[] get(String[] a) {\r
-        int size = buffer.getInt();\r
-        String[] t = new String[size];\r
-        for (int i=0; i<size; ++i)\r
-            t[i] = this.get(t[i]);\r
-        return t;\r
-    }\r
-    public void put(String[] a) {\r
-        checkCapacity(4);\r
-        this.put(a.length);\r
-        for (int i=0; i<a.length; ++i)\r
-            this.put(a[i]);\r
-    }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.server.protocol;
+
+import java.io.PrintStream;
+import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.CharBuffer;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CodingErrorAction;
+
+public class DataBuffer {
+    private static final Charset UTF8 = Charset.forName("UTF-8");
+    private boolean DEBUG = false;
+    private ByteBuffer buffer = null;
+    enum Allocation { JavaAllocation, DirectAllocation }
+    DataBuffer(Allocation a) {
+        switch (a) {
+        case JavaAllocation:
+             buffer = ByteBuffer.allocate(20);
+            break;
+        case DirectAllocation:
+            buffer = ByteBuffer.allocate(20);
+            break;
+        }
+    }
+    DataBuffer(byte[] bytes, int size) {
+        buffer = ByteBuffer.allocate(size);
+        buffer.put(bytes, 0, size);
+        buffer.rewind();
+    }
+    DataBuffer(byte[] bytes) {
+        buffer = ByteBuffer.wrap(bytes);
+        buffer.rewind();
+    }
+    public DataBuffer(ByteBuffer byteBuffer) {
+        buffer = byteBuffer.duplicate();
+        buffer.order(byteBuffer.order());
+    }
+    public DataBuffer(ByteBuffer byteBuffer, int dummy) {
+        buffer = byteBuffer;
+    }
+    public ByteBuffer getByteBuffer() {
+        return buffer;
+    }
+    private void checkCapacity(int a) {
+        if (buffer.capacity() - buffer.position() >= a)
+            return;
+        ByteBuffer t = buffer;
+        int capacity = t.capacity();
+        int position = t.position();
+        int newCapacity = capacity + Math.max(capacity/2, a);
+        buffer = ByteBuffer.allocate(newCapacity);
+        t.clear();
+        buffer.put(t);
+        buffer.position(position);
+        buffer.order(t.order());
+    }
+    public void clear() {
+        buffer.clear();
+    }
+    public void order(ByteOrder byteOrder) {
+        buffer.order(byteOrder);
+    }
+    public void mark() {
+        buffer.mark();
+    }
+    public void position(int position) {
+        buffer.position(position);
+    }
+    public byte[] getBytes() {
+        byte[] t = new byte[buffer.position()];
+        buffer.clear();
+        buffer.get(t);
+        return t;
+    }
+    public short get(short a) {
+        return buffer.getShort();
+    }
+    public int get(int a) {
+        return buffer.getInt();
+    }
+    public void put(int a) {
+        checkCapacity(4);
+        buffer.putInt(a);
+    }
+    public int[] get(int[] a) {
+        int size = buffer.getInt();
+        int[] t = new int[size];
+        buffer.asIntBuffer().get(t);
+        buffer.position(buffer.position() + size * 4);
+        return t;
+    }
+    public void put(int[] a) {
+        if (null == a)
+            a = new int[0];
+        checkCapacity(4 + 4*a.length);
+        this.put(a.length);
+        for (int i=0; i<a.length; ++i)
+            buffer.putInt(a[i]);
+    }
+    public long[] get(long[] a) {
+        int size = buffer.getInt();
+        long[] t = new long[size];
+        buffer.asLongBuffer().get(t, 0, size);
+        buffer.position(buffer.position() + 8 * size);
+        return t;
+    }
+    public void put(long[] a) {
+        checkCapacity(4 + 8*a.length);
+        this.put(a.length);
+        for (int i=0; i<a.length; ++i)
+            buffer.putLong(a[i]);
+    }
+    public boolean get(boolean a) {
+        byte b = buffer.get();
+        return !(0 == b);
+    }
+    public void put(boolean a) {
+        checkCapacity(1);
+        byte b = a ? (byte)0xff : (byte)0;
+        buffer.put(b);
+    }
+    public byte get(byte a) {
+        return buffer.get();
+    }
+    public void put(byte a) {
+        checkCapacity(1);
+        buffer.put(a);
+    }
+    public byte[] get(byte[] a) {
+        int size = buffer.getInt();
+        byte[] t = new byte[size];
+        buffer.get(t, 0, size);
+        return t;
+    }
+    public void put(byte[] a) {
+        checkCapacity(4 + a.length);
+        this.put(a.length);
+        buffer.put(a);
+    }
+    public ByteBuffer get(ByteBuffer a) {
+        int size = buffer.getInt();
+        byte[] t = new byte[size];
+        buffer.get(t, 0, size);
+        a.put(t);
+        return a;
+    }
+    public void put(ByteBuffer a) {
+        byte[] t = a.array();
+        checkCapacity(4 + t.length);
+        this.put(t.length);
+        buffer.put(t);
+    }
+    public static void printChars(PrintStream out, ByteBuffer buf, int pos) {
+        out.print("[" + buf.limit() + "]");
+        for(int i=pos;i<buf.limit();++i) {
+            int val = (int)buf.get(i);
+            if(val < 0)
+                val += 256;
+            char c = (char)val;
+            if(c >= 32 && c < 128)
+                out.print(c);
+            else if(c==0)
+                out.print('\u00A4');
+            else
+                out.print("(" + val + ")");
+        }
+        out.println();
+    }
+    public String get(String a) {
+        byte[] t = null;
+        t = this.get(t);
+        CharsetDecoder decoder = UTF8.newDecoder();
+        ByteBuffer bbuf = ByteBuffer.wrap(t);
+        CharBuffer cbuf;
+        String s = null;
+        try {
+            cbuf = decoder.decode(bbuf);
+            s = cbuf.toString();
+        } catch (CharacterCodingException e) {
+            bbuf.rewind();
+            if (DEBUG)
+                printChars(System.err, bbuf, 0);
+            try {
+                cbuf = UTF8
+                .newDecoder()
+                .onMalformedInput(CodingErrorAction.REPLACE)
+                .onUnmappableCharacter(CodingErrorAction.REPLACE)
+                .decode(bbuf);
+                s = cbuf.toString();
+            } catch (CharacterCodingException e1) {
+                throw new Error("not possible", e1);
+            }
+        }
+        return s;
+    }
+    public void put(String a) {
+        try {
+            put(a.getBytes(UTF8.name()));
+        } catch (UnsupportedEncodingException e) {
+            throw new Error("UnsupportedEncoding: " + UTF8.name());
+        }
+    }
+    public long get(long a) {
+        return buffer.getLong();
+    }
+    public void put(long a) {
+        checkCapacity(8);
+        buffer.putLong(a);
+    }
+    public float get(float f) {
+        return buffer.getFloat();
+    }
+    public void put(float a) {
+        checkCapacity(4);
+        buffer.putFloat(a);
+    }
+    public double get(double f) {
+        return buffer.getDouble();
+    }
+    public void put(double a) {
+        checkCapacity(8);
+        buffer.putDouble(a);
+    }
+    public String[] get(String[] a) {
+        int size = buffer.getInt();
+        String[] t = new String[size];
+        for (int i=0; i<size; ++i)
+            t[i] = this.get(t[i]);
+        return t;
+    }
+    public void put(String[] a) {
+        checkCapacity(4);
+        this.put(a.length);
+        for (int i=0; i<a.length; ++i)
+            this.put(a[i]);
+    }
+}