A new i/o library: a good idea or a really bad one?

For a long time I worked almost exclusively with input/output streams in java. However a few years ago I started exploring async I/O as well (e.g. running on jboss netty).

However when working in the async I/O world, I could not reuse any of my sync utilities. I also had to start on a rather large library of new I/O utilities and wasn’t sure whether to aim for blocking or non-blocking.

As things go…I wrote an API on top of the existing I/O which supports both blocking and non-blocking and the utilities are written against that.

However because I/O is such a pervasive thing, I am unsure whether it is a wise thing to do. As an example take a Base64 encoder. Yes apache has a sync I/O version and yes java 8 finally has an async encoder. But I needed an encoder that could do both.

Same for quoted-printable, deflate,… and of course it should work for sockets (regular & ssl), files,… in single-threaded and multithreaded situations,… I needed utilities to chain together I/O sources, use delimiter-based or fixed-length I/O wrappers,… I needed protocol handlers for http, ftp,…

Many of these problems can be expressed in a blocking-neutral fashion or -given enough thought- could support both async and sync, allowing wide reusability.

In short: is it wise to introduce an abstraction on top of I/O, forcing basically every library that uses it (directly or indirectly) to use it as well?

UPDATE

At the core are the ReadableByteContainer and WritableByteContainer that -to the discerning eye- will look like a cross between stream I/O and channel I/O.

public interface ReadableByteContainer extends Closeable {
    public int read(byte [] bytes);
    public int read(byte [] bytes, int offset, int length);
}

public interface WritableByteContainer extends Closeable {
    public int write(byte [] bytes);
    public int write(byte [] bytes, int offset, int length);
    public void flush();
}

One of the utils built on it is the “Transcoder” api:

public interface ByteTranscoder {
    public void transcode(ReadableByteContainer in, WritableByteContainer out);
    public void flush(WritableByteContainer out);
}

which allows you to do something like:

ByteContainer container = IOUtils.newByteContainer();
container = IOUtils.wrap(
    container,
    TranscoderUtils.wrapOutput(container, new Base64Encoder())
);
container.write("test".getBytes());
System.out.println(new String(IOUtils.toBytes(container)));

This code will output the base64 encoded bytes of “test”.

The thing is, the Base64Encoder is capable of encoding the data in an asynchronous manner, it won’t block if the output container reports that no data was written nor will it block if the input container reports that no data is available. You can continue the transcoding by calling “transcode()” again.

This works both in a jboss netty context where you get the content piece by asynchronous piece and in the streaming world where everything blocks until it can read/write.

5

Perhaps an interesting follow-up to this is the actual library that has recently been added to github: https://github.com/nablex/utils-io

Note that the library has gone through an extensive rewrite since this question was asked.

Other libraries based on this one:

  • https://github.com/nablex/utils-codec : adds encoding/decoding for base64, deflate, gzip & quoted printable
  • https://github.com/nablex/utils-mime : uses the other libraries to perform low-level mime & s/mime parsing and formatting. As a proof of concept I also wrote a lightweight HTTP client on top of it that might make it to github at some point.

I think you’re abstracting the wrong thing. Your library does not need to actually handle the I/O. You already have libraries in hand that do synchronous or asynchronous I/O. Rewriting those libraries and merging them together is not abstraction. In fact, it might be a violation of the Single Responsibility Principle.

Why not simply write a library that provides a uniform interface to those libraries you mentioned? Underneath the covers, it is simply calling one library or the other. That’s true abstraction.

Just my two cents.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

A new i/o library: a good idea or a really bad one?

For a long time I worked almost exclusively with input/output streams in java. However a few years ago I started exploring async I/O as well (e.g. running on jboss netty).

However when working in the async I/O world, I could not reuse any of my sync utilities. I also had to start on a rather large library of new I/O utilities and wasn’t sure whether to aim for blocking or non-blocking.

As things go…I wrote an API on top of the existing I/O which supports both blocking and non-blocking and the utilities are written against that.

However because I/O is such a pervasive thing, I am unsure whether it is a wise thing to do. As an example take a Base64 encoder. Yes apache has a sync I/O version and yes java 8 finally has an async encoder. But I needed an encoder that could do both.

Same for quoted-printable, deflate,… and of course it should work for sockets (regular & ssl), files,… in single-threaded and multithreaded situations,… I needed utilities to chain together I/O sources, use delimiter-based or fixed-length I/O wrappers,… I needed protocol handlers for http, ftp,…

Many of these problems can be expressed in a blocking-neutral fashion or -given enough thought- could support both async and sync, allowing wide reusability.

In short: is it wise to introduce an abstraction on top of I/O, forcing basically every library that uses it (directly or indirectly) to use it as well?

UPDATE

At the core are the ReadableByteContainer and WritableByteContainer that -to the discerning eye- will look like a cross between stream I/O and channel I/O.

public interface ReadableByteContainer extends Closeable {
    public int read(byte [] bytes);
    public int read(byte [] bytes, int offset, int length);
}

public interface WritableByteContainer extends Closeable {
    public int write(byte [] bytes);
    public int write(byte [] bytes, int offset, int length);
    public void flush();
}

One of the utils built on it is the “Transcoder” api:

public interface ByteTranscoder {
    public void transcode(ReadableByteContainer in, WritableByteContainer out);
    public void flush(WritableByteContainer out);
}

which allows you to do something like:

ByteContainer container = IOUtils.newByteContainer();
container = IOUtils.wrap(
    container,
    TranscoderUtils.wrapOutput(container, new Base64Encoder())
);
container.write("test".getBytes());
System.out.println(new String(IOUtils.toBytes(container)));

This code will output the base64 encoded bytes of “test”.

The thing is, the Base64Encoder is capable of encoding the data in an asynchronous manner, it won’t block if the output container reports that no data was written nor will it block if the input container reports that no data is available. You can continue the transcoding by calling “transcode()” again.

This works both in a jboss netty context where you get the content piece by asynchronous piece and in the streaming world where everything blocks until it can read/write.

5

Perhaps an interesting follow-up to this is the actual library that has recently been added to github: https://github.com/nablex/utils-io

Note that the library has gone through an extensive rewrite since this question was asked.

Other libraries based on this one:

  • https://github.com/nablex/utils-codec : adds encoding/decoding for base64, deflate, gzip & quoted printable
  • https://github.com/nablex/utils-mime : uses the other libraries to perform low-level mime & s/mime parsing and formatting. As a proof of concept I also wrote a lightweight HTTP client on top of it that might make it to github at some point.

I think you’re abstracting the wrong thing. Your library does not need to actually handle the I/O. You already have libraries in hand that do synchronous or asynchronous I/O. Rewriting those libraries and merging them together is not abstraction. In fact, it might be a violation of the Single Responsibility Principle.

Why not simply write a library that provides a uniform interface to those libraries you mentioned? Underneath the covers, it is simply calling one library or the other. That’s true abstraction.

Just my two cents.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật