forked from nsquare-jdzone/java-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongByteOperationUtil.java
More file actions
112 lines (100 loc) · 3.92 KB
/
LongByteOperationUtil.java
File metadata and controls
112 lines (100 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.javadeveloperzone;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.Buffer;
import java.nio.ByteBuffer;
/**
* Created by JavaDeveloperZone on 2/5/2018.
*/
public class LongByteOperationUtil {
public static void main(String[] args) throws IOException {
LongByteOperationUtil byteOperationUtil = new LongByteOperationUtil();
long number = 4444444L;
System.out.println("***************************");
byte [] intBytes = byteOperationUtil.longToByteArray(number);
long num = byteOperationUtil.convertByteArrayToLong(intBytes);
System.out.println("Num is :"+num);
intBytes = byteOperationUtil.longToByteArray(number);
num = byteOperationUtil.convertByteArrayToLong(intBytes);
System.out.println("Num is :"+num);
intBytes = byteOperationUtil.convertLongArrayToByteArray(new long[]{1,2,3});
long [] longArray = byteOperationUtil.convertByteArrayToLongArray(intBytes);
for(long l : longArray){
System.out.println(l);
}
/*for(byte b : intBytes){
System.out.println(b);
}*/
}
private byte [] convertLongToByteArray(long number) {
ByteBuffer byteBuffer = ByteBuffer.allocate(Long.BYTES);
byteBuffer.putLong(number);
return byteBuffer.array();
}
private static byte[] longtoBytes(long data) {
return new byte[]{
(byte) ((data >> 56) & 0xff),
(byte) ((data >> 48) & 0xff),
(byte) ((data >> 40) & 0xff),
(byte) ((data >> 32) & 0xff),
(byte) ((data >> 24) & 0xff),
(byte) ((data >> 16) & 0xff),
(byte) ((data >> 8) & 0xff),
(byte) ((data >> 0) & 0xff),
};
}
private byte[] longToByteArray ( final long i ) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeLong(i);
dos.flush();
return bos.toByteArray();
}
private long convertByteArrayToLong(byte[] longBytes){
ByteBuffer byteBuffer = ByteBuffer.allocate(Long.BYTES);
byteBuffer.put(longBytes);
byteBuffer.flip();
return byteBuffer.getLong();
}
private byte[] convertLongArrayToByteArray(long[] data) {
if (data == null) return null;
// ----------
byte[] byts = new byte[data.length * Long.BYTES];
for (int i = 0; i < data.length; i++)
System.arraycopy(convertLongToByteArray(data[i]), 0, byts, i * Long.BYTES, Long.BYTES);
return byts;
}
public long[] convertByteArrayToLongArray(byte[] data) {
if (data == null || data.length % Long.BYTES != 0) return null;
// ----------
long[] longs = new long[data.length / Long.BYTES];
for (int i = 0; i < longs.length; i++)
longs[i] = ( convertByteArrayToLong(new byte[] {
data[(i*Long.BYTES)],
data[(i*Long.BYTES)+1],
data[(i*Long.BYTES)+2],
data[(i*Long.BYTES)+3],
data[(i*Long.BYTES)+4],
data[(i*Long.BYTES)+5],
data[(i*Long.BYTES)+6],
data[(i*Long.BYTES)+7],
} ));
return longs;
}
public long convertByteArrayToLongArrayByShift(byte[] data) {
if (data == null || data.length % Long.BYTES != 0) return -1;
// ----------
return ( convertByteArrayToLong(new byte[] {
data[(Long.BYTES)],
data[(Long.BYTES)+1],
data[(Long.BYTES)+2],
data[(Long.BYTES)+3],
data[(Long.BYTES)+4],
data[(Long.BYTES)+5],
data[(Long.BYTES)+6],
data[(Long.BYTES)+7],
} ));
}
}