/
Implicits.scala
373 lines (305 loc) · 12.1 KB
/
Implicits.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package better.files
import java.io.{File => JFile, _}
import java.nio.MappedByteBuffer
import java.nio.channels.FileChannel
import java.nio.charset.Charset
import java.nio.file.{Path, PathMatcher}
import java.security.MessageDigest
import java.util.StringTokenizer
import java.util.stream.{Stream => JStream}
import java.util.zip._
import scala.annotation.tailrec
import scala.collection.JavaConverters._
/**
* Container for various implicits
*/
trait Implicits extends Dispose.FlatMap.Implicits with Scanner.Read.Implicits with Scanner.Source.Implicits {
//TODO: Rename all Ops to Extensions
implicit class StringInterpolations(sc: StringContext) {
def file(args: Any*): File =
value(args).toFile
private[this] def value(args: Seq[Any]) =
sc.s(args: _*)
}
implicit class StringExtensions(str: String) {
def toFile: File =
File(str)
def /(child: String): File =
toFile / child
def inputStream(implicit charset: Charset = DefaultCharset): InputStream =
new ByteArrayInputStream(str.getBytes(charset))
def reader: Reader =
new StringReader(str)
}
implicit class FileExtensions(file: JFile) {
def toScala: File =
File(file.getPath)
}
implicit class SymbolExtensions(symbol: Symbol) {
def /(child: Symbol): File =
File(symbol.name) / child
}
implicit class IteratorExtensions[A](it: Iterator[A]) {
def withHasNext(f: => Boolean): Iterator[A] = new Iterator[A] {
override def hasNext = f && it.hasNext
override def next() = it.next()
}
}
implicit class InputStreamExtensions(in: InputStream) {
def pipeTo(out: OutputStream, bufferSize: Int = DefaultBufferSize): out.type =
pipeTo(out, Array.ofDim[Byte](bufferSize))
/**
* Pipe an input stream to an output stream using a byte buffer
*/
@tailrec final def pipeTo(out: OutputStream, buffer: Array[Byte]): out.type = {
val n = in.read(buffer)
if (n > 0) {
out.write(buffer, 0, n)
pipeTo(out, buffer)
} else {
out
}
}
def asString(
closeStream: Boolean = true,
bufferSize: Int = DefaultBufferSize
)(implicit
charset: Charset = DefaultCharset
): String = {
try {
new ByteArrayOutputStream(bufferSize).autoClosed
.apply(pipeTo(_, bufferSize = bufferSize).toString(charset.displayName()))
} finally {
if (closeStream) in.close()
}
}
def buffered: BufferedInputStream =
new BufferedInputStream(in)
def buffered(bufferSize: Int): BufferedInputStream =
new BufferedInputStream(in, bufferSize)
def asGzipInputStream(bufferSize: Int = DefaultBufferSize): GZIPInputStream =
new GZIPInputStream(in, bufferSize)
def asZipInputStream(implicit charset: Charset = DefaultCharset): ZipInputStream =
new ZipInputStream(in, charset)
/**
* If bufferSize is set to less than or equal to 0, we don't buffer
* @param bufferSize
* @return
*/
def asObjectInputStream(bufferSize: Int = DefaultBufferSize): ObjectInputStream =
new ObjectInputStream(if (bufferSize <= 0) in else buffered(bufferSize))
/**
* @param bufferSize If bufferSize is set to less than or equal to 0, we don't buffer
* Code adapted from:
* https://github.com/apache/commons-io/blob/master/src/main/java/org/apache/commons/io/input/ClassLoaderObjectInputStream.java
*
* @return A special ObjectInputStream that loads a class based on a specified ClassLoader rather than the default
* This is useful in dynamic container environments.
*/
def asObjectInputStreamUsingClassLoader(
classLoader: ClassLoader = getClass.getClassLoader,
bufferSize: Int = DefaultBufferSize
): ObjectInputStream =
new ObjectInputStream(if (bufferSize <= 0) in else buffered(bufferSize)) {
override protected def resolveClass(objectStreamClass: ObjectStreamClass): Class[_] =
try {
Class.forName(objectStreamClass.getName, false, classLoader)
} catch {
case _: ClassNotFoundException ⇒ super.resolveClass(objectStreamClass)
}
}
def reader(implicit charset: Charset = DefaultCharset): InputStreamReader =
new InputStreamReader(in, charset)
def lines(implicit charset: Charset = DefaultCharset): Iterator[String] =
reader(charset).buffered.lines().toAutoClosedIterator
def bytes: Iterator[Byte] =
in.autoClosed.flatMap(res => eofReader(res.read()).map(_.toByte))
def byteArray: Array[Byte] = {
for {
_ <- in.autoClosed
out <- new ByteArrayOutputStream().autoClosed
} yield pipeTo(out).toByteArray
}.get()
}
implicit class OutputStreamExtensions(val out: OutputStream) {
def buffered: BufferedOutputStream =
new BufferedOutputStream(out)
def buffered(bufferSize: Int): BufferedOutputStream =
new BufferedOutputStream(out, bufferSize)
def asGzipOutputStream(bufferSize: Int = DefaultBufferSize, syncFlush: Boolean = false): GZIPOutputStream =
new GZIPOutputStream(out, bufferSize, syncFlush)
def writer(implicit charset: Charset = DefaultCharset): OutputStreamWriter =
new OutputStreamWriter(out, charset)
def printWriter(autoFlush: Boolean = false): PrintWriter =
new PrintWriter(out, autoFlush)
def write(bytes: Iterator[Byte], bufferSize: Int = DefaultBufferSize): out.type = {
bytes.grouped(bufferSize).foreach(buffer => out.write(buffer.toArray))
out.flush()
out
}
def writeAndClose(str: String)(implicit charset: Charset = DefaultCharset): Unit =
out.writer.autoClosed.foreach(_.write(str))
def tee(out2: OutputStream): OutputStream =
new TeeOutputStream(out, out2)
/**
* If bufferSize is set to less than or equal to 0, we don't buffer
* @param bufferSize
* @return
*/
def asObjectOutputStream(bufferSize: Int = DefaultBufferSize): ObjectOutputStream =
new ObjectOutputStream(if (bufferSize <= 0) out else buffered(bufferSize))
def asZipOutputStream(implicit charset: Charset): ZipOutputStream =
new ZipOutputStream(out, charset)
}
implicit class PrintWriterExtensions(pw: PrintWriter) {
def printLines(lines: TraversableOnce[_]): PrintWriter = {
lines.foreach(pw.println)
pw
}
}
implicit class ReaderExtensions(reader: Reader) {
def buffered: BufferedReader =
new BufferedReader(reader)
def toInputStream(implicit charset: Charset = DefaultCharset): InputStream =
new ReaderInputStream(reader)(charset)
def chars: Iterator[Char] =
new Dispose(reader).flatMap(res => eofReader(res.read()).map(_.toChar))
}
implicit class BufferedReaderExtensions(reader: BufferedReader) {
def tokens(splitter: StringSplitter = StringSplitter.Default): Iterator[String] =
reader.lines().toAutoClosedIterator.flatMap(splitter.split)
}
implicit class WriterExtensions(writer: Writer) {
def buffered: BufferedWriter =
new BufferedWriter(writer)
def outputstream(implicit charset: Charset = DefaultCharset): OutputStream =
new WriterOutputStream(writer)(charset)
}
implicit class FileChannelExtensions(fc: FileChannel) {
def toMappedByteBuffer: MappedByteBuffer =
fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size())
}
implicit class PathMatcherExtensions(matcher: PathMatcher) {
def matches(file: File, maxDepth: Int)(implicit visitOptions: File.VisitOptions = File.VisitOptions.default) =
file.collectChildren(child => matcher.matches(child.path), maxDepth)(visitOptions)
}
implicit class ObjectInputStreamExtensions(ois: ObjectInputStream) {
def deserialize[A]: A =
ois.readObject().asInstanceOf[A]
}
implicit class ObjectOutputStreamExtensions(val oos: ObjectOutputStream) {
def serialize(obj: Serializable): oos.type = {
oos.writeObject(obj)
oos
}
}
implicit class ZipOutputStreamExtensions(val out: ZipOutputStream) {
/**
* Correctly set the compression level
* See: http://stackoverflow.com/questions/1206970/creating-zip-using-zip-utility
*
* @param level
* @return
*/
def withCompressionLevel(level: Int): out.type = {
out.setLevel(level)
if (level == Deflater.NO_COMPRESSION) out.setMethod(ZipOutputStream.DEFLATED)
out
}
def add(file: File, name: String): out.type = {
val relativeName = name.stripSuffix(file.fileSystem.getSeparator)
val entryName = if (file.isDirectory) s"$relativeName/" else relativeName // make sure to end directories in ZipEntry with "/"
out.putNextEntry(new ZipEntry(entryName))
if (file.isRegularFile) file.inputStream.foreach(_.pipeTo(out))
out.closeEntry()
out
}
def +=(file: File): out.type =
add(file, file.name)
}
implicit class ZipInputStreamExtensions(val in: ZipInputStream) {
/**
* Apply `f` on each ZipEntry in the archive, closing the entry after `f` has been applied.
*
* @param f The function to apply to each ZipEntry. Can fail if it returns a lazy value,
* like Iterator, as the entry will have been closed before the lazy value is evaluated.
*/
def mapEntries[A](f: ZipEntry => A): Iterator[A] = new Iterator[A] {
private[this] var entry = in.getNextEntry
override def hasNext = entry != null
override def next() = {
try {
f(entry)
} finally {
try {
in.closeEntry()
} finally {
entry = in.getNextEntry
}
}
}
}
/**
* Apply `f` to the ZipInputStream for every entry in the archive.
* @param f The function to apply to the ZipInputStream. Can fail if it returns a lazy value,
* like Iterator, as the the entry will have been closed before the lazy value is evaluated.
*/
def foldMap[A](f: ZipInputStream => A): Iterator[A] =
mapEntries(_ => f(in))
}
implicit class ZipEntryExtensions(val entry: ZipEntry) {
/**
* Extract this ZipEntry under this rootDir
*
* @param rootDir directory under which this entry is extracted
* @param inputStream use this inputStream when this entry is a file
* @return the extracted file
*/
def extractTo(rootDir: File, inputStream: => InputStream): File = {
val entryName = entry.getName.replace("\\", "/") //see https://github.com/pathikrit/better-files/issues/262
val child = rootDir.createChild(entryName, asDirectory = entry.isDirectory, createParents = true)
if (!entry.isDirectory) child.outputStream.foreach(inputStream.pipeTo(_))
child
}
}
implicit class DisposeableExtensions[A: Disposable](resource: A) {
/**
* Lightweight automatic resource management
* Closes the resource when done e.g.
* <pre>
* for {
* in <- file.newInputStream.autoClosed
* } in.write(bytes)
* // in is closed now
* </pre>
*
* @return
*/
def autoClosed: Dispose[A] =
new Dispose(resource)
}
implicit class JStreamExtensions[A](stream: JStream[A]) {
/**
* Closes this stream when iteration is complete
* It will NOT close the stream if it is not depleted!
*
* @return
*/
def toAutoClosedIterator: Iterator[A] =
stream.autoClosed.flatMap(_.iterator().asScala)
}
private[files] implicit class OrderingExtensions[A](order: Ordering[A]) {
def andThenBy(order2: Ordering[A]): Ordering[A] =
Ordering.comparatorToOrdering(order.thenComparing(order2))
}
implicit def stringToMessageDigest(algorithmName: String): MessageDigest =
MessageDigest.getInstance(algorithmName)
implicit def stringToCharset(charsetName: String): Charset =
Charset.forName(charsetName)
implicit def tokenizerToIterator(s: StringTokenizer): Iterator[String] =
Iterator.continually(s.nextToken()).withHasNext(s.hasMoreTokens)
//implicit def posixPermissionToFileAttribute(perm: PosixFilePermission) =
// PosixFilePermissions.asFileAttribute(Set(perm))
private[files] implicit def pathStreamToFiles(files: JStream[Path]): Iterator[File] =
files.toAutoClosedIterator.map(File.apply)
}