Complete transformation flow, part 1th

Source: Internet
Author: User
Tags filter

Typically, the Java I/O framework is very versatile. The same framework supports file access, network access, character conversion, compression and encryption, and so on. However, sometimes it is not very flexible. For example, compressed streams allow you to write data in a compressed format, but they do not allow you to read data in compressed format. Similarly, some third-party modules are built to write data without considering the circumstances in which the application needs to read the data. This article is the first part of a two-part series, the Java cryptography expert and author Merlin Hughes describes the framework for enabling applications to read data efficiently from sources that only support writing data to the output stream.

The Java platform has grown tremendously since the early browser-based applets and simple applications. Now, we have multiple platforms and profiles and a lot of new APIs, and there are almost hundreds of of them. Although the complexity of the Java language is increasing, it is still an excellent tool for everyday programming tasks. While you sometimes fall into the day-to-day programming problems, occasionally you can go back and find a great solution to deal with the problems you've encountered many times before.

Just a few days ago, I wanted to compress some data that was read over a network connection (I relayed TCP data to a UDP socket in compressed format). Remember that the Java platform supports compression since version 1.1, so I went directly to the Java.util.zip package, hoping to find a solution for me. However, I found a problem: the constructed classes all apply to the general case, that is, to decompress the data at read time and compress them when writing, there is no workaround. While it is possible to bypass I/O classes, I want to build a streaming solution rather than lazy to use the compression program directly.

Not long ago, I had the exact same problem in another situation. I have a base-64 transcoding library, which, like the use of a compressed package, supports decoding of the data read from the stream and encoding the data in the write stream. What I need, however, is a library that can encode when I read data from the stream.

As I proceeded to solve the problem, I realized that I had also encountered the problem in another case: when serializing an XML document, it is usually a loop through the entire document and writes the node to the stream. However, I encountered a situation where I needed to read a serialized document to resolve the subset back into a new document.

Looking back, I realized that these isolated events represent a common problem: if there is a data source that writes data incrementally to the output stream, then I need an input stream that allows me to read the data and transparently access the data source whenever more data is needed.

In this article, we'll look at three possible solutions to this problem, while deciding on a new framework for achieving the best solution. We will then examine the framework for each of the issues listed above. We'll talk briefly about performance issues and leave a lot of discussion about this to the next article.

I/O Flow basics

First, let's take a quick look at the basic stream classes in the Java platform, as shown in Figure 1. OutputStream represents the stream to which data is written. Typically, the stream is connected directly to a device such as a file or network connection, or to another output stream (in which case it is called a filter). Typically, the output stream filter writes the converted data to the connected stream after converting the data written to it. InputStream represents a stream from which data can be read. Similarly, the stream is directly connected to a device or to another stream. The input stream filter reads the data from the connected stream, converts the data, and then allows the converted data to be read from.

Figure 1. I/O Flow basics

As for my initial question, the Gzipoutputstream class is an output stream filter that compresses the data written to it, and then writes the compressed data to the connected stream. The input stream filter I need should be able to read data from the stream, compress the data, and then let me read the results.

Java platform, version 1.4 has introduced a new I/O framework Java.nio. However, the framework is largely related to providing effective access to operating system I/O resources, and although it does provide similar functionality for some traditional java.io classes and can represent dual-use resources that support both input and output, it does not completely replace the standard streaming class. And can't deal directly with the problems I need to solve.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.