Resume last downloaded file upon Exception - java

I'm using Java SDK 1.11.534
In my tool I declared a download named 'down' using TransferManager,
since the call:
down.waitForCompletion();
is a blocking call and stops the ProgressBar acknowledgement by ProgressListener I had to introduce a SwingWorker as follows:
SwingWorker worker = new SwingWorker<Void,Integer>(){
#Override
protected void process(List<Integer> chunks) {
int j = chunks.get(chunks.size()-1);
if (i<=fileNum) jLabel4.setText("Scaricamento file " + i+ " di " + fileNum + " del DCP "+ DCPname+" in corso, attendere....");
else jLabel4.setText("Scaricamento DCP "+ DCPname+" completato con successo.");
}
#Override
protected Void doInBackground(){
for (S3ObjectSummary file: fileList){
if((!isPresent(destination,file.getKey().substring(file.getKey().lastIndexOf("/") + 1),file.getSize())) && (!(file.getKey().substring(0, file.getKey().length()-1).equals(DCPname)))){
publish(i);
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, file.getKey());
down = tx.download(getObjectRequest,new File(percorso+File.separator + file.getKey().substring(file.getKey().lastIndexOf("/") + 1)));
down.addProgressListener(progressListener);
try {
down.waitForCompletion();
} catch (AmazonClientException ex) {
ex.printStackTrace();
tx.shutdownNow(true);
//jButton4.setEnabled(true);
jButton4.doClick();
} catch (InterruptedException ex) {
ex.printStackTrace();
tx.shutdownNow(true);
//jButton4.setEnabled(true);
jButton4.doClick();
}
i++;
}
This is a portion of the code where doInBackground() shows the operations to do.
It happens sometimes to have an AmazonClientException reporting:
Not all bytes from S3inputstream were read
And this leads to have a corrupted file and a stop of the program itself upon exception.
At the beginning of my code (not reported here) before reaching the SwingWorker declaration, I stated that when the jButton4 is clicked the action starts checking if there's a size mismatch between files in the download folder and the ones on Amazon s3 and if there's a truncated file it gets deleted and the name is added to the download list again.
So the only solution I've found so far is to add the following line code:
jButton4.doClick();
In the exception code, meaning when an exception is hit the progress restarts and checks for truncated files and restarts downloads adding such a file too.
My question is:
Is there any way in the SDK to resume or better cancel and then download file again upon exception without restarting the program? I find the usage of:
jButton4.doClick();
is not a professional way of coding.

You could extract the content of the click action method into a new method and call that method instead.

Related

GStreamer open plain m3u from http

I'm trying to open plain m3u file without any meta data from http server:
1.ts
2.ts
3.ts
Simple way with playbin fails, telling it's a text file (yes, it is! tried both m3u and m3u8 extensions)
gst-launch-1.0.exe playbin uri=http://10.42.0.3:8765/list.m3u8
ERROR: from element /GstPlayBin:playbin0/GstURIDecodeBin:uridecodebin0/GstDecodeBin:decodebin0: This appears to be a text file
decodebin cannot decode plain text files
decodebin in solo refuses to get file from http:
gst-launch-1.0 filesrc location=http://10.42.0.3:8765/list.m3u8 ! decodebin
ERROR: from element /GstPipeline:pipeline0/GstFileSrc:filesrc0: Could not open file "http://10.42.0.3:8765/list.m3u8" for reading.
and forced uridecodebin (that is supposed to accept playlists according to GStreamer documentation) fails like playbin:
gst-launch-1.0.exe uridecodebin uri=http://10.42.0.3:8765/list.m3u
ERROR: from element /GstPipeline:pipeline0/GstURIDecodeBin:uridecodebin0/GstDecodeBin:decodebin0: This appears to be a text file
decodebin cannot decode plain text files
If I add HLS-style meta data to same file:
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:2.662589,
1.ts
#EXTINF:2.829011,
2.ts
#EXTINF:2.995411,
3.ts
it is correctly displayed with:
gst-launch-1.0.exe playbin uri=http://10.42.0.3:8765/list.m3u8
without any problems.
Is there any way to show video from m3u (or any other simle-list fileformat) without meta data?
And is there any way to jump to specified track during playback (I use Java bindings but they seem to have same naming as original lib)?
p.s. Same pure m3u is correctly played with vlc, ffplay and mpv
p.p.s. I understand that I can parse m3u and send links one-by-one on finished event, but I hope to gain some gapless playback without extra pain :-)
ADDED:
I wrote quick-and-durty parser with switcher:
playbin.getBus().connect(new Bus.EOS() {
#Override
public void endOfStream(GstObject source) {
EventQueue.invokeLater(() -> {
if (filesIndex < files.length) {
try {
playbin.stop();
playbin.setURI(new URI(request + files[filesIndex]));
playbin.play();
System.out.println("SWITCHED TO: " + request + files[filesIndex]);
} catch (Exception e){
e.printStackTrace();
}
}
filesIndex++;
});
}
});
playbin.getBus().connect(new Bus.ERROR() {
#Override
public void errorMessage(GstObject gstObject, int i, String s) {
EventQueue.invokeLater(() -> {
if (filesIndex < files.length) {
try {
playbin.stop();
// filesIndex++; Retry?
playbin.setURI(new URI(request + files[filesIndex]));
playbin.play();
System.out.println("SWITCHED ON ERROR TO: " + request + files[filesIndex]); // Error counter?
} catch (Exception e){
e.printStackTrace();
}
}
filesIndex++;
});
}
});
where files[] is m3u splited by lines, and this works very well (including jumping), but I still wonder what is a "built-in" way of playing lists and switching indexes

Force reload changes on a component JSwing

I made a code to delete the adobe directories from an user profile, I use it remotely conecting to remote computers. In this code when a file it's deleted an textArea must show the rute of the deleted file. In a System.out.println the rute it runs but it doesn't change the textArea until the recursive function ends.
I have this code. (Sorry for the rudimentary translate to English)
private void RecursiveDel(String rute) {
File tdel = new File(rute);
if (tdel.isDirectory()) {
for (File del : tdel.listFiles()) {
RecursiveDel(del.getAbsolutePath());
}
}
txtInform += "Removing: " + ruta + "\r\n";
ActRes();
tdel.delete();
System.out.println(rute);
if (tdel.exists()) {
txtInforme += "File isn't deleted: \r\n" + ruta + "\r\n";
ActRes();
Correct = false;
}
}
private void ActRes(){
Thread act = new Thread(new Runnable() {
#Override
public void run() {
txtResult.setText(txtInforme);
}
});
act.start();
}
How I can do show the deleted Files into the TextArea meanwile the recursive function works?
it runs but it doesn't change the textArea until the recursive function ends.
Correct, because your code is looping through all the directories and building a string rather than trying to update the text area for each directory.
Instead you should be using a SwingWorker and "publishing" the directory as you find it. Then every time you publish a value the text area can be updated.
Read the section from the Swing tutorial on Tasks That Have Interim Results for an example of this approach.

How to write file on OpenStack container using JOSS

I need help because i need to integrate JOSS in a existing code. My code uses the Consumer feature of Java 8.
Consumer<? super GHRepository> action = repo -> {
try {
if(github.getRateLimit().remaining > 0) {
Files.write(this.path, (repo.toString() + "\n").getBytes(), StandardOpenOption.APPEND);
totalIteration++;
} else {
logger.info("Time to pause for " + (github.getRateLimit().reset.getTime() - new Date().getTime()));
//wait until rate limit is ok.
do {
Thread.sleep(60000);
} while(github.getRateLimit().reset.after(new Date()));
}
} catch (Exception e) {
logger.error("Erreur d'écriture dans le fichier : " + e.getMessage());
}
};
This code works fine but disk space available on the machine is not enough. So i need to write the file directly on an OpenStack container.
I've read in the doc that JOSS uses this function to upload a file.
StoredObject object = container.getObject("dog.png");
object.uploadObject(new File("/dog.png"));
This is the method to upload a file already written. But I need to write the file directly on the container. The uploadObject function can receive a InputStream in parameter. So i want to use it. But i don't know how to integrate it with my existing code. Can you help me?
Ok, i find the way .
object.uploadObject(Files.newInputStream(Files.write(this.path, (repo.toString() + "\n").getBytes(), StandardOpenOption.APPEND)));

DataInputStream not reading small files

I am programming a blackberry app that reads small files stored on the device.
For some reason DataInputStream will only read files that are bigger then 3kb (my estimate after a couple of test). Anyone know why this may be the case?
try
{
fconn = (FileConnection)Connector.open(seedDataLocation);
if (fconn.exists())
{
UiApplication.getUiApplication().getActiveScreen().add(new RichTextField("Read?: " + fconn.canRead()));
UiApplication.getUiApplication().getActiveScreen().add(new RichTextField("Write?: " + fconn.canWrite()));
UiApplication.getUiApplication().getActiveScreen().add(new RichTextField("Open?: " + fconn.isOpen()));
int dataSize = (int) fconn.fileSize();
DataInputStream dis = fconn.openDataInputStream();
UiApplication.getUiApplication().getActiveScreen().add(new RichTextField("Data Available: " + dataSize));
byte[] b = new byte[dataSize];
dis.readFully(b);
String data = new String(b);
UiApplication.getUiApplication().getActiveScreen().add(new RichTextField("Data: " + data));
fconn.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
UiApplication.getUiApplication().getActiveScreen().add(new RichTextField(e.getMessage()));
}
edit: I tried doing dis.available(); and it is returning 0 for small files in which it does not read from the file and also 0 from bigger files it will read from. Which is confusing because it is supposed to read the amount of bytes in the file.
That getActiveScreen().add call is error prone, and it will fail if you try to call it from outside the GUI thread. The fact that it is working for you means that you are actually making these calls from the GUI thread, which is horrible practice since you are also opening a file from there.
Move the connection code to another worker thread and access the GUI as needed using UiApplication.invokeLater or UiApplication.invokeAndWait to wrap your GUI-related code inside the parameter Runnables. Or either use synchronized(application.getEventLock().
That said, the fact that you are not reading data in small files only could be caused by this line:
dis.readFully(b);
Maybe you need to flush the stream. This call will block until b.length bytes are available. Check before that file is readable with fconn.isReadable.
UPDATE:
You can try also IoUtilities.streamToBytes(dis), but do not use it with big files or you can get an out-of-memory error.

Running a Java program with a .dll from Adobe AIR's native process

I would like to be able to operate a scanner from my AIR application. Since there's no support for this natively, I'm trying to use the NativeProcess class to start a jar file that can run the scanner. The Java code is using the JTwain library to operate the scanner. The Java application runs fine by itself, and the AIR application can start and communicate with the Java application. The problem seems to be that any time I attempt to use a function from JTwain (which relies on the JTwain.dll), the application dies IF AIR STARTED IT.
I'm not sure if there's some limit about referencing dll files from the native process or what. I've included my code below
Java code-
while(true)
{
try {
System.out.println("Start");
text = in.readLine();
Source source = SourceManager.instance().getCurrentSource();
System.out.println("Java says: "+ text);
}
catch (IOException e)
{
System.err.println("Exception while reading the input. " + e);
}
catch (Exception e) {
System.out.println("Other exception occured: " + e.toString());
}
finally {
}
}
}
Air application-
import mx.events.FlexEvent;
private var nativeProcess:NativeProcess;
private var npInfo:NativeProcessStartupInfo;
private var processBuffer:ByteArray;
private var bLength:int = 0;
protected function windowedapplication1_applicationCompleteHandler(event:FlexEvent):void
{
var arg:Vector.<String> = new Vector.<String>;
arg.push("-jar");
arg.push(File.applicationDirectory.resolvePath("Hello2.jar").nativePath);
processBuffer = new ByteArray;
npInfo = new NativeProcessStartupInfo;
npInfo.executable = new File("C:/Program Files/Java/jre6/bin/javaw.exe");
npInfo.arguments = arg;
nativeProcess = new NativeProcess;
nativeProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onStandardOutputData);
nativeProcess.start(npInfo);
}
private function onStandardOutputData(e:ProgressEvent):void
{
tArea.text += nativeProcess.standardOutput.readUTFBytes(nativeProcess.standardOutput.bytesAvailable);
}
protected function button1_clickHandler(event:MouseEvent):void
{
tArea.text += 'AIR app: '+tInput.text + '\n';
nativeProcess.standardInput.writeMultiByte(tInput.text + "\n", 'utf-8');
tInput.text = '';
}
protected function windowedapplication1_closeHandler(event:Event):void
{
nativeProcess.closeInput();
}
]]>
</fx:Script>
<s:Button label="Send" x="221" y="11" click="button1_clickHandler(event)"/>
<s:TextInput id="tInput" x="10" y="10" width="203"/>
<s:TextArea id="tArea" x="10" width="282" height="88" top="40"/>
I would love some explanation about why this is dying. I've done enough testing that I know absolutely that the line that kills it is the SourceManager.instance().getCurrentSource(). I would love any suggestions. Thanks.
When calling Java add this -Djava.library.path=location_of_dll to the command line
I have 0 experience with Air, but this reminded me of a Java issue I once spent some time figuring out. I don't have a suggestion on why the scanning doesn't work, but I think a stack trace would be your best friend right now.
I'm guessing you're relying on this line to capture and display it?
nativeProcess.standardOutput.readUTFBytes(nativeProcess.standardOutput.bytesAvailable);
However, you are writing IOExceptions to System.err - is there a nativeProcess.standardError you could read in Air? Alternatively, output everything to System.out.

Categories

Resources