|
| 1 | +/** |
| 2 | + The MIT License (MIT) |
| 3 | +
|
| 4 | + Copyright (c) 2010-2019 head systems, ltd |
| 5 | +
|
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | + this software and associated documentation files (the "Software"), to deal in |
| 8 | + the Software without restriction, including without limitation the rights to |
| 9 | + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 10 | + the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | + subject to the following conditions: |
| 12 | +
|
| 13 | + The above copyright notice and this permission notice shall be included in all |
| 14 | + copies or substantial portions of the Software. |
| 15 | +
|
| 16 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 18 | + FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | + COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 20 | + IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | +
|
| 23 | + */ |
| 24 | + |
| 25 | +package su.interference.serialize; |
| 26 | + |
| 27 | +import su.interference.core.DataChunk; |
| 28 | +import su.interference.core.Instance; |
| 29 | +import su.interference.core.RowHeader; |
| 30 | +import su.interference.core.Types; |
| 31 | +import su.interference.exception.InternalException; |
| 32 | +import su.interference.persistent.Table; |
| 33 | + |
| 34 | +import java.io.UnsupportedEncodingException; |
| 35 | +import java.lang.reflect.Field; |
| 36 | +import java.net.MalformedURLException; |
| 37 | +import java.nio.ByteBuffer; |
| 38 | +import java.text.ParseException; |
| 39 | +import java.text.SimpleDateFormat; |
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.Date; |
| 42 | +import java.util.concurrent.atomic.AtomicInteger; |
| 43 | +import java.util.concurrent.atomic.AtomicLong; |
| 44 | + |
| 45 | +/** |
| 46 | + * @author Yuriy Glotanov |
| 47 | + * @since 1.0 |
| 48 | + */ |
| 49 | + |
| 50 | +public class ByteString { |
| 51 | + |
| 52 | + private byte[] b; |
| 53 | + |
| 54 | + public ByteString(byte[] b) { |
| 55 | + this.b = b; |
| 56 | + } |
| 57 | + |
| 58 | + public ByteString() { |
| 59 | + this.b = new byte[]{}; |
| 60 | + } |
| 61 | + |
| 62 | + public byte[] getBytes() { |
| 63 | + return b; |
| 64 | + } |
| 65 | + |
| 66 | + public byte[] getBytesFromInt (int p) { |
| 67 | + final ByteBuffer bb = ByteBuffer.allocate(4); |
| 68 | + bb.putInt(p); |
| 69 | + return bb.array(); |
| 70 | + } |
| 71 | + |
| 72 | + public byte[] getBytesFromLong (long p) { |
| 73 | + final ByteBuffer bb = ByteBuffer.allocate(8); |
| 74 | + bb.putLong(p); |
| 75 | + return bb.array(); |
| 76 | + } |
| 77 | + |
| 78 | + public void addBytesFromInt (int p) { |
| 79 | + final ByteBuffer bb = ByteBuffer.allocate(4); |
| 80 | + bb.putInt(p); |
| 81 | + append(bb.array()); |
| 82 | + } |
| 83 | + |
| 84 | + public void addBytesFromLong (long p) { |
| 85 | + final ByteBuffer bb = ByteBuffer.allocate(8); |
| 86 | + bb.putLong(p); |
| 87 | + append(bb.array()); |
| 88 | + } |
| 89 | + |
| 90 | + public int getIntFromBytes(byte[] b) { |
| 91 | + final ByteBuffer bb = ByteBuffer.allocate(4); |
| 92 | + bb.put(b); |
| 93 | + bb.rewind(); |
| 94 | + return bb.getInt(); |
| 95 | + } |
| 96 | + |
| 97 | + public long getLongFromBytes(byte[] b) { |
| 98 | + final ByteBuffer bb = ByteBuffer.allocate(8); |
| 99 | + bb.put(b); |
| 100 | + bb.rewind(); |
| 101 | + return bb.getLong(); |
| 102 | + } |
| 103 | + |
| 104 | + public int getIntFromBytes(int pos) { |
| 105 | + final byte[] b = substring(pos, pos+4); |
| 106 | + final ByteBuffer bb = ByteBuffer.allocate(4); |
| 107 | + bb.put(b); |
| 108 | + bb.rewind(); |
| 109 | + return bb.getInt(); |
| 110 | + } |
| 111 | + |
| 112 | + public long getLongFromBytes(int pos) { |
| 113 | + final byte[] b = substring(pos, pos+8); |
| 114 | + final ByteBuffer bb = ByteBuffer.allocate(8); |
| 115 | + bb.put(b); |
| 116 | + bb.rewind(); |
| 117 | + return bb.getLong(); |
| 118 | + } |
| 119 | + |
| 120 | + public byte[] substring(int start, int end){ |
| 121 | + final byte[] res = new byte[end-start]; |
| 122 | + System.arraycopy(b, start, res, 0, end-start); |
| 123 | + return res; |
| 124 | + } |
| 125 | + |
| 126 | + public void append(byte[] add){ |
| 127 | + final byte[] res = new byte[b.length + add.length]; |
| 128 | + System.arraycopy(b, 0, res, 0, b.length); |
| 129 | + System.arraycopy(add, 0, res, b.length, add.length); |
| 130 | + this.b = res; |
| 131 | + } |
| 132 | + |
| 133 | + public byte[] append(byte add){ |
| 134 | + final byte[] res = new byte[b.length + 1]; |
| 135 | + final byte[] a = new byte[1]; |
| 136 | + a[0] = add; |
| 137 | + System.arraycopy(b, 0, res, 0, b.length); |
| 138 | + System.arraycopy(a, 0, res, b.length, 1); |
| 139 | + return res; |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments