Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/*
* Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
* Copyright (c) 2010-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
package com.marklogic.client.impl;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Reader;
import java.io.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -117,4 +113,127 @@ public void close() {
enabled = false;
}

// Moved here in the 8.2 release to make it obvious it's only used by RequestLoggerImpl. It is only used by the
// copyContent method in RequestLoggerImpl. So while Polaris rightfully complains about the thread safety issues of
// this class, it is only used in a single thread context and so the thread safety issues are not a problem.
private static class InputStreamTee extends InputStream {
private InputStream in;
Comment on lines +116 to +120
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The newly added inner class uses tab indentation, while the rest of RequestLoggerImpl uses spaces. This makes the file inconsistent and can cause style/lint failures; please re-indent this block to match the existing spacing in the file.

Copilot uses AI. Check for mistakes.
private OutputStream tee;
private long max = 0;
private long sent = 0;

public InputStreamTee(InputStream in, OutputStream tee, long max) {
super();
this.in = in;
this.tee = tee;
this.max = max;
}

@Override
public int read() throws IOException {
if (in == null) return -1;

if (sent >= max) return in.read();

int b = in.read();
if (b == -1) {
cleanupTee();
return b;
}

if (max == Long.MAX_VALUE) {
tee.write(b);
return b;
}

tee.write(b);

sent++;
if (sent == max) cleanupTee();

return b;
}

@Override
public int read(byte[] b) throws IOException {
if (in == null) return -1;

if (sent >= max) return in.read(b);

return readTee(b, 0, in.read(b));
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
if (in == null) return -1;

if (sent >= max) return in.read(b, off, len);

return readTee(b, 0, in.read(b, off, len));
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In read(byte[] b, int off, int len), the call to readTee passes an offset of 0 instead of the provided 'off'. This causes the tee stream to write the wrong slice of the buffer (and can log stale bytes that precede 'off'). Pass 'off' through to readTee so tee.write uses the correct data.

Suggested change
return readTee(b, 0, in.read(b, off, len));
return readTee(b, off, in.read(b, off, len));

Copilot uses AI. Check for mistakes.
}

private int readTee(byte[] b, int off, int resultLen) throws IOException {
if (resultLen < 1) {
if (resultLen == -1) cleanupTee();
return resultLen;
}

if (max == Long.MAX_VALUE) {
tee.write(b, off, resultLen);
return resultLen;
}

int teeLen = ((sent + resultLen) <= max) ? resultLen : (int) (max - sent);
sent += teeLen;
tee.write(b, off, teeLen);

if (sent >= max) cleanupTee();

return resultLen;
}

private void cleanupTee() throws IOException {
if (tee == null) return;
tee.flush();
tee = null;
}

@Override
public void close() throws IOException {
if (in == null) return;
in.close();
in = null;
cleanupTee();
}

@Override
public int available() throws IOException {
if (in == null) return 0;
return in.available();
}

@Override
public boolean markSupported() {
if (in == null) return false;
return in.markSupported();
}

@Override
public synchronized void mark(int readLimit) {
if (in == null) return;
in.mark(readLimit);
}

@Override
public synchronized void reset() throws IOException {
if (in == null) throw new IOException("Input Stream closed");
in.reset();
}

@Override
public long skip(long n) throws IOException {
if (in == null) throw new IOException("Input Stream closed");
return in.skip(n);
}
}
}