国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

java 流與 byte[] 的互轉(zhuǎn)操作

瀏覽:26日期:2022-08-23 11:00:56

1. InputStream -> byte[]

引入 apache.commons.is 包

import org.apache.commons.io.IOUtils;

byte[] bytes = IOUtils.toByteArray(inputStream);

2. byte[] -> InputStream

InputStream inputStream = new ByteArrayInputStream(bytes);

補(bǔ)充知識(shí):byte[]與各種數(shù)據(jù)類型互相轉(zhuǎn)換示例

在socket開發(fā)過程中,通常需要將一些具體的值(這些值可能是各種JAVA類型)轉(zhuǎn)化為byte[]類型,為此我總結(jié)了如下這個(gè)示例,貼出來,以便經(jīng)常翻看

public class TestCase { /** * short到字節(jié)數(shù)組的轉(zhuǎn)換. */ public static byte[] shortToByte(short number) { int temp = number; byte[] b = new byte[2]; for (int i = 0; i < b.length; i++) { b[i] = new Integer(temp & 0xff).byteValue();// 將最低位保存在最低位 temp = temp >> 8;// 向右移8位 } return b; } /** * 字節(jié)數(shù)組到short的轉(zhuǎn)換. */ public static short byteToShort(byte[] b) { short s = 0; short s0 = (short) (b[0] & 0xff);// 最低位 short s1 = (short) (b[1] & 0xff); s1 <<= 8; s = (short) (s0 | s1); return s; } /** * int到字節(jié)數(shù)組的轉(zhuǎn)換. */ public static byte[] intToByte(int number) { int temp = number; byte[] b = new byte[4]; for (int i = 0; i < b.length; i++) { b[i] = new Integer(temp & 0xff).byteValue();// 將最低位保存在最低位 temp = temp >> 8;// 向右移8位 } return b; } /** * 字節(jié)數(shù)組到int的轉(zhuǎn)換. */ public static int byteToInt(byte[] b) { int s = 0; int s0 = b[0] & 0xff;// 最低位 int s1 = b[1] & 0xff; int s2 = b[2] & 0xff; int s3 = b[3] & 0xff; s3 <<= 24; s2 <<= 16; s1 <<= 8; s = s0 | s1 | s2 | s3; return s; } /** * long類型轉(zhuǎn)成byte數(shù)組 */ public static byte[] longToByte(long number) { long temp = number; byte[] b = new byte[8]; for (int i = 0; i < b.length; i++) { b[i] = new Long(temp & 0xff).byteValue();// 將最低位保存在最低位 temp = temp // >> 8;// 向右移8位 } return b; } /** * 字節(jié)數(shù)組到long的轉(zhuǎn)換. */ public static long byteToLong(byte[] b) { long s = 0; long s0 = b[0] & 0xff;// 最低位 long s1 = b[1] & 0xff; long s2 = b[2] & 0xff; long s3 = b[3] & 0xff; long s4 = b[4] & 0xff;// 最低位 long s5 = b[5] & 0xff; long s6 = b[6] & 0xff; long s7 = b[7] & 0xff; // s0不變 s1 <<= 8; s2 <<= 16; s3 <<= 24; s4 <<= 8 * 4; s5 <<= 8 * 5; s6 <<= 8 * 6; s7 <<= 8 * 7; s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7; return s; } /** * double到字節(jié)數(shù)組的轉(zhuǎn)換. */ public static byte[] doubleToByte(double num) { byte[] b = new byte[8]; long l = Double.doubleToLongBits(num); for (int i = 0; i < 8; i++) {b[i] = new Long(l).byteValue();l = l >> 8; } return b; } /** * 字節(jié)數(shù)組到double的轉(zhuǎn)換. */ public static double getDouble(byte[] b) { long m; m = b[0]; m &= 0xff; m |= ((long) b[1] << 8); m &= 0xffff; m |= ((long) b[2] << 16); m &= 0xffffff; m |= ((long) b[3] << 24); m &= 0xffffffffl; m |= ((long) b[4] << 32); m &= 0xffffffffffl; m |= ((long) b[5] << 40); m &= 0xffffffffffffl; m |= ((long) b[6] << 48); m &= 0xffffffffffffffl; m |= ((long) b[7] << 56); return Double.longBitsToDouble(m); } /** * float到字節(jié)數(shù)組的轉(zhuǎn)換. */ public static void floatToByte(float x) { //先用 Float.floatToIntBits(f)轉(zhuǎn)換成int } /** * 字節(jié)數(shù)組到float的轉(zhuǎn)換. */ public static float getFloat(byte[] b) { // 4 bytes int accum = 0; for ( int shiftBy = 0; shiftBy < 4; shiftBy++ ) { accum |= (b[shiftBy] & 0xff) << shiftBy * 8; } return Float.intBitsToFloat(accum); } /** * char到字節(jié)數(shù)組的轉(zhuǎn)換. */ public static byte[] charToByte(char c){ byte[] b = new byte[2]; b[0] = (byte) ((c & 0xFF00) >> 8); b[1] = (byte) (c & 0xFF); return b; }/** * 字節(jié)數(shù)組到char的轉(zhuǎn)換. */ public static char byteToChar(byte[] b){ char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF)); return c; } /** * string到字節(jié)數(shù)組的轉(zhuǎn)換. */ public static byte[] stringToByte(String str) throws UnsupportedEncodingException{ return str.getBytes('GBK'); } /** * 字節(jié)數(shù)組到String的轉(zhuǎn)換. */ public static String bytesToString(byte[] str) { String keyword = null; try { keyword = new String(str,'GBK'); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return keyword; } /** * object到字節(jié)數(shù)組的轉(zhuǎn)換 */ @Test public void testObject2ByteArray() throws IOException, ClassNotFoundException { // Object obj = ''; Integer[] obj = { 1, 3, 4 }; // // object to bytearray ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream oo = new ObjectOutputStream(bo); oo.writeObject(obj); byte[] bytes = bo.toByteArray(); bo.close(); oo.close(); System.out.println(Arrays.toString(bytes)); Integer[] intArr = (Integer[]) testByteArray2Object(bytes); System.out.println(Arrays.asList(intArr)); byte[] b2 = intToByte(123); System.out.println(Arrays.toString(b2)); int a = byteToInt(b2); System.out.println(a); } /** * 字節(jié)數(shù)組到object的轉(zhuǎn)換. */ private Object testByteArray2Object(byte[] bytes) throws IOException, ClassNotFoundException { // byte[] bytes = null; Object obj; // bytearray to object ByteArrayInputStream bi = new ByteArrayInputStream(bytes); ObjectInputStream oi = new ObjectInputStream(bi); obj = oi.readObject(); bi.close(); oi.close(); System.out.println(obj); return obj; } }

以上這篇java 流與 byte[] 的互轉(zhuǎn)操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 亚洲国产2017男人a天堂 | 中文字幕一二三四区2021 | 成人性视频在线三级 | 国产精品成人自拍 | 国产黄色大片网站 | 欧美视频在线观 | 日本免费大黄在线观看 | 视频网18免费 | 精品三级在线观看 | 亚洲自拍图片区 | 成人影院vs一区二区 | 国产亚洲精品一区二区久久 | 特级一级毛片视频免费观看 | 亚州在线播放 | 国产一区精品在线观看 | 久久不见久久见免费影院www日本 | 韩国一区在线 | 欧美一及片 | 日本高清色www | 国产只有精品 | 手机在线看片福利 | 99je全部都是精品视频在线 | 亚洲综合色就色手机在线观看 | 国产成人综合亚洲欧美在 | 久草不卡视频 | 日本www免费视频网站在线观看 | 亚洲国产成人91精品 | 丁香五香天堂 | 亚洲精品影院久久久久久 | 国内成人精品亚洲日本语音 | 亚洲第一区在线 | 久久久久国产成人精品亚洲午夜 | 欧美视频一区二区三区四区 | 成人亚洲精品777777 | 日鲁夜鲁鲁狠狠综合视频 | 在线视频一区二区三区在线播放 | 亚洲精品久久久久久久福利 | 成年女人看片免费视频播放器 | 亚洲美女视频网站 | 日本一区二区三区在线 视频观看免费 | 视频三区精品中文字幕 |