// Path: src/main/java/name/raev/kaloyan/kindle/chitanka/ConnectivityManager.java
// public class ConnectivityManager {
// 	
// 	public static final String BASE_URL = "http://chitanka.info";
// 
// 	private static ConnectivityManager instance;
// 
// 	private Stack history = new Stack();
// 
// 	public static ConnectivityManager getInstance() {
// 		if (instance == null) {
// 			instance = new ConnectivityManager();
// 		}
// 		return instance;
// 	}
// 
// 	public void display(final String url) {
// 		Connectivity connectivity = ContextManager.getContext().getConnectivity();
// 		connectivity.submitSingleAttemptConnectivityRequest(new ConnectivityHandler() {
// 			public void disabled(NetworkDisabledDetails details) throws InterruptedException {
// 				handleNetworkError();
// 				display(url);
// 			}
// 
// 			public void connected() throws InterruptedException {
// 				Screen screen = ScreenManager.createScreen(url);
// 				try {
// 					screen.display();
// 				} catch (IOException e) {
// 					handleNetworkError();
// 					display(url);
// 				}
// 
// 				ScreenManager.setCurrentScreen(screen);
// 			}
// 		}, true);
// 	}
// 
// 	public void downloadBook(final String href) {
// 		ProgressIndicator.start("Книгата се изтегля");
// 		Connectivity connectivity = ContextManager.getContext().getConnectivity();
// 		connectivity.submitSingleAttemptConnectivityRequest(new ConnectivityHandler() {
// 			public void disabled(NetworkDisabledDetails details) throws InterruptedException {
// 				handleNetworkError();
// 				downloadBook(href);
// 			}
// 
// 			public void connected() throws InterruptedException {
// 				try {
// 					BookDownloader.downloadMobiFromEpubUrl(href);
// 					BookDownloader.rescanDocuments();
// 				} catch (IOException e) {
// 					handleNetworkError();
// 					downloadBook(href);
// 					return;
// 				} finally {
// 					ProgressIndicator.stop();
// 				}
// 
// 				// show info message
// 				String title = "Книгата е изтеглена";
// 				String message = "Ще намерите книгата в началото на главния екран на Kindle. Натиснете бутона Home, за да преминете към главния екран.";
// 				DialogManager.displayDialog(message, title);
// 			}
// 		}, true);
// 
// 	}
// 
// 	public boolean canGoBack() {
// 		return history.size() > 1;
// 	}
// 
// 	public void goBack() {
// 		if (canGoBack()) {
// 			// remove the current page from history
// 			history.pop();
// 			// display the previous page from history
// 			display((String) history.peek());
// 		}
// 	}
// 
// 	public void navigateTo(String link) {
// 		String url = Network.getUrlFromLinkAsString(link);
// 		// push to history
// 		history.push(url);
// 		// navigate to the selected page
// 		display(url);
// 	}
// 	
// 	public void handleNetworkError() throws InterruptedException {
// 		ProgressIndicator.stop();
// 		String title = "Неуспешно свързване";
// 		String message = "Приложението не може да се свърже с мрежата. Уверете се, че сте в обхвата на безжична мрежа.\n\nЗатворете това съобщение, за да опитате отново.";
// 		DialogManager.displayDialog(message, title);
// 	}
// 
// }


import name.raev.kaloyan.kindle.chitanka.ConnectivityManager;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import java.util.zip.GZIPInputStream;
	 */
	public static void readToFile(URL url, File file) throws IOException {
		readToFile(getInputStream(url), file);
	}

	private static void readToFile(InputStream in, File file) throws IOException {
		OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
		try {
			byte[] b = new byte[4096];
			int bytesRead;
			while ((bytesRead = in.read(b)) != -1) {
				out.write(b, 0, bytesRead);
			}
		} finally {
			in.close();
			out.flush();
			out.close();
		}
	}

	/**
	 * Returns a URL object from the given hyperlink.
	 * 
	 * @param url
	 *            a hyperlink as <code>String</code> object
	 * @return a <code>URL</code> object
	 * @throws MalformedURLException
	 *             if the URL cannot be created successfully
	 */
	public static URL getUrlFromLink(String link) throws MalformedURLException {
// Path: src/main/java/name/raev/kaloyan/kindle/chitanka/ConnectivityManager.java
// public class ConnectivityManager {
// 	
// 	public static final String BASE_URL = "http://chitanka.info";
// 
// 	private static ConnectivityManager instance;
// 
// 	private Stack history = new Stack();
// 
// 	public static ConnectivityManager getInstance() {
// 		if (instance == null) {
// 			instance = new ConnectivityManager();
// 		}
// 		return instance;
// 	}
// 
// 	public void display(final String url) {
// 		Connectivity connectivity = ContextManager.getContext().getConnectivity();
// 		connectivity.submitSingleAttemptConnectivityRequest(new ConnectivityHandler() {
// 			public void disabled(NetworkDisabledDetails details) throws InterruptedException {
// 				handleNetworkError();
// 				display(url);
// 			}
// 
// 			public void connected() throws InterruptedException {
// 				Screen screen = ScreenManager.createScreen(url);
// 				try {
// 					screen.display();
// 				} catch (IOException e) {
// 					handleNetworkError();
// 					display(url);
// 				}
// 
// 				ScreenManager.setCurrentScreen(screen);
// 			}
// 		}, true);
// 	}
// 
// 	public void downloadBook(final String href) {
// 		ProgressIndicator.start("Книгата се изтегля");
// 		Connectivity connectivity = ContextManager.getContext().getConnectivity();
// 		connectivity.submitSingleAttemptConnectivityRequest(new ConnectivityHandler() {
// 			public void disabled(NetworkDisabledDetails details) throws InterruptedException {
// 				handleNetworkError();
// 				downloadBook(href);
// 			}
// 
// 			public void connected() throws InterruptedException {
// 				try {
// 					BookDownloader.downloadMobiFromEpubUrl(href);
// 					BookDownloader.rescanDocuments();
// 				} catch (IOException e) {
// 					handleNetworkError();
// 					downloadBook(href);
// 					return;
// 				} finally {
// 					ProgressIndicator.stop();
// 				}
// 
// 				// show info message
// 				String title = "Книгата е изтеглена";
// 				String message = "Ще намерите книгата в началото на главния екран на Kindle. Натиснете бутона Home, за да преминете към главния екран.";
// 				DialogManager.displayDialog(message, title);
// 			}
// 		}, true);
// 
// 	}
// 
// 	public boolean canGoBack() {
// 		return history.size() > 1;
// 	}
// 
// 	public void goBack() {
// 		if (canGoBack()) {
// 			// remove the current page from history
// 			history.pop();
// 			// display the previous page from history
// 			display((String) history.peek());
// 		}
// 	}
// 
// 	public void navigateTo(String link) {
// 		String url = Network.getUrlFromLinkAsString(link);
// 		// push to history
// 		history.push(url);
// 		// navigate to the selected page
// 		display(url);
// 	}
// 	
// 	public void handleNetworkError() throws InterruptedException {
// 		ProgressIndicator.stop();
// 		String title = "Неуспешно свързване";
// 		String message = "Приложението не може да се свърже с мрежата. Уверете се, че сте в обхвата на безжична мрежа.\n\nЗатворете това съобщение, за да опитате отново.";
// 		DialogManager.displayDialog(message, title);
// 	}
// 
// }


// Path: src/main/java/name/raev/kaloyan/kindle/chitanka/utils/Network.java
import name.raev.kaloyan.kindle.chitanka.ConnectivityManager;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import java.util.zip.GZIPInputStream;

	 */
	public static void readToFile(URL url, File file) throws IOException {
		readToFile(getInputStream(url), file);
	}

	private static void readToFile(InputStream in, File file) throws IOException {
		OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
		try {
			byte[] b = new byte[4096];
			int bytesRead;
			while ((bytesRead = in.read(b)) != -1) {
				out.write(b, 0, bytesRead);
			}
		} finally {
			in.close();
			out.flush();
			out.close();
		}
	}

	/**
	 * Returns a URL object from the given hyperlink.
	 * 
	 * @param url
	 *            a hyperlink as <code>String</code> object
	 * @return a <code>URL</code> object
	 * @throws MalformedURLException
	 *             if the URL cannot be created successfully
	 */
	public static URL getUrlFromLink(String link) throws MalformedURLException {
		return new URL(new URL(ConnectivityManager.BASE_URL), link);