Java library: creating, processing, working with files

Java is one of the pillars of the open source world. Each of his projects uses other open source applications, they are enough on the network and users do not need to “reinvent the wheel”. However, it often happens that developers need a special Java library (Library) for functionality, and they have no idea how to create it.

Key Design Principles

Code reuse is one of the holy grails of computer programming. Writing code that can be easily reused requires sophisticated skills, but it can certainly be mastered by anyone. The Java language can help create a good reusable platform. To do this, the developer needs to have basic programming knowledge, including the ability to create, compile, and execute simple programs from the command line.

A few key principles




A few key principles for a good Java library design:

  1. It should clearly convey its internal structure in such a way that the user knows how to use it, even if he does not understand how it works from the inside.
  2. Encapsulation - tightening and refining the code interface, which makes it more reliable and understandable.
  3. Extensibility - creating a Java library that performs its task well and provides the ability to fill in the missing parts to meet the needs of a particular developer.
  4. Inheritance - provides a way to configure the code without having to know all the details of the code that is being configured.
  5. Providing a way to get debugging information from code - helps users find their own mistakes.

Functionality Boundaries: In and Out

The problem is that not all designers of Java libraries think about their users, how the API will be applied in practice, and how the code will look and test.





The boundaries of functionality - this is perhaps the most important topic. Good practice is to pass all the dependencies through the constructors and their brevity with a few parameters. If you need a constructor with more than three or four parameters, then it is clear that the code needs to be reorganized.

Also, developers should always offer users more than one constructor. Clients must work with String and Integer so that they can pass a ByteArrayInputStream when testing modules.

For example, a few ways to create a Github API entry point using jcabi-github:

  • Github noauth = new RtGithub ();
  • Github basicauth = new RtGithub ("username", "password");
  • Github oauth = new RtGithub ("token").

As a second example of how to work with charles, a Java professional scanning web library, you can write the following.

Library scan




By default, templates should not be ignored. The indexPage variable is the URL from which crawling should begin; the driver is the browser used. By default, it cannot do anything, since it is not known which browser is installed on a running machine. The solution is needed mainly for the Library, which communicates with the outside world. Here you need to answer the question of how it will be processed.









To do this, write something like this:

WebCrawl graph = new GraphCrawl (...); List pages = graph.crawl ();

Interface and third parties

The developer should always use interfaces, and the user is obliged to interact with the code only through strict contracts. For example, in the jcabi-github library, the RtGithub si class is the only one that it sees.

Interface and third parties




The above snippet creates an application in the eugenp / tutorials repository. Repo and Issue instances apply, but the actual types are never expanded. The scenario above can be resolved, but then the developed algorithm will be polluted with a large amount of standard code.

Interfaces also provide ease of expansion and backward compatibility. On the one hand, developers are required to comply with already issued contracts, and on the other, the user expands the proposed interfaces: he can decorate them or write alternative implementations.

A good library is lightweight. The code should solve the problem and be functional. If you need a lot of dependencies. Probably, the developer is trying to cover too many functions and must break the project into several small ones.

The project should be as transparent as possible. The best example is to use SLF4J with an API for logging. You should not use log4j directly, perhaps the developer will want to use other logging tools.

The connection of Java document libraries that pass through the project transitively is performed so that dangerous dependencies, such as xalan or xml-apis, are not included.

There are hundreds of thousands of libraries in the world, but programmers only need to know a small number of the most functional modules.

Unit testing library

Unit testing library




Developers have very common log modules, they are needed in every project. Especially important for server applications, because the logs are placed only where you can see what is happening in them. Although the JDK comes with its own logging library, there are better alternatives, such as Log4j, SLF4j, and LogBack. A Java developer should be familiar with the pros and cons of log libraries and know why using SLF4j is better than plain Log4j.

In the modern world of web services and IoT, JSON has become a transition protocol for transferring information from a client to a server. He replaced XML as the most preferred way to pass information regardless of platform. Unfortunately, the JDK does not have a Library JSON. But there are many good third-party modules that allow you to analyze and create messages, for example, Jackson. A web developer must be familiar with at least one of these libraries.

Unit testing is the most important feature that separates the average developer from the professional. Programmers are often forgiven for not writing unit tests, and the most common excuse for not testing this is the lack of experience and knowledge of popular unit testing libraries, including JUnit, Mockito, and PowerMock.

There are several good third-party general-purpose libraries available to Java developers, such as Apache Commons and Google Guava. They simplify many tasks. As Joshua Bloch rightly said in Effective Java, there is no point in reinventing the wheel, it is better to use tried and tested modules instead of writing your own routines. For a Java developer, check out Google Guava and the Apache Commons library.

HTTP-enabled platform

Many developers don't like the lack of HTTP support in the JDK. You can establish an HTTP connection using the classes in the java.net package of third-party open source libraries such as Apache HttpClient and HttpCore.

Although JDK 9 provides HTTP 2.0 support and improved HTTP support, experts recommend that developers familiarize themselves with the popular HTTP client libraries, including HttpClient and HttpCore.

There are parsing libraries




XML parsing libraries exist, including Xerces, JAXB, JAXP, Dom4j, and Xstream. Xerces2 is the next generation of high-performance, fully compatible XML parsers in the Apache Xerces family. The new version of Xerces introduces its own Xerces Interface (XNI), a complete platform for creating parser components and configurations that is extremely modular and easy to program.

The Apache Xerces2 parser is the reference implementation of XNI, but other configuration components can be written using the Xerces native interface. Dom4j is another flexible XML structure for creating a Java library.

Real-world programs must interact with Microsoft Office in one form or another. Many of them must provide functionality for exporting data to Excel, and if you need to do this from a Java application, you will need the Apache POI API. This is a very rich library that allows you to read and write XLS files from a Java program.

If a programmer writes a wireframe that generates code or interacts with bytecodes, then he needs a bytecode library. It allows you to read and modify the bytecode generated by the application. Some of the popular Library bytes are javassist and Cglib Nodep. The Java programming assistant makes manipulating bytecode very simple. ASM is another useful module for editing it.

Ubiquitous PDF

Ubiquitous PDF




Like Microsoft Excel, PDF libraries are another ubiquitous format. If you need to maintain PDF functionality in an application, for example, export data to PDF files, then iText and Apache FOP modules can be used. Both provide useful PDF related functionality, but iText is better.

Prior to Java 8, the JDK data and time modules had flaws because they were not thread-oriented and were subject to errors. Java developers relied on JodaTime to implement date and time requirements. There is no reason to use Joda in JDK 8 because the user gets this functionality in the new date and time API, but for the old version of Java it is worth exploring this library.

Despite the fact that the JDK has a rich collection, there are third-party modules that provide more features, such as the collections of Apache Commons, Goldman Sachs, Google and Trove. Library Trove is especially useful as it provides high-speed regular and primitive collections for Java.

Javax.mail and Apache Commons Email provide an API for sending email with Java. It is built on top of the JavaMail API, which is designed to simplify work.

Cryptographic Codec Pack

Cryptographic codec pack




Apache Commons contains simple encoder and decoders for various formats, such as Base64 and Hexadecimal. In addition to these widely used encoders and decoders, it supports a set of phonetic encoding utilities. Built-in SQL database libraries, such as H2, which can be embedded in a Java application, are great for testing SQL scripts and running unit tests that need a database. However, H2 is not the only database; Apache Derby and HSQL also exist.

JDBC troubleshooting libraries are designed to make debugging easier, such as P6spy. This is a module that allows you to easily intercept database data and register them without changing the program code. You can use them to record SQL queries and their time. For example, if PreparedStatment and CallableStatement are used in the code, these Libraries can register an exact call with parameters and with the determination of the time required for execution.

Google Protocol Buffers are a way of encoding structured data in an efficient but extensible format. This is a richer and better alternative to Java serialization. Some of the useful network libraries are Netty and Apache MINA. If a developer writes a program in which it is necessary to perform a low-level network task, then he uses these modules.

Java json libraries

There are reliable JSON libraries for serialization / deserialization. You can find details about Jackson in the GitHub repository.

Java json libraries




It was provided by Doug Crockford and was introduced into the JavaEE 7 platform as part of the Java Specification Request (JSR) initiative 353 and is a standalone implementation that can be integrated with SE starting from version 8.

The ninth modification provides its own JSON support through the Enhancement Proposal (JEP) 198 initiative.

Jackson Benefits:

  1. Widely used, especially by the Spring community.
  2. Provides excellent functionality.
  3. It works well for a long time.
  4. Well maintained with an active development community.
  5. Has good documentation.
  6. Maintains focus by sticking to a single Java / JSON library.

Here are the most important classes and methods of Jackson that serialize / deserialize in JSON:

  1. ObjectMapper - Converts constructs between Java and JSON.
  2. ObjectMapper.writeValue - Converts the Java data type to JSON and in this case displays it in Writer.
  3. ObjectMapper.readValue - Converts JSON to Java data type.

Run a single unit test from the command line as follows:

cd chapter-4 / speakers-test

+ gradle test --tests org.jsonatwork.ch4.BasicJsonTypesTest +

Single unit test




Educational books Horstmann

Educational books horstmann




The variety of libraries makes programmers find an exhaustive but concise guide to all of the new Java SE 9 features. Horstmann's books help them with this, especially for beginners, the tenth edition of Professional Java Library.

Kay S. Horstmann is a professor of computer science at San Jose State University. He is an experienced professional programmer and consultant for large corporations, universities and organizations in Java, C ++, Windows and Internet programming. Horstmann is the author of many successful professional and academic books, including Big C ++, C ++ for Everyone, Big Java, Object Oriented Design and Templates.

Cornell Horstmann’s book, The Java Professional Library, covers all important aspects of the language that a modern developer should know, including modularity, as well as powerful lambda expressions in version 8. They outline the basics of programming with Java in a more accessible and less rigorous approach.

The book provides a more convenient, intense graphic design that conveys complex concepts simply and efficiently. It differs from other books on the market in that it focuses on topics such as loops and graphical interfaces, while avoiding object orientation. The emphasis is on simple programs that use pre-created data structures and algorithms.

Examples and case studies examine practical applications that may be useful to programmers, scientists, and engineers, for example, processing tabular data, collecting information on the Internet, Java graphics libraries to create custom graphics and interfaces.

Ways to Improve Library Performance

Ways to Improve Productivity




JSON is easy to work with and has become the standard data format for just about anything. Stackify uses JSON extensively for REST APIs, serialization of queued messages, and more.

Some general JSON performance tips:

  1. Use multiple JSON libraries for optimal performance and features.
  2. Use streams whenever possible.
  3. Compress custom JSON. Since this is just text, you can expect compression to 90%. Therefore, apply "zip" wherever possible.
  4. Avoid analysis if not necessary.
  5. Serialize / deserialize large and smaller JSON objects. In some cases, when you need to get an array of large objects, it is divided into smaller parts.
  6. Configure the JSON Web API parser.
  7. Do not serialize all fields, null values, or default values.
  8. Use shorter field names.
  9. Manual serialization or parsing can be faster.

Applications are actually regular computer programs running on many real-world devices. They have a special ability to access sensors and control hardware. For this, manufacturers provide so-called programming libraries that make it relatively easy to use hardware in the developed program.




All Articles