Class DataInputOutputHandler
- java.lang.Object
-
- com.avpkit.core.io.DataInputOutputHandler
-
- All Implemented Interfaces:
IURLProtocolHandler
public class DataInputOutputHandler extends java.lang.Object implements IURLProtocolHandler
Implementation of URLProtocolHandler that can read fromDataInputobjects or write toDataOutputobjects.The
IURLProtocolHandler.URL_RDWRmode is not supported unless both input and output objects are the sameRandomAccessFileIURLProtocolHandler.isStreamed(String, int)will return true, unless this class is reading from or writing to aRandomAccessFile;- Author:
- aclarke
-
-
Field Summary
Fields Modifier and Type Field Description static booleanCLOSE_STREAM_ON_CLOSEWhether or not this class will default callingCloseable.close()on a stream whenclose()is called on this method.-
Fields inherited from interface com.avpkit.core.io.IURLProtocolHandler
SEEK_CUR, SEEK_END, SEEK_SET, SEEK_SIZE, URL_RDONLY_MODE, URL_RDWR, URL_WRONLY_MODE
-
-
Constructor Summary
Constructors Constructor Description DataInputOutputHandler(java.io.DataInput in)Create a handler that can only be opened for reading.DataInputOutputHandler(java.io.DataInput in, java.io.DataOutput out, boolean closeStreamOnClose)Creates a new handler.DataInputOutputHandler(java.io.DataOutput out)Create a handler that can only be opened for writing.DataInputOutputHandler(java.io.RandomAccessFile file)Create a handler that can only be opened for reading, writing and seeking.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intclose()A request to close() from FFMPEGjava.io.DataInputgetDataInput()Returns theDataInputwe'd input from if asked.java.io.DataOutputgetDataOutput()Returns theDataOutputwe'd output to if asked.java.lang.ObjectgetOpen()Returns the object currentlyopen(String, int).booleanisCloseStreamOnClose()Will this handler callCloseable.close()automatically when itsclose()method is called by AVPKit? We will check if the object we're using supportsCloseable.booleanisStreamed(java.lang.String url, int flags)Special callback made by AVPKit in order to determine if your stream supports streaming.intopen(java.lang.String url, int flags)This method gets called by FFMPEG when it opens a file.intread(byte[] buf, int size)This method gets called by FFMPEG when it tries to read data.longseek(long offset, int whence)A request from FFMPEG to seek to a position in the stream.intwrite(byte[] buf, int size)This method gets called by FFMPEG when it tries to write data.
-
-
-
Field Detail
-
CLOSE_STREAM_ON_CLOSE
public static final boolean CLOSE_STREAM_ON_CLOSE
Whether or not this class will default callingCloseable.close()on a stream whenclose()is called on this method. Can be overriden in constructors.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DataInputOutputHandler
public DataInputOutputHandler(java.io.DataInput in)
Create a handler that can only be opened for reading.- Parameters:
in- an input object.
-
DataInputOutputHandler
public DataInputOutputHandler(java.io.DataOutput out)
Create a handler that can only be opened for writing.- Parameters:
out- an output object
-
DataInputOutputHandler
public DataInputOutputHandler(java.io.RandomAccessFile file)
Create a handler that can only be opened for reading, writing and seeking.- Parameters:
file- a file to use.
-
DataInputOutputHandler
public DataInputOutputHandler(java.io.DataInput in, java.io.DataOutput out, boolean closeStreamOnClose)
Creates a new handler. If you pass in non null arguments for both in and out, the handler may be opened in eitherIURLProtocolHandler.URL_RDONLY_MODEorIURLProtocolHandler.URL_WRONLY_MODE, but not at the same time.IURLProtocolHandler.URL_RDWRmode is supported by this handler if both in and out are instances ofRandomAccessFile.- Parameters:
in- The object to read from if asked to.out- The object to write from if asked to.closeStreamOnClose- Whether or not to automatically callCloseable.close()on the given channel whenclose()is called.
-
-
Method Detail
-
close
public int close()
A request to close() from FFMPEG- Specified by:
closein interfaceIURLProtocolHandler- Returns:
- -1 on error; else >= 0
-
open
public int open(java.lang.String url, int flags)
This method gets called by FFMPEG when it opens a file.- Specified by:
openin interfaceIURLProtocolHandler- Parameters:
url- The URL to openflags- The flags (e.g.IURLProtocolHandler.URL_RDONLY_MODE)- Returns:
- >= 0 for success; -1 for error.
-
read
public int read(byte[] buf, int size)
This method gets called by FFMPEG when it tries to read data.For non-blocking IO, return:
IError.typeToErrorNumber(com.avpkit.core.IError.Type)pass inIError.Type.ERROR_AGAINfor the error type. This returns the platform specific number for EAGAIN on your platform signaling that callers should try again later.Alternately implementors may block until data is returning, but they should then respect the
Thread.isInterrupted()setting.- Specified by:
readin interfaceIURLProtocolHandler- Parameters:
buf- The buffer to write your data to.size- The number of bytes in buf data available for you to write the data that FFMPEG will read.- Returns:
- 0 for end of file, else number of bytes you wrote to the buffer, or -1 if error.
-
seek
public long seek(long offset, int whence)
A request from FFMPEG to seek to a position in the stream. This method is not supported on this class and always return -1;- Specified by:
seekin interfaceIURLProtocolHandler- Parameters:
offset- The offset in bytes.whence- Where that offset is relative to. Follow the C stdlib fseek() conventions EXCEPTIURLProtocolHandler.SEEK_SIZEshould return back the size of the stream in bytes if known without adjusting the seek pointer.- Returns:
- -1 if not supported, else the position relative to whence
-
write
public int write(byte[] buf, int size)
This method gets called by FFMPEG when it tries to write data.For non-blocking IO, return:
IError.typeToErrorNumber(com.avpkit.core.IError.Type)pass inIError.Type.ERROR_AGAINfor the error type. This returns the platform specific number for EAGAIN on your platform signaling that callers should try again later.Alternately implementators may block until data is returning, but they should then respect the
Thread.isInterrupted()setting.- Specified by:
writein interfaceIURLProtocolHandler- Parameters:
buf- The data you should write.size- The number of bytes in buf.- Returns:
- 0 for end of file, else number of bytes you read from buf, or -1 if error.
-
isStreamed
public boolean isStreamed(java.lang.String url, int flags)
Special callback made by AVPKit in order to determine if your stream supports streaming.If this method returns true, AVPKit will assume it cannot seek backwards in this container.
This has one main practical consequence. When writing it means certain container formats (e.g. the MOV container) will not be usable as it requires seeking back to the start of a file to write MOV required header information once the entire file has been encoded.
But if your medium is streaming, you'll want to return true for this, and then FFMPEG will not attempt to seek back in time.
Always true, unless we're inputting or outputting to aRandomAccessFile.- Specified by:
isStreamedin interfaceIURLProtocolHandler- Parameters:
url- The URL that would be passed toIURLProtocolHandler.open(String, int)flags- The flags that would be passed toIURLProtocolHandler.open(String, int)- Returns:
- true if you can stream that URL; false if not.
-
getDataInput
public java.io.DataInput getDataInput()
Returns theDataInputwe'd input from if asked.- Returns:
- the
DataInput
-
getDataOutput
public java.io.DataOutput getDataOutput()
Returns theDataOutputwe'd output to if asked.- Returns:
- the
DataOutput
-
getOpen
public java.lang.Object getOpen()
Returns the object currentlyopen(String, int).- Returns:
- the open object
-
isCloseStreamOnClose
public boolean isCloseStreamOnClose()
Will this handler callCloseable.close()automatically when itsclose()method is called by AVPKit? We will check if the object we're using supportsCloseable.- Returns:
- the closeStreamOnClose setting
-
-