本文整理汇总了Java中nxt.util.CountingInputStream类的典型用法代码示例。如果您正苦于以下问题:Java CountingInputStream类的具体用法?Java CountingInputStream怎么用?Java CountingInputStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CountingInputStream类属于nxt.util包,在下文中一共展示了CountingInputStream类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doPost
import nxt.util.CountingInputStream; //导入依赖的package包/类
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PeerImpl peer = null;
JSONStreamAware response;
try {
peer = Peers.addPeer(req.getRemoteAddr(), null);
if (peer == null) {
return;
}
if (peer.isBlacklisted()) {
return;
}
JSONObject request;
CountingInputStream cis = new CountingInputStream(req.getInputStream());
try (Reader reader = new BufferedReader(new InputStreamReader(cis, "UTF-8"))) {
request = (JSONObject) JSONValue.parse(reader);
}
if (request == null) {
return;
}
if (peer.getState() == Peer.State.DISCONNECTED) {
peer.setState(Peer.State.CONNECTED);
}
peer.updateDownloadedVolume(cis.getCount());
if (! peer.analyzeHallmark(peer.getPeerAddress(), (String)request.get("hallmark"))) {
peer.blacklist();
return;
}
if (request.get("protocol") != null && ((Number)request.get("protocol")).intValue() == 1) {
PeerRequestHandler peerRequestHandler = peerRequestHandlers.get(request.get("requestType"));
if (peerRequestHandler != null) {
response = peerRequestHandler.processRequest(request, peer);
} else {
response = UNSUPPORTED_REQUEST_TYPE;
}
} else {
Logger.logDebugMessage("Unsupported protocol " + request.get("protocol"));
response = UNSUPPORTED_PROTOCOL;
}
} catch (RuntimeException e) {
Logger.logDebugMessage("Error processing POST request", e);
JSONObject json = new JSONObject();
json.put("error", e.toString());
response = json;
}
resp.setContentType("text/plain; charset=UTF-8");
CountingOutputStream cos = new CountingOutputStream(resp.getOutputStream());
try (Writer writer = new BufferedWriter(new OutputStreamWriter(cos, "UTF-8"))) {
response.writeJSONString(writer);
}
if (peer != null) {
peer.updateUploadedVolume(cos.getCount());
}
}