java源码之Arrays.toString()和Arrays.deepToString()

 

/**
 * 数组转化成字符串
 * <p>
 * Returns a string representation of the contents of the specified array.
 * If the array contains other arrays as elements, they are converted to
 * strings by the {@link Object#toString} method inherited from
 * <tt>Object</tt>, which describes their <i>identities</i> rather than
 * their contents.
 * <p>
 * 返回指定数组的字符串,
 * 如果数组包含其他数组作为元素,那么这些元素将执行继承自Object的toString方法,getClass().getName() + "@" Integer.toHexString(hashCode())的方式,而不是它们的内容。
 *
 * <p>
 * The value returned by this method is equal to the value that would
 * be returned by <tt>Arrays.asList(a).toString()</tt>, unless <tt>a</tt>
 * is <tt>null</tt>, in which case <tt>"null"</tt> is returned.
 * <p>
 * 该方法的返回值将和Arrays.asList(a).toString()方法返回值相同,除非a==null,这时方法的返回值是字符串"null",而后者返回null.
 *
 * @param a the array whose string representation to return
 * @return a string representation of <tt>a</tt>
 * @see #deepToString(Object[])
 * @since 1.5
 */
public static String toString(Object[] a) {
    if (a == null)
        return "null";

    // 不用length==0来判断空数组,因为下面的代码需要用到length-1
    int iMax = a.length - 1;
    if (iMax ==
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java进行网络抓包的示例代码: ```java import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; import java.util.Arrays; import java.util.Iterator; public class NetworkCapture { private Selector selector; public NetworkCapture() throws IOException { this.selector = Selector.open(); } public void capture(String hostname, int port) throws UnknownHostException, IOException { InetAddress address = InetAddress.getByName(hostname); SocketChannel channel = SocketChannel.open(); channel.configureBlocking(false); channel.connect(address, port); channel.register(selector, SelectionKey.OP_CONNECT); while (selector.select() > 0) { Iterator<SelectionKey> iterator = selector.selectedKeys().iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); iterator.remove(); if (key.isConnectable()) { SocketChannel sc = (SocketChannel) key.channel(); if (sc.finishConnect()) { key.interestOps(SelectionKey.OP_READ); } } else if (key.isReadable()) { SocketChannel sc = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int bytesRead = sc.read(buffer); if (bytesRead == -1) { key.cancel(); sc.close(); break; } buffer.flip(); byte[] bytes = new byte[buffer.limit()]; buffer.get(bytes); System.out.println(Arrays.toString(bytes)); } } } } public static void main(String[] args) throws UnknownHostException, IOException { NetworkCapture capture = new NetworkCapture(); capture.capture("www.google.com", 80); } } ``` 这个示例代码使用Java NIO实现了网络抓包。它连接到指定的主机和端口,并读取所有传入的数据。在读取到数据时,它将字节数组打印到控制台上。你可以根据需要修改这个示例代码,以适应你的特定需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值