ok2c commented on code in PR #453: URL: https://github.com/apache/httpcomponents-client/pull/453#discussion_r1225287521
########## httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpByteArrayCacheEntrySerializer.java: ########## @@ -163,57 +125,98 @@ public HttpByteArrayCacheEntrySerializer() { * <p> * The metadata is encoded into HTTP pseudo-headers for storage. * - * @param httpCacheEntry the HttpCacheStorageEntry to serialize. + * @param storageEntry the HttpCacheStorageEntry to serialize. * @return the byte array containing the serialized HttpCacheStorageEntry. * @throws ResourceIOException if there is an error during serialization. */ @Override - public byte[] serialize(final HttpCacheStorageEntry httpCacheEntry) throws ResourceIOException { - if (httpCacheEntry.getKey() == null) { - throw new IllegalStateException("Cannot serialize cache object with null storage key"); - } - // content doesn't need null-check because it's validated in the HttpCacheStorageEntry constructor - - // Fake HTTP request, required by response generator - // Use request method from httpCacheEntry, but as far as I can tell it will only ever return "GET". - final HttpRequest httpRequest = new BasicHttpRequest(httpCacheEntry.getContent().getRequestMethod(), "/"); - - final SimpleHttpResponse httpResponse = cachedHttpResponseGenerator.generateResponse(httpRequest, httpCacheEntry.getContent()); - final int size = httpResponse.getHeaders().length + (httpResponse.getBodyBytes() != null ? httpResponse.getBodyBytes().length : 0); - try (ByteArrayOutputStream out = new ByteArrayOutputStream(size)) { - escapeHeaders(httpResponse); - addMetadataPseudoHeaders(httpResponse, httpCacheEntry); - - final byte[] bodyBytes = httpResponse.getBodyBytes(); - int resourceLength = 0; - - if (bodyBytes == null) { - // This means no content, for example a 204 response - httpResponse.addHeader(SC_HEADER_NAME_NO_CONTENT, Boolean.TRUE.toString()); - } else { - resourceLength = bodyBytes.length; - } + public byte[] serialize(final HttpCacheStorageEntry storageEntry) throws ResourceIOException { + final String key = storageEntry.getKey(); + final HttpCacheEntry cacheEntry = storageEntry.getContent(); + final Resource resource = cacheEntry.getResource(); + try (ByteArrayOutputStream out = new ByteArrayOutputStream((resource != null ? (int) resource.length() : 0) + DEFAULT_BUFFER_SIZE)) { final SessionOutputBuffer outputBuffer = new SessionOutputBufferImpl(bufferSize); + final CharArrayBuffer line = new CharArrayBuffer(DEFAULT_BUFFER_SIZE); + + line.append(HC_CACHE_VERSION_LINE); + outputBuffer.writeLine(line, out); + + line.clear(); Review Comment: @arturobernalg I disagree. This is really a matter of taste. I personally find a utility method with 4 arguments much uglier than 5 repetitive trivial lines. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org For additional commands, e-mail: dev-h...@hc.apache.org