-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoding.java
More file actions
71 lines (59 loc) · 2.01 KB
/
coding.java
File metadata and controls
71 lines (59 loc) · 2.01 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
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ningluo
*/
public class coding {
static ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
public static byte[] encoding(short sh) throws IOException {
outputStream = new ByteArrayOutputStream();
ByteBuffer short_buf = ByteBuffer.allocate(2);
short_buf.putShort(sh);
outputStream.write(short_buf.array());
byte[] enc = outputStream.toByteArray();
return enc;
}
public static byte[] encoding(String s) throws IOException {
outputStream = new ByteArrayOutputStream();
outputStream.write(s.getBytes());
byte[] enc = outputStream.toByteArray();
return enc;
}
public static byte[] encoding(long x) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(x);
return buffer.array();
}
public static byte[] concatenate(byte[] a, byte[] b) throws IOException {
outputStream = new ByteArrayOutputStream();
outputStream.write(a);
outputStream.write(b);
byte[] con = outputStream.toByteArray();
return con;
}
public static boolean endsWith(ByteBuffer buf, String suffix) {
if (buf.position() < suffix.length()) return false;
for (int i=0; i<suffix.length(); i++) {
Character sc = suffix.charAt(suffix.length() - i - 1);
Character bc = (char) buf.get(buf.position() - i - 1);
if (sc != bc) return false;
}
return true;
// String bufStr;
// try {
// bufStr = new String(buf.array(), "ASCII");
// } catch (UnsupportedEncodingException e) {
// return false;
// }
// return (bufStr.endsWith(suffix));
}
}