Skip to content
This repository was archived by the owner on Mar 8, 2024. It is now read-only.

Commit 333876b

Browse files
authored
Merge pull request #11 from LSafer-Agile/master
Java 8 full support
2 parents 5674fb8 + 469c5c1 commit 333876b

8 files changed

Lines changed: 115 additions & 14 deletions

File tree

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ dependencies {
2424

2525
implementation("org.codehaus.groovy:groovy-all:3.0.8")
2626
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.6.0")
27-
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0-RC")
28-
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.1")
27+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0")
28+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
2929

3030
testImplementation("junit:junit:4.13.2")
3131
}

src/main/java/org/cufy/http/body/FileBody.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.cufy.http.body;
1717

1818
import org.cufy.http.Body;
19+
import org.cufy.http.internal.util.StreamUtil;
1920
import org.cufy.http.mime.Mime;
2021
import org.jetbrains.annotations.Contract;
2122
import org.jetbrains.annotations.NotNull;
@@ -154,7 +155,7 @@ public InputStream openInputStream() {
154155
public String toString() {
155156
try (InputStream in = new FileInputStream(this.file)) {
156157
return new String(
157-
in.readAllBytes(),
158+
StreamUtil.readAllBytes(in),
158159
StandardCharsets.UTF_8
159160
);
160161
} catch (IOException e) {

src/main/java/org/cufy/http/body/JsonBody.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.cufy.http.body;
1717

1818
import org.cufy.http.Body;
19+
import org.cufy.http.internal.util.StreamUtil;
1920
import org.cufy.http.json.JsonElement;
2021
import org.cufy.http.json.JsonObject;
2122
import org.cufy.http.json.JsonPath;
@@ -136,8 +137,8 @@ public JsonBody(@NotNull Consumer<@NotNull JsonBody> builder) {
136137
@Contract(value = "_->new", pure = true)
137138
public static JsonBody from(@NotNull Body body) {
138139
Objects.requireNonNull(body, "body");
139-
try (InputStream stream = body.openInputStream()) {
140-
String string = new String(stream.readAllBytes());
140+
try (InputStream is = body.openInputStream()) {
141+
String string = new String(StreamUtil.readAllBytes(is));
141142
JsonObject object = JsonObject.parse(string);
142143

143144
return new JsonBody(

src/main/java/org/cufy/http/body/ParametersBody.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.cufy.http.Body;
1919
import org.cufy.http.internal.syntax.UriRegExp;
20+
import org.cufy.http.internal.util.StreamUtil;
2021
import org.cufy.http.mime.Mime;
2122
import org.cufy.http.mime.MimeSubtype;
2223
import org.cufy.http.mime.MimeType;
@@ -131,8 +132,8 @@ public ParametersBody(@NotNull Consumer<@NotNull ParametersBody> builder) {
131132
@Contract(value = "_->new", pure = true)
132133
public static ParametersBody from(@NotNull Body body) {
133134
Objects.requireNonNull(body, "body");
134-
try (InputStream stream = body.openInputStream()) {
135-
String string = new String(stream.readAllBytes());
135+
try (InputStream is = body.openInputStream()) {
136+
String string = new String(StreamUtil.readAllBytes(is));
136137

137138
return new ParametersBody(
138139
body.getMime(),

src/main/java/org/cufy/http/body/TextBody.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.cufy.http.Body;
1919
import org.cufy.http.internal.syntax.HttpRegExp;
20+
import org.cufy.http.internal.util.StreamUtil;
2021
import org.cufy.http.mime.Mime;
2122
import org.cufy.http.mime.MimeSubtype;
2223
import org.cufy.http.mime.MimeType;
@@ -145,7 +146,7 @@ public TextBody(@NotNull Consumer<@NotNull TextBody> builder) {
145146
public static TextBody from(@NotNull Body body) {
146147
Objects.requireNonNull(body, "body");
147148
try (InputStream stream = body.openInputStream()) {
148-
String string = new String(stream.readAllBytes());
149+
String string = new String(StreamUtil.readAllBytes(stream));
149150

150151
return new TextBody(
151152
body.getMime(),
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2021-2022 Cufy and ProgSpaceSA
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.cufy.http.internal.util;
17+
18+
import org.jetbrains.annotations.ApiStatus;
19+
import org.jetbrains.annotations.Contract;
20+
import org.jetbrains.annotations.NotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
26+
/**
27+
* Internal utilities to deal with streams.
28+
*
29+
* @author LSafer
30+
* @version 1.0.0
31+
* @since 1.0.0 ~2022.01.07
32+
*/
33+
@ApiStatus.Internal
34+
public final class StreamUtil {
35+
/**
36+
* Utility classes shall have no instances.
37+
*
38+
* @throws AssertionError when called.
39+
* @since 1.0.0 ~2022.01.07
40+
*/
41+
private StreamUtil() {
42+
throw new AssertionError("No instance for you!");
43+
}
44+
45+
/**
46+
* A utility function to read all the bytes in a particular input stream. The stream
47+
* will not be closed automatically.
48+
*
49+
* @param is the input stream.
50+
* @return the bytes from reading the input stream.
51+
* @throws IOException if any I/O exception occurs while reading the input stream.
52+
* @since 1.0.0 ~2022.01.07
53+
*/
54+
@Contract(mutates = "param")
55+
public static byte @NotNull [] readAllBytes(@NotNull InputStream is) throws IOException {
56+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
57+
58+
//noinspection CheckForOutOfMemoryOnLargeArrayAllocation
59+
byte[] buffer = new byte[8192];
60+
61+
while (true) {
62+
int read = is.read(buffer, 0, buffer.length);
63+
64+
if (read < 0)
65+
break;
66+
67+
if (read == 0)
68+
continue;
69+
70+
baos.write(buffer, 0, read);
71+
}
72+
73+
return baos.toByteArray();
74+
}
75+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2021-2022 Cufy and ProgSpaceSA
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
/**
17+
* Internal utilities.
18+
*
19+
* @author LSafer
20+
* @version 1.0.0
21+
* @since 1.0.0 ~2022.01.07
22+
*/
23+
package org.cufy.http.internal.util;

src/main/java/org/cufy/http/json/JsonArray.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.io.StringReader;
2929
import java.util.*;
3030
import java.util.function.Consumer;
31-
import java.util.function.IntFunction;
3231
import java.util.function.Predicate;
3332
import java.util.function.UnaryOperator;
3433
import java.util.stream.Stream;
@@ -80,7 +79,7 @@ public JsonArray(@NotNull List<@NotNull JsonElement> list) {
8079
/**
8180
* Construct a new json array with the given {@code builder}.
8281
*
83-
* @param builder the builder to apply to the new jsona array.
82+
* @param builder the builder to apply to the new json array.
8483
* @throws NullPointerException if the given {@code builder} is null.
8584
* @since 0.2.3 ~2021.08.27
8685
*/
@@ -522,8 +521,8 @@ public <T> T[] toArray(@NotNull T[] array) {
522521
return this.list.toArray(array);
523522
}
524523

525-
@Override
526-
public <T> T[] toArray(@NotNull IntFunction<T[]> generator) {
527-
return this.list.toArray(generator);
528-
}
524+
// @Override
525+
// public <T> T[] toArray(@NotNull IntFunction<T[]> generator) {
526+
// return this.list.toArray(generator);
527+
// }
529528
}

0 commit comments

Comments
 (0)