質問編集履歴

1

Console.ktの追加

2018/09/20 08:10

投稿

shalllaugh
shalllaugh

スコア17

test CHANGED
File without changes
test CHANGED
@@ -21,3 +21,453 @@
21
21
 
22
22
 
23
23
  よろしければお教えください。
24
+
25
+
26
+
27
+ ```Console.kt
28
+
29
+ /*
30
+
31
+ * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
32
+
33
+ * that can be found in the license/LICENSE.txt file.
34
+
35
+ */
36
+
37
+
38
+
39
+ @file:JvmName("ConsoleKt")
40
+
41
+
42
+
43
+ package kotlin.io
44
+
45
+
46
+
47
+ import kotlin.text.*
48
+
49
+ import java.io.InputStream
50
+
51
+ import java.nio.Buffer
52
+
53
+ import java.nio.ByteBuffer
54
+
55
+ import java.nio.CharBuffer
56
+
57
+ import java.nio.charset.Charset
58
+
59
+ import java.nio.charset.CharsetDecoder
60
+
61
+
62
+
63
+ /** Prints the given message to the standard output stream. */
64
+
65
+ @kotlin.internal.InlineOnly
66
+
67
+ public actual inline fun print(message: Any?) {
68
+
69
+ System.out.print(message)
70
+
71
+ }
72
+
73
+
74
+
75
+ /** Prints the given message to the standard output stream. */
76
+
77
+ @kotlin.internal.InlineOnly
78
+
79
+ public inline fun print(message: Int) {
80
+
81
+ System.out.print(message)
82
+
83
+ }
84
+
85
+
86
+
87
+ /** Prints the given message to the standard output stream. */
88
+
89
+ @kotlin.internal.InlineOnly
90
+
91
+ public inline fun print(message: Long) {
92
+
93
+ System.out.print(message)
94
+
95
+ }
96
+
97
+
98
+
99
+ /** Prints the given message to the standard output stream. */
100
+
101
+ @kotlin.internal.InlineOnly
102
+
103
+ public inline fun print(message: Byte) {
104
+
105
+ System.out.print(message)
106
+
107
+ }
108
+
109
+
110
+
111
+ /** Prints the given message to the standard output stream. */
112
+
113
+ @kotlin.internal.InlineOnly
114
+
115
+ public inline fun print(message: Short) {
116
+
117
+ System.out.print(message)
118
+
119
+ }
120
+
121
+
122
+
123
+ /** Prints the given message to the standard output stream. */
124
+
125
+ @kotlin.internal.InlineOnly
126
+
127
+ public inline fun print(message: Char) {
128
+
129
+ System.out.print(message)
130
+
131
+ }
132
+
133
+
134
+
135
+ /** Prints the given message to the standard output stream. */
136
+
137
+ @kotlin.internal.InlineOnly
138
+
139
+ public inline fun print(message: Boolean) {
140
+
141
+ System.out.print(message)
142
+
143
+ }
144
+
145
+
146
+
147
+ /** Prints the given message to the standard output stream. */
148
+
149
+ @kotlin.internal.InlineOnly
150
+
151
+ public inline fun print(message: Float) {
152
+
153
+ System.out.print(message)
154
+
155
+ }
156
+
157
+
158
+
159
+ /** Prints the given message to the standard output stream. */
160
+
161
+ @kotlin.internal.InlineOnly
162
+
163
+ public inline fun print(message: Double) {
164
+
165
+ System.out.print(message)
166
+
167
+ }
168
+
169
+
170
+
171
+ /** Prints the given message to the standard output stream. */
172
+
173
+ @kotlin.internal.InlineOnly
174
+
175
+ public inline fun print(message: CharArray) {
176
+
177
+ System.out.print(message)
178
+
179
+ }
180
+
181
+
182
+
183
+ /** Prints the given message and newline to the standard output stream. */
184
+
185
+ @kotlin.internal.InlineOnly
186
+
187
+ public actual inline fun println(message: Any?) {
188
+
189
+ System.out.println(message)
190
+
191
+ }
192
+
193
+
194
+
195
+ /** Prints the given message and newline to the standard output stream. */
196
+
197
+ @kotlin.internal.InlineOnly
198
+
199
+ public inline fun println(message: Int) {
200
+
201
+ System.out.println(message)
202
+
203
+ }
204
+
205
+
206
+
207
+ /** Prints the given message and newline to the standard output stream. */
208
+
209
+ @kotlin.internal.InlineOnly
210
+
211
+ public inline fun println(message: Long) {
212
+
213
+ System.out.println(message)
214
+
215
+ }
216
+
217
+
218
+
219
+ /** Prints the given message and newline to the standard output stream. */
220
+
221
+ @kotlin.internal.InlineOnly
222
+
223
+ public inline fun println(message: Byte) {
224
+
225
+ System.out.println(message)
226
+
227
+ }
228
+
229
+
230
+
231
+ /** Prints the given message and newline to the standard output stream. */
232
+
233
+ @kotlin.internal.InlineOnly
234
+
235
+ public inline fun println(message: Short) {
236
+
237
+ System.out.println(message)
238
+
239
+ }
240
+
241
+
242
+
243
+ /** Prints the given message and newline to the standard output stream. */
244
+
245
+ @kotlin.internal.InlineOnly
246
+
247
+ public inline fun println(message: Char) {
248
+
249
+ System.out.println(message)
250
+
251
+ }
252
+
253
+
254
+
255
+ /** Prints the given message and newline to the standard output stream. */
256
+
257
+ @kotlin.internal.InlineOnly
258
+
259
+ public inline fun println(message: Boolean) {
260
+
261
+ System.out.println(message)
262
+
263
+ }
264
+
265
+
266
+
267
+ /** Prints the given message and newline to the standard output stream. */
268
+
269
+ @kotlin.internal.InlineOnly
270
+
271
+ public inline fun println(message: Float) {
272
+
273
+ System.out.println(message)
274
+
275
+ }
276
+
277
+
278
+
279
+ /** Prints the given message and newline to the standard output stream. */
280
+
281
+ @kotlin.internal.InlineOnly
282
+
283
+ public inline fun println(message: Double) {
284
+
285
+ System.out.println(message)
286
+
287
+ }
288
+
289
+
290
+
291
+ /** Prints the given message and newline to the standard output stream. */
292
+
293
+ @kotlin.internal.InlineOnly
294
+
295
+ public inline fun println(message: CharArray) {
296
+
297
+ System.out.println(message)
298
+
299
+ }
300
+
301
+
302
+
303
+ /** Prints a newline to the standard output stream. */
304
+
305
+ @kotlin.internal.InlineOnly
306
+
307
+ public actual inline fun println() {
308
+
309
+ System.out.println()
310
+
311
+ }
312
+
313
+
314
+
315
+ private const val BUFFER_SIZE: Int = 32
316
+
317
+ private const val LINE_SEPARATOR_MAX_LENGTH: Int = 2
318
+
319
+
320
+
321
+ private val decoder: CharsetDecoder by lazy { Charset.defaultCharset().newDecoder() }
322
+
323
+
324
+
325
+ /**
326
+
327
+ * Reads a line of input from the standard input stream.
328
+
329
+ *
330
+
331
+ * @return the line read or `null` if the input stream is redirected to a file and the end of file has been reached.
332
+
333
+ */
334
+
335
+ fun readLine(): String? = readLine(System.`in`, decoder)
336
+
337
+
338
+
339
+ internal fun readLine(inputStream: InputStream, decoder: CharsetDecoder): String? {
340
+
341
+ require(decoder.maxCharsPerByte() <= 1) { "Encodings with multiple chars per byte are not supported" }
342
+
343
+
344
+
345
+ val byteBuffer = ByteBuffer.allocate(BUFFER_SIZE)
346
+
347
+ val charBuffer = CharBuffer.allocate(LINE_SEPARATOR_MAX_LENGTH)
348
+
349
+ val stringBuilder = StringBuilder()
350
+
351
+
352
+
353
+ var read = inputStream.read()
354
+
355
+ if (read == -1) return null
356
+
357
+ do {
358
+
359
+ byteBuffer.put(read.toByte())
360
+
361
+ if (decoder.tryDecode(byteBuffer, charBuffer, false)) {
362
+
363
+ if (charBuffer.containsLineSeparator()) {
364
+
365
+ break
366
+
367
+ }
368
+
369
+ if (!charBuffer.hasRemaining()) {
370
+
371
+ stringBuilder.append(charBuffer.dequeue())
372
+
373
+ }
374
+
375
+ }
376
+
377
+ read = inputStream.read()
378
+
379
+ } while (read != -1)
380
+
381
+
382
+
383
+ with(decoder) {
384
+
385
+ tryDecode(byteBuffer, charBuffer, true) // throws exception if undecoded bytes are left
386
+
387
+ reset()
388
+
389
+ }
390
+
391
+
392
+
393
+ with(charBuffer) {
394
+
395
+ val length = position()
396
+
397
+ val first = get(0)
398
+
399
+ val second = get(1)
400
+
401
+ when (length) {
402
+
403
+ 2 -> {
404
+
405
+ if (!(first == '\r' && second == '\n')) stringBuilder.append(first)
406
+
407
+ if (second != '\n') stringBuilder.append(second)
408
+
409
+ }
410
+
411
+ 1 -> if (first != '\n') stringBuilder.append(first)
412
+
413
+ }
414
+
415
+ }
416
+
417
+
418
+
419
+ return stringBuilder.toString()
420
+
421
+ }
422
+
423
+
424
+
425
+ private fun CharsetDecoder.tryDecode(byteBuffer: ByteBuffer, charBuffer: CharBuffer, isEndOfStream: Boolean): Boolean {
426
+
427
+ val positionBefore = charBuffer.position()
428
+
429
+ byteBuffer.flip()
430
+
431
+ with(decode(byteBuffer, charBuffer, isEndOfStream)) {
432
+
433
+ if (isError) throwException()
434
+
435
+ }
436
+
437
+ return (charBuffer.position() > positionBefore).also { isDecoded ->
438
+
439
+ if (isDecoded) byteBuffer.clear() else byteBuffer.flipBack()
440
+
441
+ }
442
+
443
+ }
444
+
445
+
446
+
447
+ private fun CharBuffer.containsLineSeparator(): Boolean {
448
+
449
+ return get(1) == '\n' || get(0) == '\n'
450
+
451
+ }
452
+
453
+
454
+
455
+ private fun Buffer.flipBack() {
456
+
457
+ position(limit())
458
+
459
+ limit(capacity())
460
+
461
+ }
462
+
463
+
464
+
465
+ private fun CharBuffer.dequeue(): Char {
466
+
467
+ flip()
468
+
469
+ return get().also { compact() }
470
+
471
+ }
472
+
473
+ ```