Created
June 5, 2014 11:14
-
-
Save ggtools/206c244e1ee2ca6c021b to your computer and use it in GitHub Desktop.
HttpMessageConverter handling video streams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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