java的InputStream的类型

本文详细介绍了Java中InputStream类及其子类的功能与用途,包括字节数组、字符串、文件等不同来源的数据输入处理方式,并探讨了如何利用FilterInputStream增强其功能。

 

 

InputStream的作用是标志那些从不同起源地产生输入的类。这些起源地包括(每个都有一个相关的InputStream子类):
(1) 字节数组
(2) String对象
(3) 文件
(4) “管道”,它的工作原理与现实生活中的管道类似:将一些东西置入一端,它们在另一端出来。 (5) 一系列其他流,以便我们将其统一收集到单独一个流内。
(6) 其他起源地,如Internet连接等(将在本书后面的部分讲述)。

除此以外,FilterInputStream也属于InputStream的一种类型,用它可为“破坏器”类提供一个基础类,以便将属性或者有用的接口同输入流连接到一起。这将在以后讨论。

Class

Function

Constructor Arguments

How to use it

ByteArray-InputStream

Allows a buffer in memory to be used as an InputStream.

The buffer from which to extract the bytes.

As a source of data. Connect it to a FilterInputStream object to provide a useful interface.

StringBuffer-InputStream

Converts a String into an InputStream.

A String. The underlying implementation actually uses a StringBuffer.

As a source of data. Connect it to a FilterInputStream object to provide a useful interface.

File-InputStream

For reading information from a file.

A String representing the file name, or a File or FileDescriptor object.

As a source of data. Connect it to a FilterInputStream object to provide a useful interface.


类 功能 构建器参数/如何使用

ByteArrayInputStream 允许内存中的一个缓冲区作为InputStream使用 从中提取字节的缓冲区/作为一个数据源使用。通过将其同一个FilterInputStream对象连接,可提供一个有用的接口
StringBufferInputStream 将一个String转换成InputStream 一个String(字串)。基础的实施方案实际采用一个StringBuffer(字串缓冲)/作为一个数据源使用。通过将其同一个FilterInputStream对象连接,可提供一个有用的接口
FileInputStream 用于从文件读取信息 代表文件名的一个String,或者一个File或FileDescriptor对象/作为一个数据源使用。通过将其同一个FilterInputStream对象连接,可提供一个有用的接口

Piped-InputStream

Produces the data that’s being written to the associated PipedOutput-Stream. Implements the “piping” concept.

PipedOutputStream

As a source of data in multithreading. Connect it to a FilterInputStream object to provide a useful interface.

Sequence-InputStream

Coverts two or more InputStream objects into a single InputStream.

Two InputStream objects or an Enumeration for a container of InputStream objects.

As a source of data. Connect it to a FilterInputStream object to provide a useful interface.

Filter-InputStream

Abstract class which is an interface for decorators that provide useful functionality to the other InputStream classes. See Table 10-3.

See Table 10-3.

See Table 10-3.


PipedInputString 产生为相关的PipedOutputStream写的数据。实现了“管道化”的概念 PipedOutputStream/作为一个数据源使用。通过将其同一个FilterInputStream对象连接,可提供一个有用的接口
SequenceInputStream 将两个或更多的InputStream对象转换成单个InputStream使用 两个InputStream对象或者一个Enumeration,用于InputStream对象的一个容器/作为一个数据源使用。通过将其同一个FilterInputStream对象连接,可提供一个有用的接口
FilterInputStream 对作为破坏器接口使用的类进行抽象;那个破坏器为其他InputStream类提供了有用的功能。参见表10.3
### 使用JavaInputStream确定文件类型的示例 在Java中,可以通过读取文件头(即文件的前几个字节)来判断文件类型。这些字节通常被称为“魔数”,不同的文件格式具有特定的魔数值。下面是一个基于`InputStream`实现的简单方法: #### 方法描述 通过创建一个自定义函数,该函数接受`InputStream`作为参数并返回文件类型字符串。此过程涉及读取输入流中的前几个字节并与已知的文件签名进行比较。 以下是具体的代码示例: ```java import java.io.InputStream; import java.io.IOException; public class FileTypeDetector { public static String detectFileType(InputStream inputStream) throws IOException { if (inputStream == null) { throw new IllegalArgumentException("Input stream cannot be null"); } byte[] header = new byte[4]; // Read first 4 bytes as most signatures are within this range. int bytesRead = inputStream.read(header, 0, header.length); if (bytesRead < 2) { // Minimum signature length is typically 2 bytes. return "Unknown"; } StringBuilder hexBuilder = new StringBuilder(); for (int i = 0; i < bytesRead; i++) { hexBuilder.append(String.format("%02X", header[i])); } String hexString = hexBuilder.toString(); switch (hexString.toUpperCase()) { case "89504E47": // PNG Signature return "PNG Image File"; case "FFD8FFE0": // JPEG Signature with EXIF metadata case "FFD8FFDB": case "FFD8FFEE": case "FFD8FFE1": return "JPEG Image File"; case "47494638": // GIF Signature return "GIF Image File"; case "504B0304": // ZIP or JAR file return "ZIP/JAR Archive"; default: return "Unknown"; // If no match found } } public static void main(String[] args) { try (InputStream inputStream = FileTypeDetector.class.getResourceAsStream("/example.png")) { if (inputStream != null) { System.out.println(detectFileType(inputStream)); } else { System.out.println("File not found."); } } catch (IOException e) { e.printStackTrace(); } } } ``` #### 关键点说明 - **文件头检测**:上述代码通过读取文件的前四个字节并将它们转换为十六进制表示形式来进行匹配[^1]。 - **异常处理**:如果无法读取足够的字节,则默认返回“未知”类型。 - **资源管理**:使用`try-with-resources`语句自动关闭`InputStream`,从而避免资源泄漏。 这种方法适用于大多数常见的二进制文件格式,但对于某些复杂的文档格式可能需要更深入的解析逻辑。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值