获取文件的真实媒体类型。目前只支持JPG, GIF, PNG, BMP四种图片文件。

@param bytes 文件字节流
@return 媒体类型(MEME-TYPE)
354426
public static String getMimeType(byte[] bytes) {
        String suffix = getFileSuffix(bytes);
        String mimeType;

        if ("JPG".equals(suffix)) {
            mimeType = "image/jpeg";
        } else if ("GIF".equals(suffix)) {
            mimeType = "image/gif";
        } else if ("PNG".equals(suffix)) {
            mimeType = "image/png";
        } else if ("BMP".equals(suffix)) {
            mimeType = "image/bmp";
        } else {
            mimeType = "application/octet-stream";
        }

        return mimeType;
    }
422804
public static String encodeBytes(byte[] bytes)
    {
        String ret = null;

        try
        {
            // Obfuscate the string
            if(bytes != null)
                ret = new String(Base64.encodeBase64(bytes));
        }
        catch(NoClassDefFoundError e)
        {
            ret = new String(bytes);
            System.out.println("WARNING: unable to encode: "
                +e.getClass().getName()+": "+e.getMessage());
        }

        return ret;
    }
724861
public static String encodeHexString(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
1345592
public static OutputStream bytes2OutputStream(final byte[] bytes) {
        if (bytes == null || bytes.length <= 0) return null;
        ByteArrayOutputStream os = null;
        try {
            os = new ByteArrayOutputStream();
            os.write(bytes);
            return os;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            IOKit.closeQuietly(os);
        }
    }
1178065
public byte[] getImageBytes(BufferedImage image) {
		byte[] bytes = null;
		try {
			bytes = ImageUtils.writeImageToBytes(image,
					ImageUtils.IMAGE_FORMAT_PNG);
		} catch (IOException e) {
			throw new GeoPackageException("Failed to write image to "
					+ ImageUtils.IMAGE_FORMAT_PNG + " bytes", e);
		}
		return bytes;
	}
627170
public static String bytes2String(byte[] bytes) {
    try {
      final int len = bytes.length;
      char[] charArray = UTF8.getCharArray(len);
      for (int i = 0; i < bytes.length; i++) {
        if (bytes[i] < UTF8.MIN_ASCII_CODE) {
          // non-ASCII codepoints' higher bytes
          // are of the form (10xxxxxx), hence the bytes
          // represent a non-ASCII string
          // do expensive conversion
          return new String(bytes, utf8charsetName);
        }
        // copy to temporary array
        charArray[i] = (char) bytes[i];
      }
      // only ASCII bytes, do fast conversion
      // using bytes as actual characters
      return new String(charArray, 0, len);
    } catch (UnsupportedEncodingException e) {
      assert false : "UTF8 encoding is not supported ";
    }
    return null;
  }
1266920
public static String deflate(final byte[] bytes) {
        val data = new String(bytes, StandardCharsets.UTF_8);
        return deflate(data);
    }
71774
public static String getEscaped(byte... bytes) {
        final StringBuilder bld = new StringBuilder();
        for (byte b : bytes) {
            bld.append("\\").append(Hex.get(b));
        }
        return bld.toString();
    }
1169116
public static String sum(byte[] bytes) {
    String result = "";
    for (byte b : bytes) {
      result += Integer.toString((b & 0xff) + 0x100, 16).substring(1);
    }
    return result;
  }
993988
public static String encodeHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_CHARS[v >>> 4];
            hexChars[j * 2 + 1] = HEX_CHARS[v & 0x0F];
        }
        return new String(hexChars);
    }
970365
public static String bytes2HexString(byte[] bytes) {
        if (bytes == null) return null;
        int len = bytes.length;
        if (len <= 0) return null;
        char[] ret = new char[len << 1];
        for (int i = 0, j = 0; i < len; i++) {
            ret[j++] = HEX_DIGITS[bytes[i] >>> 4 & 0x0f];
            ret[j++] = HEX_DIGITS[bytes[i] & 0x0f];
        }
        return new String(ret).toLowerCase();
    }
629496
private int bytesToInt(byte[] bytes)
    {
        int result = -1;
        if (bytes.length >= 4)
        {
            result = ((bytes[0]&0xff) << 24) +
                     ((bytes[1]&0xff) << 16) +
                     ((bytes[2]&0xff) << 8) +
                     (bytes[3]&0xff);
        }
        return result;
    }
682019
public static String bcdToStr(byte[] bytes) {
		char temp[] = new char[bytes.length * 2], val;

		for (int i = 0; i < bytes.length; i++) {
			val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);
			temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0');

			val = (char) (bytes[i] & 0x0f);
			temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0');
		}
		return new String(temp);
	}
812260
public String getString(ByteBuffer bytes)
    {
        TypeSerializer<T> serializer = getSerializer();
        serializer.validate(bytes);

        return serializer.toString(serializer.deserialize(bytes));
    }
784095
private String format(final long bytes) {
        if (bytes > MB) {
            return String.valueOf(bytes / MB) + " MB";
        }
        return String.valueOf(bytes / KB) + " KB";
    }
372188
public static String string(byte[] bytes) {
    if (bytes == null) {
      return null;
    }
    try {
      return new String(bytes, ENCODING);
    } catch (UnsupportedEncodingException e) {
      log.error("UnsupportedEncodingException ", e);
      throw new RuntimeException(e);
    }
  }
1074541
public static String encode(byte[] bytes) {
		int bytesLength = bytes.length;
		int base64Length = (bytesLength * 4 + 2) / 3; // base64 encoded string length, without padding

		StringBuilder base64Builder = new StringBuilder();
		int bytesIndex = 0;

		while (bytesIndex < bytesLength) {
			int i0 = bytes[bytesIndex++] & 0xff;
			int i1 = bytesIndex < bytesLength ? bytes[bytesIndex++] & 0xff : 0;
			int i2 = bytesIndex < bytesLength ? bytes[bytesIndex++] & 0xff : 0;

			int o0 = i0 >>> 2;
			int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
			int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
			int o3 = i2 & 0x3F;

			base64Builder.append(NIBLES_2_CHARS[o0]);
			base64Builder.append(NIBLES_2_CHARS[o1]);
			base64Builder.append(base64Builder.length() < base64Length ? NIBLES_2_CHARS[o2] : '=');
			base64Builder.append(base64Builder.length() < base64Length ? NIBLES_2_CHARS[o3] : '=');
		}

		return base64Builder.toString();
	}