
com.appslandia.common.utils.FileUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of appslandia-common-cp Show documentation
Show all versions of appslandia-common-cp Show documentation
AppsLandia Common - Java Utilities
The newest version!
// The MIT License (MIT)
// Copyright © 2015 AppsLandia. All rights reserved.
// 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:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 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.
package com.appslandia.common.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import com.appslandia.common.base.StringWriter;
/**
*
* @author Loc Ha
*
*/
public class FileUtils {
private static final int __defaultBufferSize = 4096;
public static int copy(InputStream is, OutputStream os) throws IOException {
int count = 0;
byte[] buf = new byte[__defaultBufferSize];
int c = -1;
while ((c = is.read(buf)) != -1) {
os.write(buf, 0, c);
count += c;
}
return count;
}
public static void copy(byte[] src, int copyLength, OutputStream os) throws IOException {
int count = 0;
byte[] buf = new byte[__defaultBufferSize];
while (count < copyLength) {
int left = copyLength - count;
int c = (left >= buf.length) ? buf.length : left;
System.arraycopy(src, count, buf, 0, c);
count += c;
os.write(buf, 0, c);
}
}
public static byte[] toByteArray(InputStream is) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream(__defaultBufferSize);
copy(is, os);
return os.toByteArray();
}
public static int copy(Reader r, Writer w) throws IOException {
int count = 0;
char[] buf = new char[__defaultBufferSize / 2];
int c = -1;
while ((c = r.read(buf)) != -1) {
w.write(buf, 0, c);
count += c;
}
return count;
}
public static String toString(Reader r) throws IOException {
StringWriter os = new StringWriter();
copy(r, os);
return os.toString();
}
public static String toFileName(String fileName, String extraInfo) {
int dotIdx = fileName.lastIndexOf('.');
if (dotIdx < 0) {
return fileName + extraInfo;
}
return fileName.substring(0, dotIdx) + extraInfo + fileName.substring(dotIdx);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy