@Override
    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();
            line.append(HC_CACHE_KEY);
            line.append(": ");
            line.append(key);
            outputBuffer.writeLine(line, out);

            if (resource != null) {
                line.clear();
                line.append(HC_CACHE_LENGTH);
                line.append(": ");
                line.append(asStr(resource.length()));
                outputBuffer.writeLine(line, out);
            }

            line.clear();
            line.append(HC_REQUEST_INSTANT);
            line.append(": ");
            line.append(asStr(cacheEntry.getRequestInstant()));
            outputBuffer.writeLine(line, out);

            line.clear();
            line.append(HC_RESPONSE_INSTANT);
            line.append(": ");
            line.append(asStr(cacheEntry.getResponseInstant()));
            outputBuffer.writeLine(line, out);

            for (final String variant : cacheEntry.getVariants()) {
                line.clear();
                line.append(HC_VARIANT);
                line.append(": ");
                line.append(variant);
                outputBuffer.writeLine(line, out);
            }
            line.clear();
            outputBuffer.writeLine(line, out);

            line.clear();
            final RequestLine requestLine = new RequestLine(cacheEntry.getRequestMethod(), cacheEntry.getRequestURI(), HttpVersion.HTTP_1_1);
            lineFormatter.formatRequestLine(line, requestLine);
            outputBuffer.writeLine(line, out);
            for (final Iterator<Header> it = cacheEntry.requestHeaderIterator(); it.hasNext(); ) {
                line.clear();
                lineFormatter.formatHeader(line, it.next());
                outputBuffer.writeLine(line, out);
            }
            line.clear();
            outputBuffer.writeLine(line, out);

            line.clear();
            final StatusLine statusLine = new StatusLine(HttpVersion.HTTP_1_1, cacheEntry.getStatus(), "");
            lineFormatter.formatStatusLine(line, statusLine);
            outputBuffer.writeLine(line, out);
            for (final Iterator<Header> it = cacheEntry.headerIterator(); it.hasNext(); ) {
                line.clear();
                lineFormatter.formatHeader(line, it.next());
                outputBuffer.writeLine(line, out);
            }
            line.clear();
            outputBuffer.writeLine(line, out);
            outputBuffer.flush(out);

            if (resource != null) {
                out.write(resource.get());
            }
            out.flush();

            final byte[] bytes = out.toByteArray();

            if (LOG.isDebugEnabled()) {
                LOG.debug("Serialized cache entry with key {} and {} bytes", key, bytes.length);
            }
            return bytes;
        } catch (final IOException ex) {
            throw new ResourceIOException("Exception while serializing cache entry", ex);
        }
    }
Serializes an HttpCacheStorageEntry object into a byte array using an HTTP-like format.
<p>
The metadata is encoded into HTTP pseudo-headers for storage.

@param storageEntry the HttpCacheStorageEntry to serialize.
@return the byte array containing the serialized HttpCacheStorageEntry.
@throws ResourceIOException if there is an error during serialization.
httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpByteArrayCacheEntrySerializer.java