• ByteUtil


    import java.io.ByteArrayInputStream;

    import java.io.ByteArrayOutputStream;

    import java.io.IOException;

    import java.io.ObjectInputStream;

    import java.io.ObjectOutputStream;

    import java.io.UnsupportedEncodingException;

    import java.nio.ByteBuffer;

    import org.slf4j.Logger;

    import org.slf4j.LoggerFactory;

    public class ByteUtil

    {

      private static final Logger LOGGER = LoggerFactory.getLogger(ByteUtil.class);

      public static String getString(ByteBuffer byteBuffer)

      {

        short len = byteBuffer.getShort();

        byte[] bb = new byte[len];

        byteBuffer.get(bb);

        String str = null;

        try {

          str = new String(bb, "UTF-8");

        } catch (UnsupportedEncodingException e) {

          LOGGER.error("读取字符串错误.");

        }

        return str;

      }

      public static byte[] mergeByteArray(byte[] bb1, byte[] bb2)

      {

        int len1 = bb1.length;

        int len2 = bb2.length;

        byte[] bb = new byte[len1 + len2];

        System.arraycopy(bb1, 0, bb, 0, len1);

        System.arraycopy(bb2, 0, bb, len1, len2);

        return bb;

      }

      public static byte[] booleanToBytes(boolean b)

      {

        byte[] bb = new byte[1];

        bb[0] = (byte)((b) ? 1 : 0);

        return bb;

      }

      public static byte[] byteToBytes(byte b)

      {

        byte[] bb = new byte[1];

        bb[0] = b;

        return bb;

      }

      public static byte[] shortToBytes(short s)

      {

        ByteBuffer byteBuffer = ByteBuffer.allocate(2);

        byteBuffer.putShort(s);

        byteBuffer.flip();

        byte[] bb = new byte[byteBuffer.limit()];

        byteBuffer.get(bb);

        return bb;

      }

      public static byte[] intToBytes(int i)

      {

        ByteBuffer byteBuffer = ByteBuffer.allocate(4);

        byteBuffer.putInt(i);

        byteBuffer.flip();

        byte[] bb = new byte[byteBuffer.limit()];

        byteBuffer.get(bb);

        return bb;

      }

      public static byte[] longToBytes(long l)

      {

        ByteBuffer byteBuffer = ByteBuffer.allocate(8);

        byteBuffer.putLong(l);

        byteBuffer.flip();

        byte[] bb = new byte[byteBuffer.limit()];

        byteBuffer.get(bb);

        return bb;

      }

      public static byte[] stringToBytes(String s)

      {

        byte[] bb = null;

        try {

          bb = s.getBytes("UTF-8");

        } catch (UnsupportedEncodingException e) {

          e.printStackTrace();

        }

        return bb;

      }

      public static <T> T byteArrayToObject(byte[] bb)

      {

        Object obj = null;

        ByteArrayInputStream bais = null;

        ObjectInputStream ois = null;

        try {

          bais = new ByteArrayInputStream(bb);

          ois = new ObjectInputStream(bais);

          obj = ois.readObject();

        } catch (Exception e) {

          e.printStackTrace(); } finally {

          try {

            if (ois != null) ois.close();  } catch (IOException e) {

            e.printStackTrace();

          }try { if (bais != null) bais.close();  } catch (IOException e) {

            e.printStackTrace();

          }

        }

        return obj;

      }

      public static byte[] objectToByteArray(Object obj)

      {

        byte[] bb = null;

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ObjectOutputStream oos = null;

        try {

          oos = new ObjectOutputStream(baos);

          oos.writeObject(obj);

          bb = baos.toByteArray();

        } catch (Exception e) {

          e.printStackTrace(); } finally {

          try {

            if (oos != null) oos.close();  } catch (IOException e) {

            e.printStackTrace();

          }try { if (baos != null) baos.close();  } catch (IOException e) {

            e.printStackTrace();

          }

        }

        return bb;

      }

    }

  • 相关阅读:
    NHibernate版本不一致问题
    .NET中AOP的几种实现方案
    转播
    看来不得不来谈谈这个首页精华区了
    事件与委托
    关于字符集和字符编码以及代码页的前前后后(续)
    让电脑像人脑一样思考,谁养鱼问题断言推理解法
    关于那个脑袋的很漂漂的图形的C#版本
    大家都有头像,我来测试下我的新头像。
    浅谈JavaScript中的对象和类型(上)
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10408001.html
Copyright © 2020-2023  润新知