1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-03 09:49:19 +02:00
This commit is contained in:
lidoxod 2013-07-29 19:07:19 +04:00
parent c35bb90dd4
commit 17c6b24b53
4 changed files with 54 additions and 49 deletions

View file

@ -19,14 +19,12 @@
package org.geometerplus.fbreader.book; package org.geometerplus.fbreader.book;
import java.io.*; import java.io.File;
import java.util.*; import java.util.*;
import org.geometerplus.zlibrary.core.filesystem.ZLFile; import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.filesystem.ZLPhysicalFile; import org.geometerplus.zlibrary.core.filesystem.ZLPhysicalFile;
import org.geometerplus.zlibrary.core.image.ZLImage; import org.geometerplus.zlibrary.core.image.ZLImage;
import org.geometerplus.zlibrary.core.image.ZLLoadableImage;
import org.geometerplus.zlibrary.core.image.ZLSingleImage;
import org.geometerplus.zlibrary.text.view.ZLTextPosition; import org.geometerplus.zlibrary.text.view.ZLTextPosition;
import org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageData; import org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageData;
@ -618,7 +616,6 @@ public class BookCollection extends AbstractBookCollection {
@Override @Override
public boolean saveCover(Book book, String url) { public boolean saveCover(Book book, String url) {
if (getBookById(book.getId()) == null) { if (getBookById(book.getId()) == null) {
return false; return false;
} }
@ -628,54 +625,13 @@ public class BookCollection extends AbstractBookCollection {
return false; return false;
} }
if (image instanceof ZLLoadableImage) {
final ZLLoadableImage loadableImage = (ZLLoadableImage)image;
if (!loadableImage.isSynchronized()) {
loadableImage.synchronize();
}
}
final ZLAndroidImageData data = final ZLAndroidImageData data =
((ZLAndroidImageManager)ZLAndroidImageManager.Instance()).getImageData(image); ((ZLAndroidImageManager)ZLAndroidImageManager.Instance()).getImageData(image);
if (data == null) { if (data == null) {
return false; return false;
} }
final InputStream inputStream = ((ZLSingleImage)image).inputStream(); return image.saveToFile(url);
if (inputStream == null) {
return false;
}
OutputStream outputStream = null;
final File file = new File(url);
final File parent = file.getParentFile();
parent.mkdirs();
try {
outputStream = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
} }
public List<Bookmark> bookmarks(BookmarkQuery query) { public List<Bookmark> bookmarks(BookmarkQuery query) {

View file

@ -21,4 +21,5 @@ package org.geometerplus.zlibrary.core.image;
public interface ZLImage { public interface ZLImage {
String getURI(); String getURI();
boolean saveToFile(String url);
} }

View file

@ -49,4 +49,12 @@ public abstract class ZLLoadableImage extends ZLSingleImage {
public abstract void synchronize(); public abstract void synchronize();
public abstract void synchronizeFast(); public abstract void synchronizeFast();
public abstract String getId(); public abstract String getId();
@Override
public boolean saveToFile(String url) {
if (!isSynchronized()) {
synchronize();
}
return super.saveToFile(url);
}
} }

View file

@ -19,7 +19,7 @@
package org.geometerplus.zlibrary.core.image; package org.geometerplus.zlibrary.core.image;
import java.io.InputStream; import java.io.*;
import org.geometerplus.zlibrary.core.util.MimeType; import org.geometerplus.zlibrary.core.util.MimeType;
@ -35,4 +35,44 @@ public abstract class ZLSingleImage implements ZLImage {
public final MimeType mimeType() { public final MimeType mimeType() {
return myMimeType; return myMimeType;
} }
@Override
public boolean saveToFile(String url) {
final InputStream inputStream = inputStream();
if (inputStream == null) {
return false;
}
OutputStream outputStream = null;
final File file = new File(url);
final File parent = file.getParentFile();
parent.mkdirs();
try {
outputStream = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
} }