class XlsSerializer implements Serializer {

    @Override
    public InputStream serialize(InputStream givenInputStream, DataSetMetadata metadata, long limit) {
        try {

            PipedInputStream pipe = new PipedInputStream();
            PipedOutputStream jsonOutput = new PipedOutputStream(pipe);

            // override the parameter in case it needs to be wrapped in a buffered inputstream
            InputStream inputStream = givenInputStream;
            if (!inputStream.markSupported()) {
                inputStream = new BufferedInputStream(inputStream);
            }

            inputStream.mark(Integer.MAX_VALUE);

            boolean newExcelFormat = XlsUtils.isNewExcelFormat(inputStream);

            inputStream.reset();

            Runnable runnable = newExcelFormat ? //
                    serializeNew(inputStream, metadata, limit, jsonOutput)
                    : serializeOld(inputStream, metadata, limit, jsonOutput);

            // Serialize asynchronously for better performance (especially if caller doesn't consume all, see sampling).
            executor.execute(runnable);

            return pipe;
        } catch (IOException e) {
            throw new TDPException(CommonErrorCodes.UNABLE_TO_SERIALIZE_TO_JSON, e);
        }
    }

}