123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- /*
- * $Header: Util.java, 14-Jul-2005 03:27:51 luisantonioa Exp $
- *
- * $Author: luisantonioa $
- * $Date: 14-Jul-2005 03:27:51 $
- * $Revision: 1 $
- * $Log: Util.java,v $
- * Revision 1 14-Jul-2005 03:27:51 luisantonioa
- * Added copyright notice
- *
- *
- * This program is free software: you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later
- * version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package net.sf.l2j.util;
- import java.net.UnknownHostException;
- import java.nio.ByteBuffer;
- import javolution.text.TextBuilder;
- /**
- * This class ...
- *
- * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
- */
- public class Util
- {
- public static boolean isInternalIP(String ipAddress)
- {
- java.net.InetAddress addr = null;
- try
- {
- addr = java.net.InetAddress.getByName(ipAddress);
- }
- catch (UnknownHostException e)
- {
- e.printStackTrace();
- }
- return addr.isSiteLocalAddress() || addr.isLoopbackAddress();
- }
- public static String printData(byte[] data, int len)
- {
- TextBuilder result = new TextBuilder();
- int counter = 0;
- for (int i=0;i< len;i++)
- {
- if (counter % 16 == 0)
- {
- result.append(fillHex(i,4)+": ");
- }
- result.append(fillHex(data[i] & 0xff, 2) + " ");
- counter++;
- if (counter == 16)
- {
- result.append(" ");
- int charpoint = i-15;
- for (int a=0; a<16;a++)
- {
- int t1 = data[charpoint++];
- if (t1 > 0x1f && t1 < 0x80)
- {
- result.append((char)t1);
- }
- else
- {
- result.append('.');
- }
- }
- result.append("\n");
- counter = 0;
- }
- }
- int rest = data.length % 16;
- if (rest > 0 )
- {
- for (int i=0; i<17-rest;i++ )
- {
- result.append(" ");
- }
- int charpoint = data.length-rest;
- for (int a=0; a<rest;a++)
- {
- int t1 = data[charpoint++];
- if (t1 > 0x1f && t1 < 0x80)
- {
- result.append((char)t1);
- }
- else
- {
- result.append('.');
- }
- }
- result.append("\n");
- }
- return result.toString();
- }
- public static String fillHex(int data, int digits)
- {
- String number = Integer.toHexString(data);
- for (int i=number.length(); i< digits; i++)
- {
- number = "0" + number;
- }
- return number;
- }
- /**
- * @param raw
- * @return
- */
- public static String printData(byte[] raw)
- {
- return printData(raw, raw.length);
- }
-
- public static String printData(ByteBuffer buf)
- {
- byte[] data = new byte[buf.remaining()];
- buf.get(data);
- String hex = Util.printData(data, data.length);
- buf.position(buf.position() - data.length);
- return hex;
- }
-
- }
|