Netty Stream URL / InputStream - java

I'm trying to expand on the functionality here:
https://github.com/netty/netty/tree/3/src/main/java/org/jboss/netty/example/http/file
By providing support to stream URLs instead of Files as the content I'd like to serve is in the classpath within my JAR. Unfortunately I can't seem to figure out a good way to stream a URL or InputStream with Jetty, nor can I find any examples.
Examples or reference to JavaDoc would be appreciated to help get me on the right path.

Just use ChunkedWriteHandler and write an ChunkedStream that wraps the InputStream.
This should work out quite all..

Related

JOrbis can't read ogg file from JAR

I want to play an .ogg file, so i found this piece of code.
So it downloads .ogg file from the given url and plays it. I've tried it with external files like
ExamplePlayer player = new ExamplePlayer(new File("D:\\sound.ogg").toURI().toURL().toString())
and it works. But when i try this with internal files and get it using
ExamplePlayer.class.getClassLoader().getResource("sound.ogg")
it says that "There is a hole in the first packet data.". I think maybe it doesn't work because of JAR's compression or something.
So questions are: why it doesn't work? how could i fix it? If i can't fix it, is there any other way to play .ogg files using java? Thanks.
UPD: I found a lib, but the problem still the same, it cannot read from jar file. It
I'm looking at an application I wrote several years ago that uses ogg resources, and seeing that I first import the ogg file to an InputStream object using getResourceAsStream method.
Usually with wav files, importing via the URL is preferred. Unfortunately I don't recall why I did it this way--too much new tech under the bridge. First guess is that it's a requirement from the jcraft code, otherwise I would have used my preferred method.
Even if this works in the context of an IDE, IDK if it will also work after putting the code into a JAR. getResourceAsStream is often dicey in jars.
Before I invest more time, please let us know if switching to getResourceAsStream does the trick. Maybe the fix is that simple!
+++++++++++++
EDIT: Ugh. Looking at my code, I see where I copied a class from JCraft and edited it, supplying an InputStream via wrapper code. The edit was made in order to output float PCM data from the decoder rather than having the decoder play the sound. The float PCM is then saved in a custom object similar to AudioCue, and the app I wrote this for uses that for playback. All I can remember was that it took a lot tweaking to get this to work.

Load Array from the Internet

I wish to load data located on a website at http://www.example.com/file.extension. The file will most likely be .txt, but if I could save the data as an array, maybe .csv, and load the data as array as that is what it will be used as on the application side. JSON had popped into my head, but I wouldn't know how to use that website-side. How would you load this file from the internet?
The simplest way is probably URLConnection. There's a nice Oracle example of how you can load the response from a remote URL into a string. Then you can parse the string in whatever way seems easiest.
URLConnection belongs to the java.net package, which appears to be the same in Android as in the standard Java API, so it's pretty safe to use the Oracle documentation. However, to guarantee consistency with Android, you might also want to look at the Android documentation, which also provides a nice example.

Java, how to read and store file inside the JAR Package

I'm trying to build a simple HTTP Server using Java, using
java.net.ServerSocket = new ServerSocket(this.port, 0, this.ip);
java.net.Socket connection = null;
connection = server.accept();
java.io.OutputStream out = new BufferedOutputStream(connection.getOutputStream());
when connected using web browser, i'm simply write the output (HTTP headers + html code) from a string
String headers = "http headers";
String response = "this is the response";
out.write(headers.getBytes());
out.write(response.getBytes());
out.flush();
connection.close();
and the browser display it correctly.
And now my problem is, i want to construct a full webpage (html, javascript, css, images) and put those files into the Java package (JAR) file, and of course, those files are designed not-to-be modified after the JAR is ready to use. And here's the questions:
how to achieve this? storing the files inside the JAR and then output them when a connection is made.
how output images file (non-text) just like output-ing String by out.write() ?
Thanks, any sample or code is appreciated.
Is implementing an HTTP server your primary problem or just a way to achieve some other goal? If the latter, consider embedding Tomcat or Jetty, much simpler and with standard servlet API.
Back to your question: JAR is just a ZIP file, you can put anything there, including files, images, movies, etc. If you place a file inside a JAR file you can load it easily with:
InputStream is = getClass().getResourceAsStream("/dir/file.png");
See these questions for details how getResourceAsStream() works:
Junit + getResourceAsStream Returning Null
getResourceAsStream() vs FileInputStream
how do you make getResourceAsStream work while debugging Java in Eclipse?
Different ways of loading a file as an InputStream
getResourceAsStream() is always returning null
About your second question: when you have an InputStream instance you can just read it byte-by-byte and copy to target out OutputStream. Of course there are better, safer and faster ways, but that's beyond the scope of this question. Just have a look at IOUtils.copy():
IOUtils.copy(is, out);
And the last hint concerning your code: if you are sending Strings , consider OutputStreamWriter and PrintWriter which have easier API.
To work with JAR files use JarOutputStream or ZipOutputStream. To output binary data just do not wrap your output stream with Writer. OuputStream knows to write bytes using method write(byte) and write(byte[]).
The only question here is "Why are you developing HTTP server yourself?"
As long as it is not a housework I would not try to reinvent the wheel and develop another web server. There a small embedded Java web-servers available which can be used for that purpose.
I have for example use the Tiny Java Web Server and Servlet Container several times.
If you have integrated it into your application you can implement a new javax.servlet.http.HttpServlet that reads the files from the resources of your JAR file. The content can be loaded as Tomasz Nurkiewicz already pointed out getClass().getRourceAsStream(...).

How to get a list of files to be downloaded using HttpClient or Commons VFS?

I would want to know if there any another way to get a list of files to be downloaded from HttpServer,other than parsing the response using HttpClient.
I would also like to know if Commonns VFS is a suitable alternative for this as I tried using VFS but was not able to obtain just the list of files.
I checked an example for sftp in vfs, but was not able to implement it in Http.
Say i have this link http://www.ibiblio.org/maven/maven/jars/ is there a way in either HttpClient or Common VFS to just obtain the list of jars. The download portion is easier.
For HttpClient, short answer is no.
If you still want to do it with HttpClient, then you'll first have to extract the jar links, then retrieve each one of them separately.
As an alternative you can do that with Jsoup.
Look at that other message

How to download a file from a server using Java Socket?

I have an assignment about uploading and downloading a file to a server. I managed to do the uploading part using Java Sockets however I am having a hard time doing the downloading part. I should use Range: for downloading parellel. In my request, I should have the Range: header. But I don't understand how I will receive the file with that HTTP GET request. All the examples I have seen was about uploading a file. I already did it. I can upload .exe, image, .pdf, anything and when I download them back (by my browser), there are no errors. Can you help me with the downloading part? Can you give me an example beacuse I really didn't get it.
You need to read the HTTP response from the same socket on which you put the request. As a starting point, just print it out. When you are familiar with it, start parsing it. The file will be in there.
This doesn't directly answer your question, but it is (IMO) worth saying anyway ...
If your homework assignment doesn't specifically tell you to use a socket directly, there are simpler, and better ways of doing HTTP file upload and download in Java:
Using java.net.URL.openConnection() on an "http:" url will give you an HttpURLConnection that you can use to make GET, PUT, POST and so on requests to the remote server. This takes care of the basic HTTP protocol stuff for you.
The Apache HttpClient libraries do the same thing, but in a more sophisticated way, with more options and more hooks for things like handling content (including forms and MIME multiparts), connection and credential management, proxying and route finding and so on.
If the aim of your homework exercise is to teach you practical ways to talk to remote servers, then using these classes is far more practical than trying to implement a subset of the HTTP protocol from the socket level up.
(Of course, the aim could be to give you a deeper understanding of the HTTP protocol at the "wire" level ... which would make your current approach the right one.)
Thank you I did it. I used a byte array and read the input stream and wrote it to a file using fileinputstream and fileoutputstream

Categories

Resources