outputstream

Why Java OutputStream.write() Takes Integer but Writes Bytes

妖精的绣舞 提交于 2019-11-27 20:26:23
I am writing an OutputStream, just noticed this in the OutputStream interface, public abstract void write(int b) throws IOException; This call write one byte to the stream but why it takes integer as an argument? Actually I've been working with bytes a bit lately and they can be annoying. They up-convert to ints at the slightest provocation and there is no designation to turn a number into a byte--for instance, 8l will give you a long value 8, but for byte you have to say (byte)8 On top of that, they will (pretty much) always be stored internally as ints unless you are using an array (and

Java ProcessBuilder: Input/Output Stream

孤街醉人 提交于 2019-11-27 14:55:18
I want to invoke an external program in java code, then the Google tell me that the Runtime or ProcessBuilder can help me to do this work. I have tried it, and there come out a problem the java program can't exit, that means both the sub process and the father process wait for forever. they are hanging or deadlock. Someone tell me the reason is that the sub process's cache is too small. when it try to give back data to the father process, but the father process don't read it in time, then both of them hang. So they advice me fork an thread to be in charge of read sub process's cache data. I do

Write an InputStream to an HttpServletResponse

牧云@^-^@ 提交于 2019-11-27 14:21:25
I have an InputStream that I want written to a HttpServletResponse. There's this approach, which takes too long due to the use of byte[] InputStream is = getInputStream(); int contentLength = getContentLength(); byte[] data = new byte[contentLength]; is.read(data); //response here is the HttpServletResponse object response.setContentLength(contentLength); response.write(data); I was wondering what could possibly be the best way to do it, in terms of speed and efficiency. Just write in blocks instead of copying it entirely into Java's memory first. The below basic example writes it in blocks of

How to read pdf file and write it to outputStream [closed]

我怕爱的太早我们不能终老 提交于 2019-11-27 14:04:41
I need to read a pdf file with filepath "C:\file.pdf" and write it to outputStream. What is the easiest way to do that? @Controller public class ExportTlocrt { @Autowired private PhoneBookService phoneBookSer; private void setResponseHeaderTlocrtPDF(HttpServletResponse response) { response.setContentType("application/pdf"); response.setHeader("content-disposition", "attachment; filename=Tlocrt.pdf" ); } @RequestMapping(value = "/exportTlocrt.html", method = RequestMethod.POST) public void exportTlocrt(Model model, HttpServletResponse response, HttpServletRequest request){

Do I need to flush the servlet outputstream?

喜你入骨 提交于 2019-11-27 12:02:26
问题 Do I need to "flush" the OutputStream from the HttpServletResponse? I already saw from to Should I close the servlet outputstream? that I don't need to close it, but it's not clear if I need to flush it. Should I expect it from the container as well? protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { byte[] response = getResponse(); String responseType = getResponseType(); response.setContentLength(response.length); response

Java Networking: Explain InputStream and OutputStream in Socket

混江龙づ霸主 提交于 2019-11-27 09:53:42
问题 I have created a server by using ServerSocket . After that, I have created Client using Socket , and connect to this server. After that, I do "some stuff" with InputStream and OutputStream is taken from Socket Object. But, I don't really understand inputStream and outputStream so much. Here is my simple code : private Socket sock = null; private InputStream sockInput = null; private OutputStream sockOutput = null; ... String msg = "Hello World"; byte[] buffer = null; try { sockOutput.write

Android : Saving Files to SD Card

狂风中的少年 提交于 2019-11-27 08:09:38
问题 I am trying to save a file out from within my app to the external storage of a device. Specifically, I am trying to save a sound out. I've followed every 'tutorial' on doing this, and none of it seemed particularly confusing to me, yet it just will not work for me. No file is ever created on the SD card at all, much less actually transferring over the information. I've assumed I am somehow not getting the correct path to the card, but no matter what I have tried it doesn't work. I have tried

Writing to ZipArchive using the HttpContext OutputStream

霸气de小男生 提交于 2019-11-27 07:57:55
I've been trying to get the "new" ZipArchive included in .NET 4.5 ( System.IO.Compression.ZipArchive ) to work in a ASP.NET site. But it seems like it doesn't like writing to the stream of HttpContext.Response.OutputStream . My following code example will throw System.NotSupportedException: Specified method is not supported as soon as a write is attempted on the stream. The CanWrite property on the stream returns true. If I exchange the OutputStream with a filestream, pointing to a local directory, it works. What gives? ZipArchive archive = new ZipArchive(HttpContext.Response.OutputStream,

Spring: getOutputStream() has already been called for this response

断了今生、忘了曾经 提交于 2019-11-27 07:28:08
问题 I know that there are many other posts dealing with the very same error, but all of them are either about JSP / GSP pages or for any other reason not very helpful in my case. I'm using Spring MVC with Thymeleaf. The following function is for downloading a file. @RequestMapping(value = "/test/download/*", method = RequestMethod.GET) public String getFile(HttpServletResponse response) { ServletOutputStream stream = null; try { stream = response.getOutputStream(); MultipartFile f = test.getFile(

JAVA : Handling socket disconnection

浪子不回头ぞ 提交于 2019-11-27 06:33:06
问题 Two computers are connected by socket connection. If the server/client closes the connection from their end(i.e closes the InputStream , OutputStream and Socket ) then how can I inform the other end about the disconnection? There is one way I know of - trying to read from the InputStream , which throws an IOException if connection is closed, but is there any other way to detect this? Another question, I looked the problem up on the internet and saw inputStream.available() does not solve this