forked from melod1n/fast-messenger
Move from java/ to kotlin/ directory
Android 12 dynamic color usage on login screen
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package com.meloda.fast.io
|
||||
|
||||
import java.io.ByteArrayOutputStream
|
||||
|
||||
class BytesOutputStream : ByteArrayOutputStream {
|
||||
constructor() : super(8192)
|
||||
constructor(size: Int) : super(size)
|
||||
|
||||
val byteArray: ByteArray = buf
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.meloda.fast.io
|
||||
|
||||
import java.nio.charset.Charset
|
||||
import java.nio.charset.StandardCharsets
|
||||
|
||||
object Charsets {
|
||||
|
||||
val ASCII: Charset = StandardCharsets.US_ASCII
|
||||
|
||||
val UTF_8: Charset = StandardCharsets.UTF_8
|
||||
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
package com.meloda.fast.io
|
||||
|
||||
import org.jetbrains.annotations.Contract
|
||||
import java.io.*
|
||||
import java.nio.charset.Charset
|
||||
import java.util.zip.GZIPInputStream
|
||||
import java.util.zip.GZIPOutputStream
|
||||
import kotlin.math.max
|
||||
|
||||
object EasyStreams {
|
||||
|
||||
const val BUFFER_SIZE = 8192
|
||||
const val CHAR_BUFFER_SIZE = 4096
|
||||
|
||||
@JvmOverloads
|
||||
@Throws(IOException::class)
|
||||
fun read(from: InputStream, encoding: Charset? = Charsets.UTF_8): String {
|
||||
return read(InputStreamReader(from, encoding))
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
@Throws(IOException::class)
|
||||
fun read(from: Reader): String {
|
||||
val builder = StringWriter(CHAR_BUFFER_SIZE)
|
||||
return try {
|
||||
copy(from, builder)
|
||||
builder.toString()
|
||||
} finally {
|
||||
close(from)
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
@Throws(IOException::class)
|
||||
fun readBytes(from: InputStream): ByteArray {
|
||||
val output = ByteArrayOutputStream(max(from.available(), BUFFER_SIZE))
|
||||
try {
|
||||
copy(from, output)
|
||||
} finally {
|
||||
close(from)
|
||||
}
|
||||
return output.toByteArray()
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun write(from: ByteArray?, to: OutputStream) {
|
||||
try {
|
||||
to.write(from)
|
||||
to.flush()
|
||||
} finally {
|
||||
close(to)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun write(from: String?, to: OutputStream?) {
|
||||
write(from, OutputStreamWriter(to, Charsets.UTF_8))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun write(from: CharArray?, to: Writer) {
|
||||
try {
|
||||
to.write(from)
|
||||
to.flush()
|
||||
} finally {
|
||||
close(to)
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
@Throws(IOException::class)
|
||||
fun write(from: String?, to: Writer) {
|
||||
try {
|
||||
to.write(from)
|
||||
to.flush()
|
||||
} finally {
|
||||
close(to)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun copy(from: Reader, to: Writer): Long {
|
||||
val buffer = CharArray(CHAR_BUFFER_SIZE)
|
||||
var read: Int
|
||||
var total: Long = 0
|
||||
while (from.read(buffer).also { read = it } != -1) {
|
||||
to.write(buffer, 0, read)
|
||||
total += read.toLong()
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun copy(from: InputStream, to: OutputStream): Long {
|
||||
val buffer = ByteArray(BUFFER_SIZE)
|
||||
var read: Int
|
||||
var total: Long = 0
|
||||
while (from.read(buffer).also { read = it } != -1) {
|
||||
to.write(buffer, 0, read)
|
||||
total += read.toLong()
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
fun buffer(input: InputStream?): BufferedInputStream {
|
||||
return buffer(input, BUFFER_SIZE)
|
||||
}
|
||||
|
||||
@Contract("null, _ -> new")
|
||||
fun buffer(input: InputStream?, size: Int): BufferedInputStream {
|
||||
return if (input is BufferedInputStream) input else BufferedInputStream(input, size)
|
||||
}
|
||||
|
||||
fun buffer(output: OutputStream?): BufferedOutputStream {
|
||||
return buffer(output, BUFFER_SIZE)
|
||||
}
|
||||
|
||||
@Contract("null, _ -> new")
|
||||
fun buffer(output: OutputStream?, size: Int): BufferedOutputStream {
|
||||
return if (output is BufferedOutputStream) output else BufferedOutputStream(output, size)
|
||||
}
|
||||
|
||||
fun buffer(input: Reader?): BufferedReader {
|
||||
return buffer(input, CHAR_BUFFER_SIZE)
|
||||
}
|
||||
|
||||
@Contract("null, _ -> new")
|
||||
fun buffer(input: Reader?, size: Int): BufferedReader {
|
||||
return if (input is BufferedReader) input else BufferedReader(input, size)
|
||||
}
|
||||
|
||||
fun buffer(output: Writer?): BufferedWriter {
|
||||
return buffer(output, CHAR_BUFFER_SIZE)
|
||||
}
|
||||
|
||||
@Contract("null, _ -> new")
|
||||
fun buffer(output: Writer?, size: Int): BufferedWriter {
|
||||
return if (output is BufferedWriter) output else BufferedWriter(output, size)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun gzip(input: InputStream?): GZIPInputStream {
|
||||
return gzip(input, BUFFER_SIZE)
|
||||
}
|
||||
|
||||
@Contract("null, _ -> new")
|
||||
@Throws(IOException::class)
|
||||
fun gzip(input: InputStream?, size: Int): GZIPInputStream {
|
||||
return if (input is GZIPInputStream) input else GZIPInputStream(input, size)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun gzip(input: OutputStream?): GZIPOutputStream {
|
||||
return gzip(input, BUFFER_SIZE)
|
||||
}
|
||||
|
||||
@Contract("null, _ -> new")
|
||||
@Throws(IOException::class)
|
||||
fun gzip(input: OutputStream?, size: Int): GZIPOutputStream {
|
||||
return if (input is GZIPOutputStream) input else GZIPOutputStream(input, size)
|
||||
}
|
||||
|
||||
fun close(c: Closeable?): Boolean {
|
||||
if (c != null) {
|
||||
try {
|
||||
c.close()
|
||||
return true
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.meloda.fast.io
|
||||
|
||||
import org.jetbrains.annotations.Contract
|
||||
import java.io.*
|
||||
import java.math.BigInteger
|
||||
|
||||
object FileStreams {
|
||||
|
||||
val lineSeparatorChar = lineSeparator()[0]
|
||||
|
||||
const val ONE_KB = 1024
|
||||
const val ONE_MB = ONE_KB * 1024
|
||||
const val ONE_GB = ONE_MB * 1024
|
||||
const val ONE_TB = ONE_GB * 1024L
|
||||
const val ONE_PB = ONE_TB * 1024L
|
||||
const val ONE_EB = ONE_PB * 1024L
|
||||
|
||||
val ONE_ZB: BigInteger = BigInteger.valueOf(ONE_EB).multiply(BigInteger.valueOf(1024L))
|
||||
val ONE_YB: BigInteger = ONE_ZB.multiply(BigInteger.valueOf(1024L))
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun read(from: File?): String {
|
||||
return EasyStreams.read(reader(from))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun write(from: String?, to: File?) {
|
||||
EasyStreams.write(from, writer(to))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun write(from: ByteArray?, to: File?) {
|
||||
EasyStreams.write(from, FileOutputStream(to))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun append(from: ByteArray?, to: File?) {
|
||||
EasyStreams.write(from, FileOutputStream(to, true))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun append(from: CharArray?, to: File?) {
|
||||
EasyStreams.write(from, FileWriter(to, true))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun append(from: CharSequence, to: File?) {
|
||||
EasyStreams.write(if (from is String) from else from.toString(), FileWriter(to, true))
|
||||
}
|
||||
|
||||
fun delete(dir: File) {
|
||||
if (dir.isDirectory) {
|
||||
val files = dir.listFiles() ?: return
|
||||
for (file in files) {
|
||||
delete(file)
|
||||
}
|
||||
} else {
|
||||
dir.delete()
|
||||
}
|
||||
}
|
||||
|
||||
fun lineSeparator(): String {
|
||||
return System.lineSeparator()
|
||||
}
|
||||
|
||||
fun search(dir: File, name: String?): File? {
|
||||
require(dir.isDirectory) { "dir can't be file." }
|
||||
|
||||
val files = dir.listFiles() ?: return null
|
||||
|
||||
if (files.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
|
||||
for (file in files) {
|
||||
if (file.isDirectory) {
|
||||
search(file, name)
|
||||
} else if (file.name.contains(name!!)) {
|
||||
return file
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
@Contract("_ -> new")
|
||||
@Throws(FileNotFoundException::class)
|
||||
fun reader(from: File?): Reader {
|
||||
return InputStreamReader(FileInputStream(from), Charsets.UTF_8)
|
||||
}
|
||||
|
||||
@Contract("_ -> new")
|
||||
@Throws(FileNotFoundException::class)
|
||||
fun writer(to: File?): Writer {
|
||||
return OutputStreamWriter(FileOutputStream(to), Charsets.UTF_8)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user