Home » Core Java » util » Page 29

util

List contents of zip file

In this example we shall show you how to get the list of the contents of a zip file, with ZipFile that is used to read entries from a zip file. To get the files that a zip file contains one should perform the following steps: Create a new ZipFile and open it for reading. Get the Enumeration of the ...

Read More »

Calculate CRC-32 checksum of zip entry

With this example we are going to demonstrate how to calculate the CRC-32 checksum of a ZipEntry. In short, to calculate the CRC-32 checksum of a ZipEntry you should: Create a new ZipFile and open it for reading. Get the Enumeration of the ZipFile entries, with entries() API method of ZipFile and iterate through each one of them. For each ...

Read More »

Get zip entry compression method

This is an example of how to get the ZipEntry Compression method. The compression method of a ZipEntry can be either STORED for uncompressed entries, or DEFLATED for compressed (deflated) entries. Getting the compression method of the ZipEntry implies that you should: Create a new ZipFile to read a zip file with the given name. Get the Enumeration of the ...

Read More »

Calculate CRC32 checksum for byte array

In this example we shall show you how to calculate the CRC32 Checksum of a byte array. To calculate the CRC32 checksum of a byte array one should perform the following steps: Get the byte array of a String, using getBytes() API method of String. Create a new Checksum object, that represents a data checksum. Update the current checksum with ...

Read More »

Compress Objects example

With this example we are going to demonstrate how to compress and expand an Object. We have implemented the ObjectCompressionUtil Class, that extends the Serializable class and has a parameter that is the generic type of the serializable object to be compressed. The class consists of two methods. The compressObject(final T objectToCompress, final OutputStream outstream) takes the object to compress and ...

Read More »

Compress – Decompress files example

This is an example of how to zip and unzip a file. We have implemented the FileCompressionUtil class, that consists of four methods, that zip and unzip a file with or without a checksum. The first method zipFilesInPath(final String zipFileName, final String filePath) zips a file in a specific path. The method creates a ZipOutputStream, using a FileOutputStream, with the ...

Read More »

CountDownLatch example of a more general wait/notify mechanism

In this example we shall show you how to create a CountDownLatch of a more general wait/notify mechanism. We have implemented a method, that is testWaitNotify(). The steps of the method are described below: It creates an Object, to be the mutex. It creates a new Thread, that in its run() method acquires the object, using the synchronized() statement. Although ...

Read More »

Synchronous Queue example to execute commands

private BlockingQueue workQueue = new LinkedBlockingQueue(); private Map commandQueueMap = new ConcurrentHashMap(); public SynchronousQueue addCommand(Command command) { SynchronousQueue queue = new SynchronousQueue(); commandQueueMap.put(command, queue); workQueue.offer(command); return queue; } public Object call() throws Exception { try { Command command = workQueue.take(); Result result = command.execute(); SynchronousQueue queue = commandQueueMap.get(command); queue.offer(result); return null; } catch (InterruptedException e) { throw new WorkException(e); } ...

Read More »

Blocking Queue example of limited connection pool

private BlockingQueue<Connection> pool = new ArrayBlockingQueue<Connection>(10); private AtomicInteger connCount = new AtomicInteger(); public Connection getConnection() { Connection conn = pool.poll(5, TimeUnit.SECONDS); if (conn == null) { synchronized (connCount) { if (connCount.get() < 10) { conn = getNewConnection(); pool.offer(conn); connCount.incrementAndGet(); } } if (conn == null) { throw new ConnUnavailException(); } else { return conn; } } } Related Article: Java ...

Read More »

Blocking Queue example to execute commands

import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; private BlockingQueue<Command> workQueue = new LinkedBlockingQueue<Command>(); public void addCommand(Command command) { workQueue.offer(command); } public Object call() throws Exception { try { Command command = workQueue.take(); command.execute(); } catch (InterruptedException e) { throw new WorkException(e); } } Related Article: Java Concurrency Tutorial – Blocking Queues Reference: Java Concurrency Part 5 – Blocking Queues from our JCG partners ...

Read More »