From 68fcd5c58675b95aa52ebdec0cebc16e0e2519cf Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Sat, 12 Mar 2005 18:04:02 +0000 Subject: [PATCH] better formatting --- .../src/net/hyperic/sigar/cmd/Netstat.java | 69 ++++++++++++++----- 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/bindings/java/src/net/hyperic/sigar/cmd/Netstat.java b/bindings/java/src/net/hyperic/sigar/cmd/Netstat.java index c6a34741..3cc8d41d 100644 --- a/bindings/java/src/net/hyperic/sigar/cmd/Netstat.java +++ b/bindings/java/src/net/hyperic/sigar/cmd/Netstat.java @@ -2,6 +2,7 @@ package net.hyperic.sigar.cmd; import java.net.InetAddress; import java.net.UnknownHostException; +import java.util.ArrayList; import net.hyperic.sigar.SigarException; import net.hyperic.sigar.NetConnection; @@ -13,14 +14,29 @@ import net.hyperic.sigar.NetPortMap; */ public class Netstat extends SigarCommandBase { + private static final int LADDR_LEN = 20; + private static final int RADDR_LEN = 35; + + private static final String OUTPUT_FORMAT = + "%-5s %-" + LADDR_LEN + "s %-" + RADDR_LEN + "s %s"; + + private static final String[] HEADER = new String[] { + "Proto", + "Local Address", + "Foreign Address", + "State" + }; + private static boolean isNumeric; public Netstat(Shell shell) { super(shell); + setOutputFormat(OUTPUT_FORMAT); } public Netstat() { super(); + setOutputFormat(OUTPUT_FORMAT); } protected boolean validateArgs(String[] args) { @@ -93,21 +109,32 @@ public class Netstat extends SigarCommandBase { return String.valueOf(port); } - private String formatAddress(String address) { + private String formatAddress(String proto, String ip, + long portnum, int max) { + + String port = formatPort(proto, portnum); + String address; + if (isNumeric) { - return address; + address = ip; } - if (address.equals("0.0.0.0")) { - return "*"; + else if (ip.equals("0.0.0.0")) { + address = "*"; + } + else { + try { + address = InetAddress.getByName(ip).getHostName(); + } catch (UnknownHostException e) { + address = ip; + } } - //advantage of InetAddress' lookup cache. - try { - InetAddress addr = InetAddress.getByName(address); - return addr.getHostName(); - } catch (UnknownHostException e) { - return address; + max -= port.length() + 1; + if (address.length() > max) { + address = address.substring(0, max); } + + return address + ":" + port; } //XXX currently weak sauce. should end up like netstat command. @@ -120,7 +147,7 @@ public class Netstat extends SigarCommandBase { } NetConnection[] connections = this.sigar.getNetConnectionList(flags); - println("Proto\tLocal Address\tForeign Address\tState"); + printf(HEADER); for (int i=0; i