aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/gnunet/construct/parsers/IntegerUtil.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/gnunet/construct/parsers/IntegerUtil.java')
-rw-r--r--src/main/java/org/gnunet/construct/parsers/IntegerUtil.java92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/main/java/org/gnunet/construct/parsers/IntegerUtil.java b/src/main/java/org/gnunet/construct/parsers/IntegerUtil.java
new file mode 100644
index 0000000..83391de
--- /dev/null
+++ b/src/main/java/org/gnunet/construct/parsers/IntegerUtil.java
@@ -0,0 +1,92 @@
1/*
2 This file is part of GNUnet.
3 (C) 2011, 2012 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21package org.gnunet.construct.parsers;
22
23
24import java.math.BigInteger;
25import java.nio.ByteBuffer;
26
27public class IntegerUtil {
28 public static long readLong(ByteBuffer srcBuf, boolean isSigned, int byteSize) {
29 long val = 0;
30
31 final int first = srcBuf.position();
32 final int last = first + byteSize - 1;
33
34 // read all bytes except the last
35 while (srcBuf.position() != last) {
36 byte b = srcBuf.get();
37 // byte b may be signed, if so interpret it as unsigned byte; store it in an int
38 int s = b >= 0 ? b : (256 + b);
39
40 val |= s;
41 val <<= 8;
42 }
43
44 // read the last byte, we don't have to shift val after that
45 byte b = srcBuf.get();
46 int s = b >= 0 ? b : (256 + b);
47 val |= s;
48
49 if (isSigned) {
50 // explicitly OR sign bit to the right place if the source buffer is
51 // too large
52 long sign = (srcBuf.get(first) & 0x80);
53 val |= (sign << 7);
54 }
55
56 return val;
57 }
58
59 public static void writeLong(final long val, final ByteBuffer dstBuf, boolean isSigned, int byteSize) {
60 long myval = val;
61
62 // position of the last byte we are responsible to write
63 int last = dstBuf.position() + byteSize - 1;
64
65 while (last >= dstBuf.position()) {
66 dstBuf.put(last, (byte) (myval & 0xFF));
67 myval >>>= 8;
68 last -= 1;
69 }
70
71 if (isSigned) {
72 // a long has 8 bytes, shift by 7 bytes (non-arithmetically) to get the sign
73 byte sign = (byte) ((val >>> (7*8)) & 0x80);
74 // remove the sign bit from the buffer
75 dstBuf.put(dstBuf.position() + byteSize - 1, (byte) (dstBuf.get(dstBuf.position() + byteSize - 1) & ~sign));
76 // ... and put it in the right place (lowest byte)
77 dstBuf.put(dstBuf.position(), (byte) (dstBuf.get(dstBuf.position()) | sign));
78
79 }
80
81 dstBuf.position(dstBuf.position() + byteSize);
82 }
83
84
85 public static void writeBitInteger(BigInteger big, ByteBuffer dstBuf, boolean isSigned, int byteSize) {
86 throw new UnsupportedOperationException("not yet implemented");
87 }
88
89 public static BigInteger readBigInteger(final ByteBuffer srcBuf, boolean isSigned, int byteSize) {
90 throw new UnsupportedOperationException("not yet implemented");
91 }
92}