Skip to content

Instantly share code, notes, and snippets.

@lpicanco
Created January 21, 2016 17:54
Show Gist options
  • Save lpicanco/a2d00399f69850140cef to your computer and use it in GitHub Desktop.
Save lpicanco/a2d00399f69850140cef to your computer and use it in GitHub Desktop.
public static MultivaluedMap<String, String> parseForm(InputStream entityStream)
throws IOException
{
char[] buffer = new char[100];
StringBuffer buf = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(entityStream));
int wasRead = 0;
do
{
wasRead = reader.read(buffer, 0, 100);
if (wasRead > 0) buf.append(buffer, 0, wasRead);
} while (wasRead > -1);
String form = buf.toString();
MultivaluedMap<String, String> formData = new MultivaluedMapImpl<String, String>();
if ("".equals(form)) return formData;
String[] params = form.split("&");
for (String param : params)
{
if (param.indexOf('=') >= 0)
{
String[] nv = param.split("=");
String val = nv.length > 1 ? nv[1] : "";
formData.add(nv[0], val);
}
else
{
formData.add(param, "");
}
}
return formData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment