Skip to content

Commit 809e4e3

Browse files
lazer-devAlex Danilenko
andauthored
memory improvements (#52)
Co-authored-by: Alex Danilenko <[email protected]>
1 parent d4c699e commit 809e4e3

File tree

14 files changed

+247
-177
lines changed

14 files changed

+247
-177
lines changed

core/src/main/java/spotty/common/http/HttpHeaders.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ public HttpHeaders(HttpHeaders headers) {
315315
/**
316316
* add header
317317
*
318-
* @param name header name
318+
* @param name header name
319319
* @param value header value
320320
* @return this instance of headers
321321
*/
@@ -360,39 +360,39 @@ public String get(String name) {
360360
* remove header by name
361361
*
362362
* @param name header name
363-
* @return the previous header value associated with <tt>name</tt>, or
364-
* <tt>null</tt> if there was no header for given <tt>name</tt>.
363+
* @return the previous header value associated with name, or
364+
* null if there was no header for given name.
365365
*/
366366
public String remove(String name) {
367367
return headers.remove(name);
368368
}
369369

370370
/**
371-
* Returns <tt>true</tt> if this HttpHeaders contains a header for the specified name.
371+
* Returns true if this HttpHeaders contains a header for the specified name.
372372
*
373373
* @param name header name
374-
* @return <tt>true</tt> if this HttpHeaders contains a header for the specified name.
374+
* @return true if this HttpHeaders contains a header for the specified name.
375375
*/
376376
public boolean has(String name) {
377377
return headers.containsKey(name);
378378
}
379379

380380
/**
381-
* Returns <tt>false</tt> if this HttpHeaders contains no header for the specified name.
381+
* Returns false if this HttpHeaders contains no header for the specified name.
382382
*
383383
* @param name header name
384-
* @return <tt>true</tt> if this HttpHeaders contains no header for the specified name.
384+
* @return true if this HttpHeaders contains no header for the specified name.
385385
*/
386386
public boolean hasNot(String name) {
387387
return !headers.containsKey(name);
388388
}
389389

390390
/**
391-
* Returns <tt>true</tt> if this HttpHeaders contains a header for the specified name and header value is equal with given.
391+
* Returns true if this HttpHeaders contains a header for the specified name and header value is equal with given.
392392
*
393-
* @param name header name
393+
* @param name header name
394394
* @param value header value
395-
* @return <tt>true</tt> if this HttpHeaders contains a header for the specified name and header value is equal with given.
395+
* @return true if this HttpHeaders contains a header for the specified name and header value is equal with given.
396396
*/
397397
public boolean hasAndEqual(String name, String value) {
398398
final String header = headers.get(name);
@@ -413,18 +413,18 @@ public int size() {
413413
}
414414

415415
/**
416-
* Returns <tt>true</tt> if this HttpHeaders contains no headers.
416+
* Returns true if this HttpHeaders contains no headers.
417417
*
418-
* @return <tt>true</tt> if this HttpHeaders contains no headers
418+
* @return true if this HttpHeaders contains no headers
419419
*/
420420
public boolean isEmpty() {
421421
return headers.isEmpty();
422422
}
423423

424424
/**
425-
* Returns <tt>true</tt> if this HttpHeaders contains headers.
425+
* Returns true if this HttpHeaders contains headers.
426426
*
427-
* @return <tt>true</tt> if this HttpHeaders contains headers
427+
* @return true if this HttpHeaders contains headers
428428
*/
429429
public boolean isNotEmpty() {
430430
return headers.size() > 0;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2022 - Alex Danilenko
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 spotty.common.response;
17+
18+
import spotty.common.http.HttpHeaders;
19+
import spotty.common.stream.output.SpottyByteArrayOutputStream;
20+
21+
import static java.nio.charset.StandardCharsets.UTF_8;
22+
23+
public final class ResponseHeadersWriter {
24+
private static final byte[] HEADER_SPLITTER = ": ".getBytes(UTF_8);
25+
private static final byte[] SPACE = " ".getBytes(UTF_8);
26+
private static final byte[] CONTENT_LENGTH = HttpHeaders.CONTENT_LENGTH.getBytes(UTF_8);
27+
private static final byte[] CONTENT_TYPE = HttpHeaders.CONTENT_TYPE.getBytes(UTF_8);
28+
private static final byte[] SET_COOKIE = HttpHeaders.SET_COOKIE.getBytes(UTF_8);
29+
30+
public static void write(SpottyByteArrayOutputStream writer, SpottyResponse response) {
31+
writer.print(response.protocol().code); writer.write(SPACE); writer.print(response.status().toString());
32+
writer.println();
33+
34+
writer.write(CONTENT_LENGTH); writer.write(HEADER_SPLITTER); writer.print(Integer.toString(response.contentLength()));
35+
writer.println();
36+
37+
if (response.contentType() != null) {
38+
writer.write(CONTENT_TYPE); writer.write(HEADER_SPLITTER); writer.print(response.contentType());
39+
writer.println();
40+
}
41+
42+
response.headers()
43+
.forEach((name, value) -> {
44+
writer.print(name); writer.write(HEADER_SPLITTER); writer.print(value);
45+
writer.println();
46+
});
47+
48+
response.cookies()
49+
.forEach(cookie -> {
50+
writer.write(SET_COOKIE); writer.write(HEADER_SPLITTER); writer.print(cookie.toString());
51+
writer.println();
52+
});
53+
54+
writer.println();
55+
}
56+
57+
}

core/src/main/java/spotty/common/response/ResponseWriter.java

Lines changed: 0 additions & 72 deletions
This file was deleted.

core/src/main/java/spotty/common/session/Session.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public Session putIfAbsent(Object key, Object value) {
112112
*
113113
* @param key key with which the specified value is to be associated
114114
* @param mapper the function to compute a value
115-
* @param <T> return type
115+
* @param <T> return type
116116
* @return the current (existing or computed) value associated with
117117
* the specified key, or null if the computed value is null
118118
*/
@@ -132,7 +132,7 @@ public <T> T computeIfAbsent(Object key, Function<Object, T> mapper) {
132132
*
133133
* @param key key with which the specified value is to be associated
134134
* @param mapper the function to compute a value
135-
* @param <T> return type
135+
* @param <T> return type
136136
* @return the current (existing or computed) value associated with
137137
* the specified key, or null if the computed value is null
138138
*/
@@ -154,7 +154,7 @@ public <T> T computeIfPresent(Object key, BiFunction<Object, T, T> mapper) {
154154
*
155155
* @param key key with which the specified value is to be associated
156156
* @param mapper the function to compute a value
157-
* @param <T> return type
157+
* @param <T> return type
158158
* @return the new value associated with the specified key, or null if none
159159
*/
160160
@SuppressWarnings("all")
@@ -192,7 +192,7 @@ public <T> T get(Object key) {
192192
*
193193
* @param key the key whose associated value is to be returned
194194
* @param defaultValue the default mapping of the key
195-
* @param <T> return type
195+
* @param <T> return type
196196
* @return the value to which the specified key is mapped, or
197197
* {@code defaultValue} if this map contains no mapping for the key
198198
*/
@@ -209,14 +209,14 @@ public int size() {
209209
}
210210

211211
/**
212-
* @return <tt>true</tt> if this session contains no key-value mappings
212+
* @return true if this session contains no key-value mappings
213213
*/
214214
public boolean isEmpty() {
215215
return data.isEmpty();
216216
}
217217

218218
/**
219-
* @return <tt>true</tt> if this session contains key-value mappings
219+
* @return true if this session contains key-value mappings
220220
*/
221221
public boolean isNotEmpty() {
222222
return data.size() > 0;
@@ -271,31 +271,31 @@ public Set<Map.Entry<Object, Object>> entrySet() {
271271
}
272272

273273
/**
274-
* Returns <tt>true</tt> if this session contains a mapping for the specified key.
274+
* Returns true if this session contains a mapping for the specified key.
275275
*
276276
* @param key key whose presence in this session is to be tested
277-
* @return <tt>true</tt> if this session contains a mapping for the specified key
277+
* @return true if this session contains a mapping for the specified key
278278
*/
279279
public boolean has(Object key) {
280280
return data.containsKey(key);
281281
}
282282

283283
/**
284-
* Returns <tt>true</tt> if this session contains no mapping for the specified key.
284+
* Returns true if this session contains no mapping for the specified key.
285285
*
286286
* @param key key whose presence in this session is to be tested
287-
* @return <tt>true</tt> if this session contains no mapping for the specified key
287+
* @return true if this session contains no mapping for the specified key
288288
*/
289289
public boolean hasNot(Object key) {
290290
return !data.containsKey(key);
291291
}
292292

293293
/**
294-
* Returns <tt>true</tt> if this session contains a value for the specified key and value is equal with given.
294+
* Returns true if this session contains a value for the specified key and value is equal with given.
295295
*
296296
* @param key key whose presence in this session is to be tested
297297
* @param value value whose presence in this session is to be tested
298-
* @return <tt>true</tt> if this session contains a value for the specified key and value is equal with given.
298+
* @return true if this session contains a value for the specified key and value is equal with given.
299299
*/
300300
public boolean hasAndEqual(Object key, Object value) {
301301
final Object foundValue = data.get(key);

core/src/main/java/spotty/common/stream/output/SpottyFixedByteOutputStream.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,15 @@ public boolean isFull() {
9797
return size == limit;
9898
}
9999

100+
public byte[] sourceData() {
101+
return data;
102+
}
103+
100104
public byte[] toByteArray() {
105+
if (size == data.length) {
106+
return data;
107+
}
108+
101109
return Arrays.copyOf(data, size);
102110
}
103111

@@ -115,7 +123,7 @@ public void capacity(int capacity) {
115123
}
116124

117125
if (data.length != capacity) {
118-
byte[] d = new byte[capacity];
126+
final byte[] d = new byte[capacity];
119127
this.size = min(size, capacity);
120128

121129
if (size > 0) {

core/src/main/java/spotty/server/Server.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ private void accept(SelectionKey acceptKey) throws IOException {
260260
private void registerConnection(Connection connection, Selector selector) {
261261
final SelectionKey key = connection.register(selector);
262262
if (key == null) {
263+
connection.close();
263264
return;
264265
}
265266

0 commit comments

Comments
 (0)