/** * Asynchronously gets the data from cache if available or fetches its content from the network. * * @return A Task that is resolved when the data has been fetched. */ public Task<byte[]> getDataInBackground() { return getDataInBackground((ProgressCallback) null); }
/** * Saves the file to the Parse cloud in a background thread. * * @return A Task that will be resolved when the save completes. */ public Task<Void> saveInBackground() { return saveInBackground((ProgressCallback) null); }
/** * Asynchronously gets the data stream from cached file if available or fetches its content from * the network, saves the content as cached file and returns the data stream of the cached file. * * @return A Task that is resolved when the data stream has been fetched. */ public Task<InputStream> getDataStreamInBackground() { return getDataStreamInBackground((ProgressCallback) null); }
/* package */ JSONObject encode() throws JSONException { JSONObject json = new JSONObject(); json.put("__type", "File"); json.put("name", getName()); String url = getUrl(); if (url == null) { throw new IllegalStateException("Unable to encode an unsaved ParseFile."); } json.put("url", getUrl()); return json; }
void writeToParcel(Parcel dest, ParseParcelEncoder encoder) { if (isDirty()) { throw new RuntimeException("Unable to parcel an unsaved ParseFile."); } dest.writeString(getUrl()); // Not null dest.writeString(getName()); // Not null String type = state.mimeType(); // Nullable dest.writeByte(type != null ? (byte) 1 : 0); if (type != null) { dest.writeString(type); } }
@Override public Task<Void> then(Task<Void> task) { if (!isDirty()) { return Task.forResult(null); saveTask = getFileController().saveAsync( state, data, cancellationToken); } else { saveTask = getFileController().saveAsync( state, file,
@Test public void testCancel() { ParseFile file = new ParseFile(new ParseFile.State.Builder().build()); TaskQueueTestHelper queueHelper = new TaskQueueTestHelper(file.taskQueue); queueHelper.enqueue(); List<Task<Void>> saveTasks = Arrays.asList( file.saveInBackground(), file.saveInBackground(), file.saveInBackground()); List<Task<byte[]>> getDataTasks = Arrays.asList( file.getDataInBackground(), file.getDataInBackground(), file.getDataInBackground()); file.cancel(); queueHelper.dequeue(); for (int i = 0; i < saveTasks.size(); i++) { assertTrue("Task #" + i + " was not cancelled", saveTasks.get(i).isCancelled()); } for (int i = 0; i < getDataTasks.size(); i++) { assertTrue("Task #" + i + " was not cancelled", getDataTasks.get(i).isCancelled()); } }
@Test public void testGetParseFileKeyExist() { final ParseFile file = new ParseFile( new ParseFile.State.Builder().name("image.png").url("http://yarr.com/image.png").build()); ParseFile fileAgain = new ParseFile( new ParseFile.State.Builder().name("file.txt").url("http://yarr.com/file.txt").build()); final Map<String, Object> params = new HashMap<>(); params.put("key", file); ParseConfig config = new ParseConfig(params); ParseFile fileConfig = config.getParseFile("key"); assertEquals(file.getName(), fileConfig.getName()); assertEquals(file.getUrl(), fileConfig.getUrl()); assertSame(fileConfig, config.getParseFile("key", fileAgain)); }
@Test public void testParcelable() { String mime = "mime"; String name = "name"; String url = "url"; ParseFile file = new ParseFile(new ParseFile.State.Builder() .name(name) .mimeType(mime) .url(url) .build()); Parcel parcel = Parcel.obtain(); file.writeToParcel(parcel, 0); parcel.setDataPosition(0); file = ParseFile.CREATOR.createFromParcel(parcel); assertEquals(file.getName(), name); assertEquals(file.getUrl(), url); assertEquals(file.getState().mimeType(), mime); assertFalse(file.isDirty()); }
ParseFile imgFile = new ParseFile("profileImg.png", bytearray); imgFile.saveInBackground(); userProfile.setPhotoFile(imgFile);
/** * Asynchronously gets the file pointer from cache if available or fetches its content from the * network. * <strong>Note: </strong> The {@link File} location may change without notice and should not be * stored to be accessed later. * * @return A Task that is resolved when the data has been fetched. */ public Task<File> getFileInBackground() { return getFileInBackground((ProgressCallback) null); }
ParseFile file = new ParseFile(state); Task<Void> saveTaskA = file.saveAsync(null, null, null); queueHelper.enqueue(); Task<byte[]> getDataTaskA = file.getDataInBackground(); queueHelper.enqueue(); Task<Void> saveTaskB = file.saveAsync(null, null, null); queueHelper.enqueue(); Task<byte[]> getDataTaskB = file.getDataInBackground();
ParseFile parseFile = new ParseFile(name, data, contentType); assertEquals("name", parseFile.getName()); assertEquals("content_type", parseFile.getState().mimeType()); assertTrue(parseFile.isDirty()); parseFile = new ParseFile(data); assertEquals("file", parseFile.getName()); // Default assertEquals(null, parseFile.getState().mimeType()); assertTrue(parseFile.isDirty()); parseFile = new ParseFile(name, data); assertEquals("name", parseFile.getName()); assertEquals(null, parseFile.getState().mimeType()); assertTrue(parseFile.isDirty()); parseFile = new ParseFile(data, contentType); assertEquals("file", parseFile.getName()); // Default assertEquals("content_type", parseFile.getState().mimeType()); assertTrue(parseFile.isDirty()); parseFile = new ParseFile(file); assertEquals(name, parseFile.getName()); // Default assertEquals(null, parseFile.getState().mimeType()); assertTrue(parseFile.isDirty()); parseFile = new ParseFile(file, contentType); assertEquals(name, parseFile.getName()); // Default assertEquals("content_type", parseFile.getState().mimeType());
.url(url) .build(); ParseFile parseFile = new ParseFile(state); byte[] data = ParseTaskUtils.wait(parseFile.getDataInBackground()); byte[] dataAgain = ParseTaskUtils.wait(parseFile.getDataInBackground());
if (file.getUrl() == null) { dirtyFiles.add(file);
@Override public ParseFile createFromParcel(Parcel source) { return new ParseFile(source); }
.url(url) .build(); ParseFile parseFile = new ParseFile(state); InputStream dataStream = ParseTaskUtils.wait(parseFile.getDataStreamInBackground()); InputStream dataStreamAgain = ParseTaskUtils.wait(parseFile.getDataStreamInBackground());
.url(url) .build(); ParseFile parseFile = new ParseFile(state); File fetchedFile = ParseTaskUtils.wait(parseFile.getFileInBackground()); File fetchedFileAgain = ParseTaskUtils.wait(parseFile.getFileInBackground());