📄 streamutils.java
字号:
/* The Bluetooth Library for client-server communication Copyright (C) 2006 Martin Vysny 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 2 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. */package net.sf.btw.tools;import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.EOFException;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;/** * Utilities for working with streams. * * @author Martin Vysny */public final class StreamUtils { private StreamUtils() { // hidden } /** * Writes the byte array to the output stream. * * @param bytes * the bytes to write. Handles <code>null</code> array * correctly. * @param out * writes to this stream. * @throws IOException * if write fails. */ public static void writeByteArray(final byte[] bytes, final OutputStream out) throws IOException { writePackedInt(bytes == null ? -1 : bytes.length, out); if (bytes != null) out.write(bytes); } /** * Reads the byte array from the input stream. The array must have been * written by {@link #writeByteArray(byte[], OutputStream)}. * * @param in * reads from this stream. * @throws IOException * if read fails. * @return byte array, may be <code>null</code> if a null array was * written. */ public static byte[] readByteArray(final InputStream in) throws IOException { final int length = readPackedInt(in); if (length < 0) return null; final byte[] result = new byte[length]; if (length == 0) return result; if (in.read(result) < length) throw new EOFException("Packet.readByteArray()"); //$NON-NLS-1$ return result; } /** * Writes the number in a packed format. The number may occupy 1-5 bytes * depending on its value. * * @param number * the number to write * @param out * the output stream. * @throws IOException */ public static void writePackedInt(final int number, OutputStream out) throws IOException { int numberToWrite = number; final boolean negative = (numberToWrite < 0); if (negative) numberToWrite = ~numberToWrite; byte dataBits = 6; int dataMask = 0x3F; do { int byteToWrite = (numberToWrite & dataMask); if ((dataBits == 6) && negative) byteToWrite |= 0x40; numberToWrite >>= dataBits; if (numberToWrite != 0) byteToWrite |= 0x80; dataBits = 7; dataMask = 0x7f; out.write(byteToWrite); } while (numberToWrite != 0); } /** * Reads the number in a packed format. The number may occupy 1-5 bytes * depending on its value. * * @param in * the input stream. * @return the number. * @throws IOException */ public static int readPackedInt(InputStream in) throws IOException { int result = 0; boolean negative = false; byte currentBit = 0; int byteRead; do { byteRead = in.read(); if (byteRead < 0) throw new EOFException("StreamUtils.readPackedInt(): EOF"); //$NON-NLS-1$ if (currentBit == 0) { negative = (byteRead & 0x40) != 0; byteRead &= ~0x40; } int value = (byteRead & 0x7f) << currentBit; result += value; if (currentBit == 0) currentBit = 6; else currentBit += 7; } while (byteRead >= 0x80); if (negative) result = ~result; return result; } /** * Writes given string to data output stream as a string. This method is * incompatible with {@link String#getBytes()}; use * {@link DataInputStream#readUTF()} instead. * * @param string * string to convert. Cannot be <code>null</code>. * @return array of bytes. */ public static byte[] stringToByteArray(final String string) { final ByteArrayOutputStream out = new ByteArrayOutputStream(string .length()); final DataOutputStream dataOut = new DataOutputStream(out); try { dataOut.writeUTF(string); dataOut.flush(); } catch (IOException e) { // shouldn't happen... Logger.error(null, e); throw new Error(); } return out.toByteArray(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -