I'm trying to decode an mp3 file to a wav file using the ProcessBuilder class under Linux. For some reason the process doesn't stop, so I have to cancel it manually.

Can someone give me a hint. I think the quoted code is easy to reproduce:

import java.io.*;public class Test { public static void main(String[] args) { try { Process lameProcess = new ProcessBuilder("lame","--decode","test.mp3","-").start(); InputStream is = lameProcess.getInputStream(); FileOutputStream fileOutput = new FileOutputStream("test.wav"); DataOutputStream dataOutput = new DataOutputStream(fileOutput); byte[] buf = new byte[32 * 1024]; int nRead = 0; int counter = 0; while((nRead = is.read(buf)) != -1) { dataOutput.write(buf, 0, buf.length); } is.close(); fileOutput.close(); } catch (Exception e) { e.printStackTrace(); } }}

output of jstack

"main"prio=10 tid=0x0000000002588800 nid=0x247a runnable [0x00007f17e2761000] java.lang.Thread.State: RUNNABLE at java.io.FileInputStream.readBytes(Native Method) at java.io.FileInputStream.read(FileInputStream.java:236) at java.io.BufferedInputStream.fill(BufferedInputStream.java:235) at java.io.BufferedInputStream.read1(BufferedInputStream.java:275) at java.io.BufferedInputStream.read(BufferedInputStream.java:334) - locked(a java.io.BufferedInputStream) at java.io.FilterInputStream.read(FilterInputStream.java:107) at Test.main(Test.java:17)

Solution:

You need to empty the process's output (via getInputStream() ) and error (via getErrorStream() ) streams, otherwise it may block.

Quoting the Process documentation:

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

(applies to error streams and output streams)

You may need to drain each stream in a different thread, as each stream may block when there is no data.

java-ProcessBuilder will not stop Related posts

  1. read the file name from the directory w/python

    I am trying to make a pygame executable file. I will use the following installation file:from distutils.core import setup import py2exe setup( console = ["game.py"], author ="me", data_files = [(directory, [file_1, file_2...])] )I have made it work well in a simple game, but the game I want to package has> 700 image files. My question is, is there an easy way to read all file names from a directory, so I Don’t have to list each file name separately?Although some files are numbered sequentiall ...

  2. php: output [] w / join vs $output.=

    I am modifying some code, where the original author built a web page by using an array:$output[]=$stuff_from_database; $output[]='more stuff'; // etc echo join('',$output);Can anyone think of why this would be better (and vice versa):$output =$stuff_from_database; $output .='more stuff'; // etc echo ...

  3. java - How does a JVM running multiple threads handle ctrl-c, w/ and w/o shutdown hooks?

    Couldn't find this answer online. When pressing Ctrl C:What happens to the running threads when we don't have any shutdown hooks - they are each hit with InterruptedException? When we have shutdown hooks, I know that shutdown hooks run in new threads in arbitrary order. But what happens to existing ...

  4. python-Use the string subs('x','w') instead of the symbol subs(x,w) to replace Sympy

    I am working on an application with a circuit simulator ahkab, to make a long story short I need to replace the laplace variable s in some equations with 1j*w. For me, the symbol name is used instead of the symbol itself to perform this substitution and other substitutions. Convenience. I encountered some strange behavior.If you do>>> x = Symbol('x') >>> y = Symbol('y') >>> expr = x + y x + y >>> expr.subs('x','w') w + yThis seems to work as I expected. The pr ...

  5. w descriptors and sizes: Under the hood

    Eric Portis digs into how the browser decides which image to downloads when you give it <img srcset=""sizes"">. Notably, a browser can do whatever it wants:Intentionally un-specified behavior lets browsers provide innovative answers to an open-ended question.Still, calculations happen based on ...

  6. python – pip uninstall w / – environment flag?

    I can't seem to get pip to uninstall packages when using environment flags.I created a virtual environment:virtualenv --no-site-packages /path/to/testenvWhile not in a virtual environment, I issue:pip install --environment /path/to/testenv djangoDjango is downloaded and installed.If I do the same co ...

  7. python file operation contact r+ w+ a+ understanding

    Suddenly there was a sentence:"He has the ambition to master the four directions."The file operation trilogy: 1. Open with open first, 2. Write and close 3. Then go back to the middle writing operation. It is easy to close it as soon as it is opened, so write it first. . .The basic format f = open(" ...

  8. MongoDB Full Course w/ Node.js, Express, & Mongoose

    Mongo DB (the"M"in MEAN and MERN stack) is among the most dominant databases in use today.Along with MySQL and PostgreSQL, Mongo DB an industry-standard database consideration among startups and large companies alike. But unlike the other two, Mongo DB is the de-facto standard for document based dat ...

  9. Python-invalid mode ('w') or file name

    I encountered a very strange error.Many functions in my python script contain the following code:url=['http://agecommunity.com/query/query.aspx?name=', name,'&md=user']url=``.join(url)file = open("QueryESO.xml","w")filehandle = urllib.urlopen(url)for lines in filehandle.readlines(): file.write(l ...

  10. Python regular expressions \W* and \W*? Problems encountered in the matching process

    When Lao Yuan analyzed the problem in"In-depth analysis of the matching process of Python regular expressions \W+ and \W*", he thought of a question, if"re.split('(\W*)','Hello, world') What will happen if the processing of"is changed to non-greedy mode? According to the prediction of the old monkey ...

  11. [timeisprecious][JavaScript ]JavaScript RegExp \W Metacharacter

    JavaScript>RegExp regular expression> \W metacharacter1. From RunnobJavaScript RegExp \W metacharacterDefinition and usage:The \W metacharacter is used to find non-word characters.Word characters include: az, AZ, 0-9, and underscore.grammar:new RegExp("\W") or a simpler way: /\W/Demo:Case Code ...

  12. Python open(path,'w') cannot create file

    I'm seeing strange behavior of Python open(..., 'w') on Linux. I create a bunch of files (file1...file100) in a new directory, each with:with open(nextfile, 'w') as f:It always fails if dir is empty:IOError: [Errno 2] No such file or directory: '../mydir/file1'There is no problem with permissions.If ...

  13. python-cannot get different records-Django w/ Rest Framework

    I have this viewset defined, and I want to create a custom function that returns distinct animal species_types, called distinct_species.class AnimalViewSet(viewsets.ModelViewSet):"""This viewset automatically provides `list`, `create`, `retrieve`, `update` and `destroy` actions."""queryset = Animal. ...

  14. In-depth analysis of the matching process of Python regular expressions \W+ and \W*

    In the process of learning the re.split function, it was found that the execution and return of the following statements were inconsistent with what the old ape had expected:>>> re.split('\W*','Hello, world')['','H','e','l','l','o','','w', 'o','r','l','d','']What the old ape expects is ['', ...

  15. IronPython w/ C# - How to read the value of a Python variable

    I have two python files: mainfile.py and subfile.pymainfile.py depends on some types in subfile.py.mainfile.py looks like this.from subfile import *my_variable = [1,2,3,4,5]def do_something_with_subfile #Do something with things in the subfile. #Return something.I'm trying to load mainfile.py in C# ...

  16. javascript - ECMAScript associative array via object w/ prototype null?

    I see a lot of people doing thisObject.prototype.foo = 'HALLO';var hash = {baz: 'quuz'};for ( var v in hash ) { // Do not print property `foo` if ( hash.hasOwnProperty(v) ) { console. log( v +"is a hash property"); }}My question is, instead of testing .hasOwnProperty every time you want to use Objec ...

Recent Posts

  1. java-configure gridSize in Spring Batch partition

    In Spring Batch partitioning, the relationship between the PartitionHandler's gridSize and the number of ExecutionContexts returned by the Partitioner is somewhat confusing. For example, the MultiResourcePartitioner states that it ignores the gridSize, but the Partitioner documentation doesn't say w...

  2. node.js uses kafka-node

    kafka-node site: https://www.npmjs.com/package/kafka-nodeThe following code is only for consumption informationGet the message from the specified offset, otherwise, get the message from the offset last committed by the topic. // consumer.addTopics( // [{ topic:"", offset: 1 }], // function(err, data...

  3. Count Suanke (7) Regular expressions in Python

    1. What is a regular expressionRegular expressions are a powerful logical representation for matching textual forms, and the re module in Python provides support for regular expressions.Regular expressions consist of some ordinary characters and some meta characters. Ordinary characters include uppe...

  4. python virtualenv virtual environment build

    Building a virtual environmentadvantage1. Make different application development environments independent of each other 2. The environment upgrade will not affect other applications, nor will it affect the global python environment 3. Prevent package management confusion and package version conflict...

  5. Java marked union/sum type

    Is there a way to define the sum type in Java? Java seems to naturally support product types directly, I think enums might allow it to support sum types, and inheritance looks like it might do it, but there's at least one situation I can't solve. To elaborate, the sum type is a type, It can have one...

  6. Detailed explanation of resultset class in java

    One: JDBCsun: provides a set of universal interfaces: can connect to any database: the specific instance of the connection to the database is implemented by the specific database manufacturer. Steps to connect data (don't forget to copy the jar package)...

  7. Java socket: my server input stream won't read from client output stream?

    Edit: I'm getting long, but does anyone know how to program sockets?My question confuses me a bit. I have a server running on one computer and another computer and I have a client connected to it. When I type a message from the client to the console and send it , the server doesn't seem to receive i...

  8. Where is the plugin.jar for Java 7 for OSX

    If you are using Java < 1.7 on OS X and need to use JSObject to connect the applet to JavaScript, it can be found in $JAVA_HOME/jre/lib/plugin.jar.There doesn't seem to be a"plugin.jar"archive for Java 1.7 for OS X (packaged by Oracle). There is a jfxrt.jar that seems to contain JSObject, but unf...

  9. java entry test code (spring boot 1)

    refer to:Articles about annotationshttps://blog.csdn.net/javaloveiphone/article/details/52182899https://blog.csdn.net/lipinganq/article/details/79155072https://blog.csdn.net/lipinganq/article/details/79167982...

  10. publishes a high-performance c++ network library with reference to tornado: libtnet

    Original link: https://my.oschina.net/siddontang/blog/263259libtnet is a high-performance network library written in C++. It mainly refers to tornado in design. It provides a concise and efficient interface for server-side network programming, which is very easy to use.Echo Servervoid onConnEvent(co...