hexdump sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -*- 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