/** * @Declaration : - (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest * *)request @Description : Creates a download task for the specified URL request and saves the * results to a file. * * @param request An NSURLRequest object that provides the URL, cache policy, request type, body * data or body stream, and so on. * @return Return Value The new session download task. @Discussion After you create the task, you * must start it by calling its resume method. */ public DownloadTask downloadTaskWithRequest(NSURLRequest request) { NSURL nsURL = request.URL(); URL url = nsURL.getUrl(); downloadTask = new DownloadTask(url); downloadTasks.add(downloadTask); return downloadTask; }
/** * @Declaration : - (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request * completionHandler:(void (^)(NSURL *location, NSURLResponse *response, NSError * *error))completionHandler @Description : Creates a download task for the specified URL request, * saves the results to a file, and calls a handler upon completion. * * @param request An NSURLRequest object that provides the URL, cache policy, request type, body * data or body stream, and so on. * @param completionHandler The completion handler to call when the load request is complete. This * handler is executed on the delegate queue. Unless you have provided a custom delegate, this * parameter must not be nil, because there is no other way to retrieve the response data. * @return Return Value The new session download task. @Discussion After you create the task, you * must start it by calling its resume method. */ public DownloadTask downloadTaskWithRequestCompletionHandler( NSURLRequest request, PerformBlock.VoidBlockNSURLNSURLResponseNSError completionHandler) { NSURL nsURL = request.URL(); URL url = nsURL.getUrl(); downloadTask = new DownloadTask(url); downloadTasks.add(downloadTask); return downloadTask; }
public void uploadFile() throws IOException { String fileName = url.path().toString(); DataOutputStream dos = null; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1 * 1024 * 1024; FileInputStream fileInputStream = new FileInputStream(fileName); // Open a HTTP connection to the URL request = (HttpURLConnection) url.getUrl().openConnection(); request.setDoInput(true); // Allow Inputs request.setDoOutput(true); // Allow Outputs request.setUseCaches(false); // Don't use a Cached Copy request.setChunkedStreamingMode(1024); request.setRequestMethod("POST"); request.setRequestProperty("requestection", "Keep-Alive"); request.setRequestProperty("ENCTYPE", "multipart/form-data"); request.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); request.setRequestProperty("uploaded_file", fileName); dos = new DataOutputStream(request.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes( "Content-Disposition: form-data; name=\"file\";filename=\"" + fileName + "\"" + lineEnd); dos.writeBytes(lineEnd); // create a buffer of maximum size bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // read file and write it into form... bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } // send multipart form data necesssary after file data... dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // close the streams // fileInputStream.close(); dos.flush(); dos.close(); }
static NSURL getURL(String fileName) { NSString unescapedStr; String lowercaseName = fileName.toLowerCase(); if (lowercaseName.startsWith(PREFIX_HTTP) || lowercaseName.startsWith(PREFIX_HTTPS) || lowercaseName.startsWith(PREFIX_FILE)) { unescapedStr = NSString.stringWith("%#"); // $NON-NLS-1$ } else { unescapedStr = NSString.stringWith("%"); // $NON-NLS-1$ } NSString fullPath = NSString.stringWith(fileName); if (NSFileManager.defaultManager().fileExistsAtPath(fullPath)) { fullPath = NSURL.fileURLWithPath(fullPath).absoluteString(); } long /*int*/ ptr = OS.CFURLCreateStringByAddingPercentEscapes( 0, fullPath.id, unescapedStr.id, 0, OS.kCFStringEncodingUTF8); NSString escapedString = new NSString(ptr); NSURL url = NSURL.URLWithString(escapedString); OS.CFRelease(ptr); return url; }
private void stop() { if (Main.getAudioPlayer() != null) { Main.getAudioPlayer().stop(); } if (Main.library.getMusicInfos(songInfo).size() == 0) { playButton.setEnabled(false); repeatButton.setEnabled(false); } else if (Main.library.getMusicInfos(songInfo).size() > 1) { Main.setAudioPlayer(null); } else { Main.setAudioPlayer( AVAudioPlayer.audioPlayerWithContentsOfURL( NSURL.fileURLWithPath( Main.library.getMusicInfos(songInfo).get(0).getMusicPath().getPath()), null)); Main.getAudioPlayer().prepareToPlay(); Main.getAudioPlayer().setNumberOfLoops(repeat ? -1 : 0); } setToolbarItems(new ArrayList<UIBarButtonItem>(buttonsPlay)); }
public void show(final SongInfo songInfo) { this.songInfo = songInfo; setTitle(songInfo.getName()); final NSURL pdfURL = NSURL.fileURLWithPath(songInfo.getPdfPath().getPath()); NSURLRequest request = requests.get(songInfo); if (request == null) { request = NSURLRequest.requestWithURL(pdfURL); requests.put(songInfo, request); } pdfView.loadRequest(request); if (UIPrintInteractionController.isPrintingAvailable()) { rightBarButtonItem = new UIBarButtonItem( UIBarButtonSystemItem.Action, new UIBarButtonItemDelegate() { public void clicked() { UIPrintInteractionController print = UIPrintInteractionController.sharedPrintController(); print.setPrintingItem(pdfURL); print.presentFromBarButtonItem( rightBarButtonItem, true, new UIPrintInteractionController.UIPrintInteractionCompletionHandler() { public void completed( UIPrintInteractionController controller, boolean b, NSError nsError) { System.out.println("finished: " + b + ", error: " + nsError); } }); } }); setHidesBottomBarWhenPushed(false); getNavigationItem().setRightBarButtonItem(rightBarButtonItem); } playButton = new UIBarButtonItem( UIBarButtonSystemItem.Play, new UIBarButtonItemDelegate() { public void clicked() { final List<MusicInfo> musicInfos = Main.library.getMusicInfos(songInfo); if (Main.getAudioPlayer() == null && musicInfos.size() > 1) { String[] titles = new String[musicInfos.size()]; for (int i = 0; i < titles.length; i++) { titles[i] = musicInfos.get(i).getName(); } // player is stopped and we have multiple music files UIActionSheet uiActionSheet = UIActionSheet.init( "Play", new UIActionSheetDelegate() { @Override public void clickedButtonAtIndex( UIActionSheet actionSheet, int buttonIndex) { Main.setAudioPlayer( AVAudioPlayer.audioPlayerWithContentsOfURL( NSURL.fileURLWithPath( musicInfos.get(buttonIndex).getMusicPath().getPath()), null)); Main.getAudioPlayer().prepareToPlay(); play(); } }, null, null, titles); uiActionSheet.showFromBarButtonItem(playButton, true); } else { play(); } } private void play() { updateRepeatMode(); Main.getAudioPlayer().play(); Main.getAudioPlayer() .setDelegate( new AVAudioPlayerDelegate() { public void audioPlayerDidFinishPlaying( AVAudioPlayer player, boolean successfully) { stop(); } public void audioPlayerDecodeErrorDidOccur( AVAudioPlayer player, NSError error) {} public void audioPlayerBeginInterruption(AVAudioPlayer player) {} public void audioPlayerEndInterruption(AVAudioPlayer player) {} }); setToolbarItems(new ArrayList<UIBarButtonItem>(buttonsPauseStop)); } }); pauseButton = new UIBarButtonItem( UIBarButtonSystemItem.Pause, new UIBarButtonItemDelegate() { public void clicked() { Main.getAudioPlayer().pause(); setToolbarItems(new ArrayList<UIBarButtonItem>(buttonsPlayStop)); } }); stopButton = new UIBarButtonItem( UIBarButtonSystemItem.Stop, new UIBarButtonItemDelegate() { public void clicked() { stop(); } }); repeat = false; repeatButton = new UIBarButtonItem( UIImage.imageNamed("repeat.png"), UIBarButtonItemStyle.Plain, new UIBarButtonItemDelegate() { public void clicked() { repeat = !repeat; if (repeat) { repeatButton.setImage(UIImage.imageNamed("no-repeat.png")); } else { repeatButton.setImage(UIImage.imageNamed("repeat.png")); } updateRepeatMode(); } }); buttonsPlay = Arrays.asList(playButton, repeatButton); buttonsPauseStop = Arrays.asList(pauseButton, stopButton, repeatButton); buttonsPlayStop = Arrays.asList(playButton, stopButton, repeatButton); setToolbarItems(new ArrayList<UIBarButtonItem>(buttonsPlay)); stop(); }