package vn.sis.appointment; import com.sun.net.httpserver.HttpExchange; import java.io.IOException; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.util.LinkedHashMap; import java.util.Map; final class FormData { private static final int MAX_REQUEST_BYTES = 256 * 1024; private FormData() { } static Map read(HttpExchange exchange) throws IOException { byte[] data = exchange.getRequestBody().readNBytes(MAX_REQUEST_BYTES + 1); if (data.length > MAX_REQUEST_BYTES) { throw new IllegalArgumentException("Du lieu gui len vuot qua gioi han 256 KB."); } String body = new String(data, StandardCharsets.UTF_8); Map result = new LinkedHashMap<>(); if (body.isBlank()) { return result; } for (String pair : body.split("&")) { int separator = pair.indexOf('='); String rawKey = separator >= 0 ? pair.substring(0, separator) : pair; String rawValue = separator >= 0 ? pair.substring(separator + 1) : ""; result.put(decode(rawKey), decode(rawValue)); } return result; } private static String decode(String value) { return URLDecoder.decode(value, StandardCharsets.UTF_8); } }