Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31ba41e

Browse files
committed
Add writeAndFlush method in Response interface.
1 parent ef2e6a2 commit 31ba41e

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/main/java/org/tinystruct/http/Response.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.tinystruct.http;
22

3+
import org.tinystruct.ApplicationException;
4+
35
import java.io.IOException;
46

57
public interface Response<T, O> extends Protocol {
@@ -28,5 +30,7 @@ public interface Response<T, O> extends Protocol {
2830

2931
void sendRedirect(String url) throws IOException;
3032

33+
void writeAndFlush(byte[] bytes) throws ApplicationException;
34+
3135
O get();
3236
}

src/main/java/org/tinystruct/http/ResponseBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.netty.handler.codec.http.FullHttpResponse;
44
import io.netty.handler.codec.http.HttpHeaders;
55
import io.netty.handler.codec.http.HttpResponseStatus;
6+
import org.tinystruct.ApplicationException;
67

78
import java.io.IOException;
89

@@ -92,4 +93,13 @@ public void sendRedirect(String url) throws IOException {
9293
this.setStatus(ResponseStatus.TEMPORARY_REDIRECT);
9394
}
9495

96+
/**
97+
* @param bytes
98+
* @throws ApplicationException
99+
*/
100+
@Override
101+
public void writeAndFlush(byte[] bytes) throws ApplicationException {
102+
this.get().content().writeBytes(bytes);
103+
}
104+
95105
}

src/main/java/org/tinystruct/http/servlet/ResponseBuilder.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import jakarta.servlet.ServletOutputStream;
44
import jakarta.servlet.http.HttpServletResponse;
5+
import org.tinystruct.ApplicationException;
56
import org.tinystruct.http.*;
67

78
import java.io.IOException;
@@ -100,4 +101,18 @@ public void sendRedirect(String url) throws IOException {
100101
if (!this.response.isCommitted())
101102
this.response.sendRedirect(url);
102103
}
104+
105+
/**
106+
* @param bytes
107+
* @throws ApplicationException
108+
*/
109+
@Override
110+
public void writeAndFlush(byte[] bytes) throws ApplicationException {
111+
try {
112+
this.get().write(bytes);
113+
this.get().flush();
114+
} catch (IOException e) {
115+
throw new RuntimeException(e);
116+
}
117+
}
103118
}

0 commit comments

Comments
 (0)