/** * Read the bytes of an input stream and convert to a string. * * @param in the input stream to read from. * @param type the media type that determines the character set defining * how to decode bytes to characters. * @return the string. * * @throws IOException if there is an error reading from the input stream. */ public static String readFromAsString(InputStream in, MediaType type) throws IOException { return readFromAsString(new InputStreamReader(in, getCharset(type))); }
/** * Reader bytes from an input stream and write then to an output stream. * * @param in the input stream to read from. * @param out the output stream to write to. * @throws IOException if there is an error reading or writing bytes. */ public static void writeTo(InputStream in, OutputStream out) throws IOException { ReaderWriter.writeTo(in, out); }
/** * Buffer the entity stream (if not empty). * * @return {@code true} if the entity input stream was successfully buffered. * @throws javax.ws.rs.ProcessingException in case of an IO error. */ public boolean bufferEntity() throws ProcessingException { entityContent.ensureNotClosed(); try { if (entityContent.isBuffered() || !entityContent.hasContent()) { return true; } final InputStream entityStream = entityContent.getWrappedStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ReaderWriter.writeTo(entityStream, baos); } finally { // Workaround for JRFCAF-1344: the underlying stream close() implementation may be thread-unsafe // and as such the close() may result in an IOException at the socket input stream level, // if the close() gets called at once from multiple threads somehow. // We want to ignore these exceptions in the readEntity/bufferEntity operations though. ReaderWriter.safelyClose(entityStream); } entityContent.setContent(new ByteArrayInputStream(baos.toByteArray()), true); return true; } catch (IOException ex) { throw new ProcessingException(LocalizationMessages.MESSAGE_CONTENT_BUFFERING_FAILED(), ex); } }
/** * Read the bytes of an input stream and convert to a string. * * @param in the input stream to read from. * @param type the media type that determines the character set defining * how to decode bytes to characters. * @return the string. * * @throws IOException if there is an error reading from the input stream. */ public static String readFromAsString(InputStream in, MediaType type) throws IOException { return ReaderWriter.readFromAsString(in, type); }
/** * Get the character set from a media type. * <p> * The character set is obtained from the media type parameter "charset". * If the parameter is not present the {@code UTF8} charset is utilized. * * @param media the media type. * @return the character set. */ public static Charset getCharset(final MediaType media) { return ReaderWriter.getCharset(media); }
ReaderWriter.safelyClose(stream);
/** * Convert a string to bytes and write those bytes to an output stream. * * @param s the string to convert to bytes. * @param out the output stream to write to. * @param type the media type that determines the character set defining * how to decode bytes to characters. * @throws IOException in case of a write failure. */ public static void writeToAsString(String s, OutputStream out, MediaType type) throws IOException { ReaderWriter.writeToAsString(s, out, type); }
/** * Read the bytes of an input stream and convert to a string. * * @param in the input stream to read from. * @param type the media type that determines the character set defining * how to decode bytes to characters. * @return the string. * * @throws IOException if there is an error reading from the input stream. */ public static String readFromAsString(InputStream in, MediaType type) throws IOException { return ReaderWriter.readFromAsString(in, type); }
/** * Get the character set from a media type. * <p> * The character set is obtained from the media type parameter "charset". * If the parameter is not present the {@link #UTF8} charset is utilized. * * @param m the media type. * @return the character set. */ public static Charset getCharset(MediaType m) { return ReaderWriter.getCharset(m); }
ReaderWriter.safelyClose(stream);
/** * Convert a string to bytes and write those bytes to an output stream. * * @param s the string to convert to bytes. * @param out the output stream to write to. * @param type the media type that determines the character set defining * how to decode bytes to characters. * @throws IOException in case of a write failure. */ public static void writeToAsString(String s, OutputStream out, MediaType type) throws IOException { ReaderWriter.writeToAsString(s, out, type); }
/** * Reader characters from an input stream and write then to an output stream. * * @param in the reader to read from. * @param out the writer to write to. * @throws IOException if there is an error reading or writing characters. */ public static void writeTo(Reader in, Writer out) throws IOException { ReaderWriter.writeTo(in, out); }
/** * Read the bytes of an input stream and convert to a string. * * @param in the input stream to read from. * @param type the media type that determines the character set defining * how to decode bytes to characters. * @return the string. * * @throws IOException if there is an error reading from the input stream. */ public static String readFromAsString(InputStream in, MediaType type) throws IOException { return readFromAsString(new InputStreamReader(in, getCharset(type))); }
/** * Buffer the entity stream (if not empty). * * @return {@code true} if the entity input stream was successfully buffered. * @throws javax.ws.rs.ProcessingException in case of an IO error. */ public boolean bufferEntity() throws ProcessingException { entityContent.ensureNotClosed(); try { if (entityContent.isBuffered() || !entityContent.hasContent()) { return true; } final InputStream entityStream = entityContent.getWrappedStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ReaderWriter.writeTo(entityStream, baos); } finally { // Workaround for JRFCAF-1344: the underlying stream close() implementation may be thread-unsafe // and as such the close() may result in an IOException at the socket input stream level, // if the close() gets called at once from multiple threads somehow. // We want to ignore these exceptions in the readEntity/bufferEntity operations though. ReaderWriter.safelyClose(entityStream); } entityContent.setContent(new ByteArrayInputStream(baos.toByteArray()), true); return true; } catch (IOException ex) { throw new ProcessingException(LocalizationMessages.MESSAGE_CONTENT_BUFFERING_FAILED(), ex); } }
@Override public TokenResult readFrom(Class<TokenResult> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { Map<String, Object> map = new HashMap<>(); final String str = ReaderWriter.readFromAsString(entityStream, mediaType); final String[] splitArray = str.split("&"); for (String s : splitArray) { final String[] keyValue = s.split("="); map.put(keyValue[0], keyValue[1]); } return new TokenResult(map); } }
/** * Get the character set from a media type. * <p> * The character set is obtained from the media type parameter "charset". * If the parameter is not present the {@code UTF8} charset is utilized. * * @param media the media type. * @return the character set. */ public static Charset getCharset(final MediaType media) { return ReaderWriter.getCharset(media); }
ReaderWriter.safelyClose(entityContent);
/** * Convert a string to bytes and write those bytes to an output stream. * * @param s the string to convert to bytes. * @param out the output stream to write to. * @param type the media type that determines the character set defining * how to decode bytes to characters. * @throws IOException in case of a write failure. */ public static void writeToAsString(String s, OutputStream out, MediaType type) throws IOException { ReaderWriter.writeToAsString(s, out, type); }
/** * Reader bytes from an input stream and write then to an output stream. * * @param in the input stream to read from. * @param out the output stream to write to. * @throws IOException if there is an error reading or writing bytes. */ public static void writeTo(InputStream in, OutputStream out) throws IOException { ReaderWriter.writeTo(in, out); }
/** * Read the bytes of an input stream and convert to a string. * * @param in the input stream to read from. * @param type the media type that determines the character set defining * how to decode bytes to characters. * @return the string. * * @throws IOException if there is an error reading from the input stream. */ public static String readFromAsString(InputStream in, MediaType type) throws IOException { return readFromAsString(new InputStreamReader(in, getCharset(type))); }