Skip to content

Instantly share code, notes, and snippets.

@NorimasaNabeta
Created October 24, 2021 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NorimasaNabeta/d69485dc886fecea40f5f5b8ad016f17 to your computer and use it in GitHub Desktop.
Save NorimasaNabeta/d69485dc886fecea40f5f5b8ad016f17 to your computer and use it in GitHub Desktop.
hexdump sample
// -*- mode: java; coding: utf-8 -*-
//
// Time-stamp: <2021-10-22 09:12:02 norim>
//
// TODO: echo server を作成、任意のデータを受信すると、それをhexdump して返信
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
// byte[] b = "Morning".getBytes(StandardCharsets.UTF_8); //byte array
// String string = new String(b, StandardCharsets.UTF_8); //string with "UTF-8" encoding
class receiver {
//
// TODO: StringBufferかDataOutoutStreamに出力する版を作成
//
public static void hexdump(byte[] binIn){
final int BYTES_PER_LINE = 16;
StringBuffer sb = new StringBuffer();
byte[] binDup = new byte[BYTES_PER_LINE];
for (int i = 0; binIn.length > i; i++) {
if ((sb.length()>0)) {
if ((i % BYTES_PER_LINE) == 0){
String ascii = new String(binDup, java.nio.charset.StandardCharsets.UTF_8);
String addrs = String.format("%08x", i);
System.out.println(addrs + ": "+ sb.toString() + " " + ascii);
sb.delete(0, sb.length());
}
else {
sb.append(" ");
}
}
sb.append(String.format("%02x", binIn[i] & 0xff));
if (((binIn[i] & 0xff) > 31) && ((binIn[i] & 0xff) < 64)){
binDup[i % BYTES_PER_LINE] = binIn[i];
}
else {
binDup[i % BYTES_PER_LINE] = 45;
}
}
sb.delete(0, sb.length());
}
public static byte[] sample(){
final int MAGIC = 0x12; // 1234; // 0x04D2
final int VERSION = 0x34; // 5678; // 0x162E
// final byte [] CODE = new byte
DataOutputStream in = null;
ByteArrayOutputStream bIn = new ByteArrayOutputStream(100);
try {
in = new DataOutputStream(bIn);
in.write(MAGIC);
in.write(VERSION);
in.writeShort(1); // max_stack
in.writeShort(1); // max_locals
for (int i=1; i<50; i++){
in.writeShort(i);
}
// in.writeInt(CODE.length); // code length
// in.write(CODE);
if(in != null) {
try {
in.close();
} catch (IOException e) {
// throw new ObjenesisException(e);
// return bIn.toByteArray();
return null;
}
}
} catch (IOException e) {
// throw new ObjenesisException(e);
} finally {
if(in != null) {
try {
in.close();
} catch (IOException e) {
//throw new ObjenesisException(e);
}
}
}
return bIn.toByteArray();
}
public static void main(String args[]) throws Exception {
// DatagramSocket serverSocket = new DatagramSocket(1024); // port# : 1024
// byte[] receiveData = new byte[1024];
// byte[] sendData = new byte[1024];
// System.out.println("Listening...");
byte[] result = sample();
hexdump(result);
// while(true){
// DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
// serverSocket.receive(receivePacket);
// // double data = ByteBuffer.wrap(receiveData).getDouble();
// ByteBuffer bb = ByteBuffer.wrap(receivePacket.getData(), 0, receivePacket.getLength());
// bb.order(ByteOrder.LITTLE_ENDIAN);
// double data = bb.getDouble();
// System.out.println(data);
// InetSocketAddress answerAddr = new InetSocketAddress("localhost",10005);
// DatagramPacket sendPacket = new DatagramPacket(data, data.length, anwserAddr);
// serverSocket.send(sendPacket);
// }
serverSocket.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment