Skip to content

Instantly share code, notes, and snippets.

@ggtools
Created June 5, 2014 11:14
Show Gist options
  • Save ggtools/206c244e1ee2ca6c021b to your computer and use it in GitHub Desktop.
Save ggtools/206c244e1ee2ca6c021b to your computer and use it in GitHub Desktop.
HttpMessageConverter handling video streams
public class VideoHttpMessageConverter extends AbstractHttpMessageConverter {
public VideoHttpMessageConverter() {
super(new MediaType("video", "mp4"));
}
@Override
protected boolean supports(Class clazz) {
return InputStream.class.isAssignableFrom(clazz) || File.class.isAssignableFrom(clazz);
}
@Override
protected Object readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
InputStream source = inputMessage.getBody();
File file = getOutputMediaFile();
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
ByteStreams.copy(source, os);
return file;
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
// log if
}
}
}
}
private static File getOutputMediaFile() {
// Do what ever you want
}
@Override
protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
if (!(o instanceof InputStream)) {
throw new HttpMessageNotReadableException("Object class " + o.getClass() + " is not supported");
}
ByteStreams.copy((InputStream) o, outputMessage.getBody());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment