I found this code snippet in latest Android notepad tutorial link which is using contentprovider and implementing PipeDataWriter.
The interface has its method writeDataToPipe which they implemented like this:
@Override
public void writeDataToPipe(ParcelFileDescriptor output, Uri uri, String mimeType, Bundle opts, Cursor c) {
// We currently only support conversion-to-text from a single note entry,
// so no need for cursor data type checking here.
FileOutputStream fout = new FileOutputStream(output.getFileDescriptor());
PrintWriter pw = null;
try {
pw = new PrintWriter(new OutputStreamWriter(fout, "UTF-8"));
pw.println(c.getString(READ_NOTE_TITLE_INDEX));
pw.println("");
pw.println(c.getString(READ_NOTE_NOTE_INDEX));
} catch (UnsupportedEncodingException e) {
Log.w(TAG, "Ooops", e);
} finally {
c.close();
if (pw != null) {
pw.flush();
}
try {
fout.close();
} catch (IOException e) {
}
}
}
My doubt is that why specifically they are using PipeDataWriter?
Is that some kind of design pattern?
I found no other source where it has been used. Why so?
They are using
openPipeHelper()in their implementation ofopenTypedAssetFile().openPipeHelper()takes aPipeDataWriteras a parameter. In their case, they implementedPipeDataWriteron theNotePadProvideritself, and therefore need to implementopenPipeHelper()to fulfill the contract required by thePipeDataWriterinterface.PipeDataWriterandopenPipeHelper()are new to API Level 11. Previously, you had to roll your own solution for forking a thread to return the contents of a file.