下面列出了怎么用org.apache.commons.io.HexDump的API类实例代码及写法,或者点击链接到github查看源代码。
private int traceBeforeNegotiate() {
int beforeNumSubjectCreds = 0;
// Traces all credentials too.
if (subject != null) {
log.debug("[" + getName() + "] AUTH_NEGOTIATE as subject " + subject.toString());
beforeNumSubjectCreds = subject.getPrivateCredentials().size();
}
if (negotiationToken != null && negotiationToken.length > 0) {
try {
OutputStream os = new ByteArrayOutputStream();
HexDump.dump(negotiationToken, 0, os, 0);
log.debug("[" + getName() + "] AUTH_NEGOTIATE Process token from acceptor==>\n"
+ os.toString());
} catch (IOException e) {}
}
return beforeNumSubjectCreds;
}
private void traceAfterNegotiate( int beforeNumSubjectCreds ) {
if (subject != null) {
int afterNumSubjectCreds = subject.getPrivateCredentials().size();
if (afterNumSubjectCreds > beforeNumSubjectCreds) {
log.debug("[" + getName() + "] AUTH_NEGOTIATE have extra credentials.");
// Traces all credentials too.
log.debug("[" + getName() + "] AUTH_NEGOTIATE updated subject=" + subject.toString());
}
}
if (negotiationToken != null && negotiationToken.length > 0) {
try {
OutputStream os = new ByteArrayOutputStream();
HexDump.dump(negotiationToken, 0, os, 0);
log.debug("[" + getName() + "] AUTH_NEGOTIATE Send token to acceptor==>\n"
+ os.toString());
} catch (IOException e) {}
}
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
logger.info("Sending headers: " + request.getHeaders());
if (body.length > 0) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
HexDump.dump(body, 0, baos, 0);
logger.info("Sending to [{}]: \n{}", request.getURI(), baos.toString(Charsets.UTF_8.name()).trim());
} else {
logger.info("Sending empty body to [{}]!", request.getURI());
}
return execution.execute(request, body);
}
public static <T extends TLObject> void dump(T object, byte[] serialized) {
try {
String path = getFilePath(object.getClass());
FileUtils.writeStringToFile(new File(dumpDir + path + ".json"), toJson(object), Charset.forName("UTF-8"));
FileUtils.writeStringToFile(new File(dumpDir + path + ".dump"), StreamUtils.toHexString(serialized), Charset.forName("UTF-8"));
HexDump.dump(serialized, 0, new FileOutputStream(dumpDir + path + ".dump2"), 0); // More friendly dump
} catch (IOException e) {
e.printStackTrace();
}
}
public static String hexDump(byte[] bytes) {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
try {
HexDump.dump(bytes, 0, buf, 0);
return buf.toString();
}
catch (Exception x) {
}
return "";
}
/**
* Interprets the message to create a string representation
*
* @param message
* The message to interpret
* @param displayHex
* Whether to display BytesMessages in hexdump style, ignored for simple text messages
* @return String representation of the message
*/
private String interpretMessage(Message<byte[]> message, boolean displayHex) throws IOException {
StringBuilder sb = new StringBuilder();
String properties = Arrays.toString(message.getProperties().entrySet().toArray());
String data;
byte[] msgData = message.getData();
ByteArrayOutputStream out = new ByteArrayOutputStream();
if (!displayHex) {
data = new String(msgData);
} else {
HexDump.dump(msgData, 0, out, 0);
data = new String(out.toByteArray());
}
String key = null;
if (message.hasKey()) {
key = message.getKey();
}
sb.append("key:[").append(key).append("], ");
sb.append("properties:").append(properties).append(", ");
sb.append("content:").append(data);
return sb.toString();
}
public static <T extends TLObject> void dump(T object, byte[] serialized) {
try {
String path = getFilePath(object.getClass());
FileUtils.writeStringToFile(new File(dumpDir + path + ".json"), toJson(object), Charset.forName("UTF-8"));
FileUtils.writeStringToFile(new File(dumpDir + path + ".dump"), StreamUtils.toHexString(serialized), Charset.forName("UTF-8"));
HexDump.dump(serialized, 0, new FileOutputStream(dumpDir + path + ".dump2"), 0); // More friendly dump
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
if (body.length > 0) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
HexDump.dump(body, 0, baos, 0);
logger.info("Sending to [{}]: \n{}", request.getURI(), baos.toString(Charsets.UTF_8.name()).trim());
} else {
logger.info("Sending empty body to [{}]!", request.getURI());
}
return execution.execute(request, body);
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
if (body.length > 0) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
HexDump.dump(body, 0, baos, 0);
logger.info("Sending to [{}]: \n{}", request.getURI(), baos.toString(Charsets.UTF_8.name()).trim());
} else {
logger.info("Sending empty body to [{}]!", request.getURI());
}
return execution.execute(request, body);
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
if (body.length > 0) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
HexDump.dump(body, 0, baos, 0);
logger.info("Sending to [{}]: \n{}", request.getURI(), baos.toString(Charsets.UTF_8.name()).trim());
} else {
logger.info("Sending empty body to [{}]!", request.getURI());
}
return execution.execute(request, body);
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
if (body.length > 0) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
HexDump.dump(body, 0, baos, 0);
logger.info("Sending to [{}]: \n{}", request.getURI(), baos.toString(Charsets.UTF_8.name()).trim());
} else {
logger.info("Sending empty body to [{}]!", request.getURI());
}
return execution.execute(request, body);
}
public static String dumpEvent(Event event, int maxBytes) {
StringBuilder buffer = new StringBuilder();
if (event == null || event.getBody() == null) {
buffer.append("null");
} else if (event.getBody().length == 0) {
// do nothing... in this case, HexDump.dump() will throw an exception
} else {
byte[] body = event.getBody();
byte[] data = Arrays.copyOf(body, Math.min(body.length, maxBytes));
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
HexDump.dump(data, 0, out, 0);
String hexDump = new String(out.toByteArray());
// remove offset since it's not relevant for such a small dataset
if(hexDump.startsWith(HEXDUMP_OFFSET)) {
hexDump = hexDump.substring(HEXDUMP_OFFSET.length());
}
buffer.append(hexDump);
} catch (Exception e) {
if(LOGGER.isInfoEnabled()) {
LOGGER.info("Exception while dumping event", e);
}
buffer.append("...Exception while dumping: ").append(e.getMessage());
}
String result = buffer.toString();
if(result.endsWith(EOL) && buffer.length() > EOL.length()) {
buffer.delete(buffer.length() - EOL.length(), buffer.length()).toString();
}
}
return "{ headers:" + event.getHeaders() + " body:" + buffer + " }";
}
/**
* 显示RDB字节内容以及格式化
*/
@Test
public void showRDBDumpData() throws Exception {
HexDump.dump(rdbData, 0, System.out, 0);
}
private static void dumpBytes(byte[] bytes) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
HexDump.dump(bytes, 0, baos, 0);
logger.info("Serialized object to: \n{}", baos.toString(Charsets.UTF_8.name()).trim());
}
private static void dumpToLog(MessageSerDe serDe, byte[] data) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
HexDump.dump(data, 0, baos, 0);
logger.info("Serialized object using [{}] to: \n{}", serDe.getMessageFormatName(), baos.toString(Charsets.UTF_8.name()).trim());
}