package example;

import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.FunctionComputeLogger;
import com.aliyun.fc.runtime.StreamRequestHandler;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

public class StreamHandler implements StreamRequestHandler {

	@Override
	public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
		FunctionComputeLogger logger = context.getLogger();
		ByteArrayOutputStream result = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		for (int length; (length = input.read(buffer)) != -1;) {
			result.write(buffer, 0, length);
		}
		String res = result.toString(StandardCharsets.UTF_8.name());
		logger.info("Input data: " + res);
		output.write(res.getBytes());
	}

}