Skip to content

Commit 7b601f3

Browse files
committed
updated to the official kotlin code conventions
1 parent ff9c537 commit 7b601f3

File tree

12 files changed

+258
-261
lines changed

12 files changed

+258
-261
lines changed

.github/scripts/update-readme-version.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/bin/bash
22

33
#
4-
# Copyright 2024 Kazimierz Pogoda / Xemantic
4+
# Copyright 2024-2025 Kazimierz Pogoda / Xemantic
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
99
#
10-
# http://www.apache.org/licenses/LICENSE-2.0
10+
# https://www.apache.org/licenses/LICENSE-2.0
1111
#
1212
# Unless required by applicable law or agreed to in writing, software
1313
# distributed under the License is distributed on an "AS IS" BASIS,

.idea/copyright/apache2_0.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gradlew

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
#!/bin/sh
22

33
#
4-
# Copyright 2024 Kazimierz Pogoda / Xemantic
4+
# Copyright © 2015-2021 the original authors.
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
99
#
10-
# http://www.apache.org/licenses/LICENSE-2.0
10+
# https://www.apache.org/licenses/LICENSE-2.0
1111
#
1212
# Unless required by applicable law or agreed to in writing, software
1313
# distributed under the License is distributed on an "AS IS" BASIS,
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717
#
18+
# SPDX-License-Identifier: Apache-2.0
19+
#
1820

1921
##############################################################################
2022
#
@@ -84,8 +86,7 @@ done
8486
# shellcheck disable=SC2034
8587
APP_BASE_NAME=${0##*/}
8688
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
87-
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
88-
' "$PWD" ) || exit
89+
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
8990

9091
# Use the maximum available, or set MAX_FD != -1 to use that value.
9192
MAX_FD=maximum

src/commonMain/kotlin/MediaType.kt

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2024 Kazimierz Pogoda / Xemantic
2+
* Copyright 2024-2025 Kazimierz Pogoda / Xemantic
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,66 +16,67 @@
1616

1717
package com.xemantic.ai.file.magic
1818

19+
import com.xemantic.ai.file.magic.MediaType.entries
1920
import kotlinx.io.files.Path
2021

2122
/**
2223
* A subset of MIME types and associated type detectors.
2324
*/
2425
@OptIn(ExperimentalUnsignedTypes::class)
2526
public enum class MediaType(
26-
public val mime: String,
27-
private val detect: (ByteArray) -> Boolean
27+
public val mime: String,
28+
private val detect: (ByteArray) -> Boolean
2829
) {
2930

30-
JPEG(
31-
mime = "image/jpeg",
32-
detect = { it.startsWith(0xFFu, 0xD8u, 0xFFu) }
33-
),
34-
35-
PNG(
36-
mime = "image/png",
37-
detect = { it.startsWith(0x89u, 0x50u, 0x4Eu, 0x47u, 0x0Du, 0x0Au, 0x1Au, 0x0Au) }
38-
),
39-
40-
GIF(
41-
mime = "image/gif",
42-
detect = { it.startsWith("GIF87") || it.startsWith("GIF89") }
43-
),
44-
45-
WEBP(
46-
mime = "image/webp",
47-
detect = { (it.size >= 12) && it.sliceArray(8..11).startsWith("WEBP") }
48-
),
49-
50-
PDF(
51-
mime = "application/pdf",
52-
detect = { it.startsWith("%PDF-") }
53-
);
54-
55-
public companion object {
56-
57-
public fun detect(
58-
bytes: ByteArray
59-
): MediaType? = entries.find {
60-
it.detect(bytes)
61-
}
31+
JPEG(
32+
mime = "image/jpeg",
33+
detect = { it.startsWith(0xFFu, 0xD8u, 0xFFu) }
34+
),
35+
36+
PNG(
37+
mime = "image/png",
38+
detect = { it.startsWith(0x89u, 0x50u, 0x4Eu, 0x47u, 0x0Du, 0x0Au, 0x1Au, 0x0Au) }
39+
),
40+
41+
GIF(
42+
mime = "image/gif",
43+
detect = { it.startsWith("GIF87") || it.startsWith("GIF89") }
44+
),
45+
46+
WEBP(
47+
mime = "image/webp",
48+
detect = { (it.size >= 12) && it.sliceArray(8..11).startsWith("WEBP") }
49+
),
6250

63-
}
51+
PDF(
52+
mime = "application/pdf",
53+
detect = { it.startsWith("%PDF-") }
54+
);
55+
56+
public companion object {
57+
58+
public fun detect(
59+
bytes: ByteArray
60+
): MediaType? = entries.find {
61+
it.detect(bytes)
62+
}
63+
64+
}
6465

6566
}
6667

6768
public fun ByteArray.startsWith(
68-
data: String
69+
data: String
6970
): Boolean = (size >= data.length)
70-
&& sliceArray(0 ..< data.length)
71-
.contentEquals(data.encodeToByteArray())
71+
&& sliceArray(0..<data.length)
72+
.contentEquals(data.encodeToByteArray())
7273

7374
@OptIn(ExperimentalUnsignedTypes::class)
7475
public fun ByteArray.startsWith(
75-
vararg bytes: UByte
76+
vararg bytes: UByte
7677
): Boolean = (size >= bytes.size)
77-
&& sliceArray(0 ..< bytes.size)
78-
.toUByteArray().contentEquals(bytes)
78+
&& sliceArray(0..<bytes.size)
79+
.toUByteArray().contentEquals(bytes)
7980

8081
public fun ByteArray.detectMediaType(): MediaType? = MediaType.detect(this)
8182

src/commonMain/kotlin/Paths.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2024 Kazimierz Pogoda / Xemantic
2+
* Copyright 2024-2025 Kazimierz Pogoda / Xemantic
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -23,13 +23,13 @@ import kotlinx.io.readByteArray
2323
import kotlinx.io.readString
2424

2525
public fun Path.readBytes(): ByteArray = SystemFileSystem.source(
26-
this
26+
this
2727
).buffered().use {
28-
it.readByteArray()
28+
it.readByteArray()
2929
}
3030

3131
public fun Path.readText() = SystemFileSystem.source(
32-
this
32+
this
3333
).buffered().use {
34-
it.readString()
34+
it.readString()
3535
}
Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2024 Kazimierz Pogoda / Xemantic
2+
* Copyright 2024-2025 Kazimierz Pogoda / Xemantic
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,44 +16,39 @@
1616

1717
package com.xemantic.ai.file.magic
1818

19-
import com.xemantic.ai.file.magic.test.MINIMAL_GIF
20-
import com.xemantic.ai.file.magic.test.MINIMAL_JPEG
21-
import com.xemantic.ai.file.magic.test.MINIMAL_PDF
22-
import com.xemantic.ai.file.magic.test.MINIMAL_PNG
23-
import com.xemantic.ai.file.magic.test.MINIMAL_WEBP
24-
import com.xemantic.ai.file.magic.test.testDataDir
19+
import com.xemantic.ai.file.magic.test.*
2520
import com.xemantic.kotlin.test.assert
2621
import com.xemantic.kotlin.test.isBrowserPlatform
2722
import kotlinx.io.files.Path
2823
import kotlin.test.Test
2924

3025
class MediaTypeTest {
3126

32-
@Test
33-
fun `Should detect MediaTypes`() {
34-
assert(byteArrayOf().detectMediaType() == null)
35-
assert("foo".encodeToByteArray().detectMediaType() == null)
36-
assert(MINIMAL_PDF.detectMediaType() == MediaType.PDF)
37-
assert(MINIMAL_JPEG.detectMediaType() == MediaType.JPEG)
38-
assert(MINIMAL_PNG.detectMediaType() == MediaType.PNG)
39-
assert(MINIMAL_WEBP.detectMediaType() == MediaType.WEBP)
40-
assert(MINIMAL_GIF.detectMediaType() == MediaType.GIF)
41-
}
27+
@Test
28+
fun `Should detect MediaTypes`() {
29+
assert(byteArrayOf().detectMediaType() == null)
30+
assert("foo".encodeToByteArray().detectMediaType() == null)
31+
assert(MINIMAL_PDF.detectMediaType() == MediaType.PDF)
32+
assert(MINIMAL_JPEG.detectMediaType() == MediaType.JPEG)
33+
assert(MINIMAL_PNG.detectMediaType() == MediaType.PNG)
34+
assert(MINIMAL_WEBP.detectMediaType() == MediaType.WEBP)
35+
assert(MINIMAL_GIF.detectMediaType() == MediaType.GIF)
36+
}
4237

43-
@Test
44-
fun `Should detect MediaTypes of Paths`() {
45-
if (isBrowserPlatform) return // we don't have file access in the browser, but we do have with node.js
46-
assert(Path(testDataDir, "minimal.gif").detectMediaType() == MediaType.GIF)
47-
assert(Path(testDataDir, "minimal.jpeg").detectMediaType() == MediaType.JPEG)
48-
assert(Path(testDataDir, "minimal.pdf").detectMediaType() == MediaType.PDF)
49-
assert(Path(testDataDir, "minimal.png").detectMediaType() == MediaType.PNG)
50-
assert(Path(testDataDir, "minimal.webp").detectMediaType() == MediaType.WEBP)
51-
}
38+
@Test
39+
fun `Should detect MediaTypes of Paths`() {
40+
if (isBrowserPlatform) return // we don't have file access in the browser, but we do have with node.js
41+
assert(Path(testDataDir, "minimal.gif").detectMediaType() == MediaType.GIF)
42+
assert(Path(testDataDir, "minimal.jpeg").detectMediaType() == MediaType.JPEG)
43+
assert(Path(testDataDir, "minimal.pdf").detectMediaType() == MediaType.PDF)
44+
assert(Path(testDataDir, "minimal.png").detectMediaType() == MediaType.PNG)
45+
assert(Path(testDataDir, "minimal.webp").detectMediaType() == MediaType.WEBP)
46+
}
5247

53-
@Test
54-
fun `Should not detect MediaTypes of a Path with empty file`() {
55-
if (isBrowserPlatform) return // we don't have file access in the browser, but we do have with node.js
56-
assert(Path(testDataDir, "zero.txt").detectMediaType() == null)
57-
}
48+
@Test
49+
fun `Should not detect MediaTypes of a Path with empty file`() {
50+
if (isBrowserPlatform) return // we don't have file access in the browser, but we do have with node.js
51+
assert(Path(testDataDir, "zero.txt").detectMediaType() == null)
52+
}
5853

5954
}
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2024 Kazimierz Pogoda / Xemantic
2+
* Copyright 2024-2025 Kazimierz Pogoda / Xemantic
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -18,34 +18,34 @@ package com.xemantic.ai.file.magic.test
1818

1919
@OptIn(ExperimentalUnsignedTypes::class)
2020
val MINIMAL_GIF: ByteArray = ubyteArrayOf(
21-
// Header
22-
0x47u, 0x49u, 0x46u, 0x38u, 0x39u, 0x61u, // "GIF89a"
21+
// Header
22+
0x47u, 0x49u, 0x46u, 0x38u, 0x39u, 0x61u, // "GIF89a"
2323

24-
// Logical Screen Descriptor
25-
0x01u, 0x00u, // Width (1 pixel)
26-
0x01u, 0x00u, // Height (1 pixel)
27-
0x80u, // Global Color Table Flag (1), Color Resolution (0), Sort Flag (0), Size of Global Color Table
28-
0x00u, // Background Color Index
29-
0x00u, // Pixel Aspect Ratio
24+
// Logical Screen Descriptor
25+
0x01u, 0x00u, // Width (1 pixel)
26+
0x01u, 0x00u, // Height (1 pixel)
27+
0x80u, // Global Color Table Flag (1), Color Resolution (0), Sort Flag (0), Size of Global Color Table
28+
0x00u, // Background Color Index
29+
0x00u, // Pixel Aspect Ratio
3030

31-
// Global Color Table (2 colors: black and white)
32-
0x00u, 0x00u, 0x00u, // Black
33-
0xFFu, 0xFFu, 0xFFu, // White
31+
// Global Color Table (2 colors: black and white)
32+
0x00u, 0x00u, 0x00u, // Black
33+
0xFFu, 0xFFu, 0xFFu, // White
3434

35-
// Image Descriptor
36-
0x2Cu, // Image Separator
37-
0x00u, 0x00u, // Left Position
38-
0x00u, 0x00u, // Top Position
39-
0x01u, 0x00u, // Width
40-
0x01u, 0x00u, // Height
41-
0x00u, // Local Color Table (0), Interlace (0), Sort (0), Reserved (0), Size of Local Color Table (0)
35+
// Image Descriptor
36+
0x2Cu, // Image Separator
37+
0x00u, 0x00u, // Left Position
38+
0x00u, 0x00u, // Top Position
39+
0x01u, 0x00u, // Width
40+
0x01u, 0x00u, // Height
41+
0x00u, // Local Color Table (0), Interlace (0), Sort (0), Reserved (0), Size of Local Color Table (0)
4242

43-
// Image Data
44-
0x02u, // LZW Minimum Code Size
45-
0x02u, // Block Size
46-
0x44u, 0x01u, // Compressed image data
47-
0x00u, // Block Terminator
43+
// Image Data
44+
0x02u, // LZW Minimum Code Size
45+
0x02u, // Block Size
46+
0x44u, 0x01u, // Compressed image data
47+
0x00u, // Block Terminator
4848

49-
// Trailer
50-
0x3Bu // GIF Trailer
49+
// Trailer
50+
0x3Bu // GIF Trailer
5151
).toByteArray()

0 commit comments

Comments
 (0)