All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.blade.kit.IOKit Maven / Gradle / Ivy

/**
 * Copyright (c) 2017, biezhi 王爵 ([email protected])
 * 

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.blade.kit; import lombok.experimental.UtilityClass; import lombok.extern.slf4j.Slf4j; import java.io.*; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.stream.Collectors; import java.util.zip.GZIPOutputStream; /** * IO Kit * * @author biezhi * 2017/6/2 */ @Slf4j @UtilityClass public class IOKit { public static void closeQuietly(Closeable closeable) { try { if (null == closeable) { return; } closeable.close(); } catch (Exception e) { log.error("Close closeable error", e); } } public static String readToString(String file) throws IOException { return readToString(Paths.get(file)); } public static String readToString(BufferedReader bufferedReader) { return bufferedReader.lines().collect(Collectors.joining()); } public static String readToString(Path path) throws IOException { BufferedReader bufferedReader = Files.newBufferedReader(path); return bufferedReader.lines().collect(Collectors.joining()); } public static String readToString(InputStream input) throws IOException { try (BufferedReader buffer = new BufferedReader(new InputStreamReader(input, "UTF-8"))) { return buffer.lines().collect(Collectors.joining("\n")); } } public static void copyFile(File source, File dest) throws IOException { try (FileChannel in = new FileInputStream(source).getChannel(); FileChannel out = new FileOutputStream(dest).getChannel()) { out.transferFrom(in, 0, in.size()); } } public static void compressGZIP(File input, File output) throws IOException { try (GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(output))) { try (FileInputStream in = new FileInputStream(input)) { byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } } } } public static byte[] compressGZIPAsString(String content, Charset charset) throws IOException { if (content == null || content.length() == 0) { return null; } GZIPOutputStream gzip; ByteArrayOutputStream out = new ByteArrayOutputStream(); gzip = new GZIPOutputStream(out); gzip.write(content.getBytes(charset)); gzip.close(); return out.toByteArray(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy