Skip to content

Instantly share code, notes, and snippets.

@giuseppe998e
Created April 28, 2022 11:03
Show Gist options
  • Save giuseppe998e/1f33b3dacf6a0f5ccc852c20282a09d9 to your computer and use it in GitHub Desktop.
Save giuseppe998e/1f33b3dacf6a0f5ccc852c20282a09d9 to your computer and use it in GitHub Desktop.
Utility class that writes and reads serializable classes using Java.NIO
// package ...
import java.io.*;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
/**
* MIT License
* <p>
* Copyright (c) 2022 Giuseppe Eletto
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* <p>
* <p>
* This utility class allows you to read and write serializable classes using
* the Java.NIO library.
*/
public final class Serializer {
public static void write(File file, Serializable serializable) throws IOException {
Path filePath = file.toPath();
write(filePath, serializable);
}
public static void write(String file, Serializable serializable) throws IOException {
Path filePath = Paths.get(file);
write(filePath, serializable);
}
public static void write(Path file, Serializable serializable) throws IOException {
try (FileChannel fileChannel = FileChannel.open(file, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
FileLock _fileLock = fileChannel.lock();
OutputStream outputStream = Channels.newOutputStream(fileChannel);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)) {
objectOutputStream.writeObject(serializable);
objectOutputStream.flush();
}
}
public static <T extends Serializable> T read(File file) throws IOException, ClassNotFoundException {
Path filePath = file.toPath();
return read(filePath);
}
public static <T extends Serializable> T read(String file) throws IOException, ClassNotFoundException {
Path filePath = Paths.get(file);
return read(filePath);
}
@SuppressWarnings("unchecked")
public static <T extends Serializable> T read(Path file) throws IOException, ClassNotFoundException {
try (FileChannel fileChannel = FileChannel.open(file, StandardOpenOption.READ);
FileLock _fileLock = fileChannel.lock(0, Long.MAX_VALUE, true);
InputStream inputStream = Channels.newInputStream(fileChannel);
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) {
return (T) objectInputStream.readObject();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment