List of usage examples for java.util.zip GZIPOutputStream GZIPOutputStream
public GZIPOutputStream(OutputStream out) throws IOException
From source file:bookkeepr.jettyhandlers.CandidateHandler.java
public void handle(String path, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException { if (path.startsWith("/cand/")) { ((Request) request).setHandled(true); if (path.startsWith("/cand/lists")) { if (request.getMethod().equals("GET")) { if (path.startsWith("/cand/lists/from/")) { long targetId = Long.parseLong(path.substring(17), 16); if (dbMan.getType(new DummyIdAble(targetId)) == TypeIdManager .getTypeFromClass(Psrxml.class)) { CandListSelectionRequest req = new CandListSelectionRequest(); req.setAcceptPsrxmlIds(new long[] { targetId }); XMLWriter.write(response.getOutputStream(), candMan.getCandidateLists(req)); } else if (dbMan.getType(new DummyIdAble(targetId)) == TypeIdManager .getTypeFromClass(Processing.class)) { CandListSelectionRequest req = new CandListSelectionRequest(); req.setAcceptProcessingIds(new long[] { targetId }); XMLWriter.write(response.getOutputStream(), candMan.getCandidateLists(req)); } else { response.sendError(400, "Bad GET request for /cand/lists/from/..."); return; }//from w ww .java 2s . c o m } else { CandidateListIndex idx = new CandidateListIndex(); List<IdAble> list = dbMan .getAllOfType(TypeIdManager.getTypeFromClass(CandidateListStub.class)); for (IdAble stub : list) { idx.addCandidateListStub((CandidateListStub) stub); } OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } XMLWriter.write(out, idx); out.close(); return; } } else if (request.getMethod().equals("POST")) { try { XMLAble xmlable = XMLReader.read(request.getInputStream()); if (xmlable instanceof CandListSelectionRequest) { CandListSelectionRequest req = (CandListSelectionRequest) xmlable; OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } XMLWriter.write(out, candMan.getCandidateLists(req)); out.close(); } else { response.sendError(400, "Bad POST request for /cand/lists"); return; } } catch (SAXException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.SEVERE, null, ex); } } else if (request.getMethod().equals("DELETE")) { String[] elems = path.substring(1).split("/"); if (elems.length < 3) { response.sendError(400, "Bad DELETE request for /cand/lists/{clistId}"); return; } final long clistId = Long.parseLong(elems[2], 16); CandidateListStub cls = (CandidateListStub) dbMan.getById(clistId); if (cls == null) { response.sendError(404, "Bad ClistID requested for deletion (no such candlist)"); Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Bad ClistID requested for deletion (no such candlist)"); return; } try { this.candMan.deleteCandidateListAndCands(cls); response.setStatus(202); } catch (BookKeeprException ex) { response.sendError(500, "Could not delete candidate list as requested"); Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Could not delete candidate list as requested", ex); return; } } else { response.sendError(400, "Bad non-GET/non-POST/non-DELETE request for /cand/lists"); return; } } else if (path.startsWith("/cand/viewed")) { if (request.getMethod().equals("GET")) { ViewedCandidatesIndex idx = new ViewedCandidatesIndex(); List<IdAble> list = dbMan.getAllOfType(TypeIdManager.getTypeFromClass(ViewedCandidates.class)); for (IdAble stub : list) { idx.addViewedCandidates((ViewedCandidates) stub); } OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } XMLWriter.write(out, idx); out.close(); return; } else if (request.getMethod().equals("POST")) { ViewedCandidates newViewed = null; try { XMLAble xmlable = XMLReader.read(request.getInputStream()); if (xmlable instanceof ViewedCandidates) { newViewed = (ViewedCandidates) xmlable; } else { response.sendError(400, "Bad Content type in request /cand/viewed"); return; } } catch (SAXException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.INFO, "Bad XML in client request to POST new viewed cands"); response.sendError(400, "Bad XML in request /cand/viewed"); return; } IdAble idable = this.dbMan.getById(newViewed.getId()); if (idable instanceof ViewedCandidates) { newViewed.append(((ViewedCandidates) idable)); } else { newViewed.setId(0); } Session session = new Session(); this.dbMan.add(newViewed, session); try { this.dbMan.save(session); } catch (BookKeeprException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.SEVERE, "Error saving viewed cands to database", ex); response.sendError(500, "Error saving viewed cands to database"); return; } OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } XMLWriter.write(out, newViewed); out.close(); return; } else { response.sendError(400, "Bad non-GET/POST request /cand/viewed"); return; } } else if (path.startsWith("/cand/psrcat")) { if (request.getMethod().equals("GET")) { String[] elems = path.substring(1).split("/"); if (elems.length > 2) { try { long id = Long.parseLong(elems[2], 16); ClassifiedCandidate cand = (ClassifiedCandidate) dbMan.getById(id); String str = cand.getPsrcatEntry().toString(); response.setContentType("text/plain"); OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } PrintWriter wr = new PrintWriter(out); wr.write(str); wr.close(); out.close(); } catch (ClassCastException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Invalid candidate id passed to /cand/psrcat", ex); response.sendError(400, "Bad id in request for /cand/psrcat"); } } else { response.sendError(400, "Bad request for /cand/psrcat"); } } else { response.sendError(400, "Bad non-GET request for /cand/psrcat"); } } else if (path.startsWith("/cand/classified")) { if (request.getMethod().equals("GET")) { String[] elems = path.substring(1).split("/"); ClassifiedCandidateIndex idx = new ClassifiedCandidateIndex(); List<IdAble> list = dbMan .getAllOfType(TypeIdManager.getTypeFromClass(ClassifiedCandidate.class)); if (elems.length > 2) { int cl = Integer.parseInt(elems[2]); for (IdAble stub : list) { if (((ClassifiedCandidate) stub).getCandClassInt() == cl) { idx.addClassifiedCandidate((ClassifiedCandidate) stub); } } } else { for (IdAble stub : list) { idx.addClassifiedCandidate((ClassifiedCandidate) stub); } } OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } XMLWriter.write(out, idx); out.close(); return; } else if (request.getMethod().equals("POST")) { // post a CandidateSelectRequest ClassifiedCandidateSelectRequest ccsreq = null; try { XMLAble xmlable = XMLReader.read(request.getInputStream()); if (xmlable instanceof ClassifiedCandidateSelectRequest) { ccsreq = (ClassifiedCandidateSelectRequest) xmlable; } else { response.sendError(400, "Bad item posted to /cand/classified for Searching"); return; } } catch (SAXException ex) { response.sendError(400, "Bad item posted to /cand/classified for Searching"); return; } // reply with the searched index ClassifiedCandidateIndex idx = candMan.searchCandidates(ccsreq); OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } XMLWriter.write(out, idx); out.close(); } else { response.sendError(400, "Bad non-POST/GET request /cand/classified"); return; } } else if (path.startsWith("/cand/newclassified")) { if (request.getMethod().equals("POST")) { ClassifiedCandidate cand = null; try { XMLAble xmlable = XMLReader.read(request.getInputStream()); if (xmlable instanceof ClassifiedCandidate) { cand = (ClassifiedCandidate) xmlable; } else { response.sendError(400, "Bad item posted to /cand/newclassified for Classifying"); return; } } catch (SAXException ex) { response.sendError(400, "Bad item posted to /cand/newclassified for Classifying"); return; } cand.setId(0); Session session = new Session(); dbMan.add(cand, session); try { dbMan.save(session); } catch (BookKeeprException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.SEVERE, "error saving database when adding new classified cand", ex); response.sendError(500, "error saving database when adding new classified cand"); } OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } XMLWriter.write(out, cand); out.close(); return; } else { response.sendError(400, "Bad non-POST request /cand/newclassified"); return; } } else if (path.startsWith("/cand/add/")) { // assume that 'add' is not an ID, since it would have server and type = 0, which is invalid. if (request.getMethod().equals("POST")) { String[] elems = path.substring(1).split("/"); if (elems.length < 4) { response.sendError(400, "Bad URL for /cand/add/{psrxmlId}/{procId}"); return; } final long psrxmlId = Long.parseLong(elems[2], 16); final long procId = Long.parseLong(elems[3], 16); if (dbMan.getById(procId) == null) { response.sendError(400, "Bad URL for /cand/add/{psrxmlId}/{procId}. ProcId " + Long.toHexString(procId) + " does not exist!"); return; } if (dbMan.getById(psrxmlId) == null) { response.sendError(400, "Bad URL for /cand/add/{psrxmlId}/{procId}. PsrxmlId " + Long.toHexString(psrxmlId) + " does not exist!"); return; } synchronized (this) { if (nAddSubmitted > 2) { // too many jobs on. to prevent memory overload, end the session! response.setHeader("Retry-After", "60"); response.sendError(503); return; } else { //increment the workload counter nAddSubmitted++; } } final ArrayList<RawCandidate> rawCands = new ArrayList<RawCandidate>(); if (request.getContentType().equals("application/x-tar")) { TarInputStream tarin = new TarInputStream(request.getInputStream()); while (true) { // loop over all entries in the tar file. TarEntry tarEntry = tarin.getNextEntry(); if (tarEntry == null) { break; } else { } InputStream inStream; /* * There is some complication as the XML reader * closes the input stream when it is done, but we * don't want to do that as it closes the entire tar * * So we define a 'UnclosableInputStream' that ignores * the close() command. * * If we have a gzipped xml file, we should pass * through a gzip input stream too. */ if (tarEntry.getName().endsWith(".gz")) { inStream = new UncloseableInputStream(new GZIPInputStream(tarin)); } else { inStream = new UncloseableInputStream(tarin); } try { // parse the xml document. XMLAble xmlable = (XMLAble) XMLReader.read(inStream); if (xmlable instanceof RawCandidate) { rawCands.add((RawCandidate) xmlable); } else { response.sendError(400, "POSTed tar file MUST only contain raw candidates."); return; } } catch (SAXException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, null, ex); response.sendError(400, "POSTed tar file MUST only contain vaild xml candidates."); return; } } // finaly, we can close the tar stream. tarin.close(); Logger.getLogger(CandidateHandler.class.getName()).log(Level.INFO, "Received " + rawCands.size() + " raw candidates for processing from " + request.getRemoteAddr()); final BackgroundedTask bgtask = new BackgroundedTask("AddRawCandidates"); bgtask.setTarget(new Runnable() { public void run() { candMan.addRawCandidates(rawCands, psrxmlId, procId); synchronized (CandidateHandler.this) { // decriment the counter for how many workloads are left to do. CandidateHandler.this.nAddSubmitted--; } } }); bookkeepr.getBackgroundTaskRunner().offer(bgtask); StringBuffer buf = new StringBuffer(); Formatter formatter = new Formatter(buf); formatter.format("%s/%s/%d", bookkeepr.getConfig().getExternalUrl(), "tasks", bgtask.getId()); response.setStatus(303); response.addHeader("Location", buf.toString()); return; } else { response.sendError(400, "Must POST application/x-tar type documents to this URL"); } } else { response.sendError(400, "Bad non-POST request /cand/add"); return; } } else { // see if we are requesting and ID. long targetId = 0; String[] elems = path.substring(1).split("/"); // try and make an int from the passed value. try { if (elems.length < 2) { response.sendError(400, "Bad URL for /cand/{id}"); return; } targetId = Long.parseLong(elems[1], 16); } catch (NumberFormatException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.INFO, "Recieved request for bad id " + elems[1]); response.sendError(400, "Submitted URI was malformed\nMessage was '" + ex.getMessage() + "'"); return; } IdAble idable = dbMan.getById(targetId); if (idable == null) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.INFO, "Recieved request for non-existing ID " + Long.toHexString(targetId)); response.sendError(400, "Submitted request was for non-existing ID " + Long.toHexString(targetId)); return; } if (idable instanceof CandidateListStub) { if (request.getMethod().equals("GET")) { int origin = dbMan.getOrigin(idable); if (dbMan.getOriginId() == origin) { // request for a local item... response.setHeader("Content-Encoding", "gzip"); outputToInput( new FileInputStream(candMan.getCandidateListFile((CandidateListStub) idable)), response.getOutputStream()); } else { HttpClient httpclient = bookkeepr.checkoutHttpClient(); try { // request for a remote item... BookkeeprHost host = bookkeepr.getConfig().getBookkeeprHost(origin); String targetpath = host.getUrl() + path; HttpGet httpreq = new HttpGet(targetpath); HttpResponse httpresp = httpclient.execute(httpreq); for (Header head : httpresp.getAllHeaders()) { if (head.getName().equalsIgnoreCase("transfer-encoding")) { continue; } response.setHeader(head.getName(), head.getValue()); } response.setStatus(httpresp.getStatusLine().getStatusCode()); httpresp.getEntity().writeTo(response.getOutputStream()); } catch (URISyntaxException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Bad uri specified for remote candidate", ex); response.sendError(500, "Bad uri specified for remote candidate"); } catch (HttpException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Could not make a httpreqest for remote Candidate", ex); response.sendError(500, "Could not make a httpreqest for remote Candidate"); } catch (IOException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Could not make a httpreqest for remote Candidate", ex); response.sendError(500, "Could not make a httpreqest for remote Candidate"); } bookkeepr.returnHttpClient(httpclient); // if (host != null) { // response.setStatus(301); // response.addHeader("Location", host.getUrl() + path); // } else { // response.sendError(500, "Cannot find a bookkeepr server for origin id " + origin); // } } } else { response.sendError(400, "Bad non-GET request for a candidate list"); return; } } else if (idable instanceof ClassifiedCandidate) { // /cand/{id} if (request.getMethod().equals("POST")) { // int origin = dbMan.getOrigin(idable); // if (dbMan.getOriginId() == origin) { RawCandidateMatched stub = null; try { XMLAble xmlable = XMLReader.read(request.getInputStream()); if (xmlable instanceof RawCandidateMatched) { stub = (RawCandidateMatched) xmlable; } else { response.sendError(400, "Bad item posted to /cand/... for Classifying"); return; } } catch (SAXException ex) { response.sendError(400, "Bad item posted to /cand/... for Classifying"); return; } ClassifiedCandidate c = null; try { c = (ClassifiedCandidate) ((ClassifiedCandidate) idable).clone(); } catch (CloneNotSupportedException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.SEVERE, null, ex); response.sendError(500, "Server error that cannot happen happened! Classified Candidates are Cloneable!"); return; } c.addRawCandidateMatched(stub); Session session = new Session(); dbMan.add(c, session); try { dbMan.save(session); } catch (BookKeeprException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Error when saving database adding raw to classified cand", ex); response.sendError(500, "Error when saving database adding raw to classified cand"); } OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } XMLWriter.write(out, c); out.close(); // OutputStream out = response.getOutputStream(); // String hdr = request.getHeader("Accept-Encoding"); // if (hdr != null && hdr.contains("gzip")) { // // if the host supports gzip encoding, gzip the output for quick transfer speed. // out = new GZIPOutputStream(out); // response.setHeader("Content-Encoding", "gzip"); // } // XMLWriter.write(out, idx); // out.close(); // } else { // // request for a remote item... // // currently re-direct to the remote server.. perhaps we should pass through? // BookkeeprHost host = bookkeepr.getConfig().getBookkeeprHost(origin); // if (host != null) { // response.setStatus(301); // response.addHeader("Location", host.getUrl() + path); // } else { // response.sendError(500, "Cannot find a bookkeepr server for origin id " + origin); // } // } } else { response.sendError(400, "Bad non-POST request for a classified candidate"); return; } } else if (idable instanceof RawCandidateStub) { if (request.getMethod().equals("GET")) { int origin = dbMan.getOrigin(idable); if (dbMan.getOriginId() == origin) { if (elems.length > 2) { try { int h = 600; int w = 800; String[] parts = elems[2].split("x|\\.png"); if (parts.length > 1) { try { h = Integer.parseInt(parts[1]); w = Integer.parseInt(parts[0]); } catch (NumberFormatException e) { h = 600; w = 800; } } RawCandidate cand = (RawCandidate) XMLReader .read(new GZIPInputStream(new FileInputStream( candMan.getRawCandidateFile((RawCandidateStub) idable)))); response.setContentType("image/png"); BufferedImage img = candMan.makeImageOf(cand, w, h); ImageIO.write(img, "png", response.getOutputStream()); return; } catch (SAXException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.SEVERE, null, ex); } } // request for a local item... response.setHeader("Content-Encoding", "gzip"); outputToInput( new FileInputStream(candMan.getRawCandidateFile((RawCandidateStub) idable)), response.getOutputStream()); } else { HttpClient httpclient = bookkeepr.checkoutHttpClient(); try { // request for a remote item... BookkeeprHost host = bookkeepr.getConfig().getBookkeeprHost(origin); String targetpath = host.getUrl() + path; HttpGet httpreq = new HttpGet(targetpath); HttpResponse httpresp = httpclient.execute(httpreq); for (Header head : httpresp.getAllHeaders()) { if (head.getName().equalsIgnoreCase("transfer-encoding")) { continue; } response.setHeader(head.getName(), head.getValue()); } response.setStatus(httpresp.getStatusLine().getStatusCode()); httpresp.getEntity().writeTo(response.getOutputStream()); } catch (URISyntaxException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Bad uri specified for remote candidate", ex); response.sendError(500, "Bad uri specified for remote candidate"); } catch (HttpException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Could not make a httpreqest for remote Candidate", ex); response.sendError(500, "Could not make a httpreqest for remote Candidate"); } catch (IOException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Could not make a httpreqest for remote Candidate", ex); response.sendError(500, "Could not make a httpreqest for remote Candidate"); } bookkeepr.returnHttpClient(httpclient); } } else { response.sendError(400, "Bad non-GET request for a raw candidate"); return; } } else { Logger.getLogger(CandidateHandler.class.getName()).log(Level.INFO, "Recieved request for non-candidate ID " + targetId); response.sendError(400, "Submitted request was for non-candidate ID " + targetId); return; } } } }
From source file:dk.netarkivet.common.utils.ZipUtils.java
/** GZip a file into a given dir. The resulting file will have .gz * appended./* w ww.j ava 2s. com*/ * * @param f A file to gzip. This must be a real file, not a directory * or the like. * @param toDir The directory that the gzipped file will be placed in. */ private static void gzipFileInto(File f, File toDir) { try { GZIPOutputStream out = null; try { File outF = new File(toDir, f.getName() + GZIP_SUFFIX); out = new GZIPOutputStream(new FileOutputStream(outF)); FileUtils.writeFileToStream(f, out); } finally { if (out != null) { try { out.close(); } catch (IOException e) { // Not really a problem to not be able to close, // so don't abort log.debug("Error closing output file for '" + f + "'", e); } } } } catch (IOException e) { throw new IOFailure("Error while gzipping file '" + f + "'", e); } }
From source file:com.appeligo.channelfeed.work.FileWriter.java
private synchronized void closeDoc() { if (body == null) { return;/* ww w . ja v a 2 s.co m*/ } try { doc = new PrintStream(new GZIPOutputStream(new FileOutputStream(filenamePrefix + ".html.gz"))); writeHead(); } catch (IOException e) { log.error("Can't create .html.gz file.", e); return; } try { String programTitle = program.getProgramTitle(); doc.println("<!-- WARNING, XDS IS INFORMATIONAL ONLY AND CAN BE VERY UNRELIABLE -->"); if (xdsData.getCallLettersAndNativeChannel() != null) { doc.println( "<meta name=\"XDSCallLettersAndNativeChannel\" content=\"" + StringEscapeUtils.escapeHtml( xdsData.getCallLettersAndNativeChannel().replaceAll("[\\p{Cntrl}]", "")) + "\"/>"); } if (xdsData.getNetworkName() != null) { doc.println("<meta name=\"XDSNetworkName\" content=\"" + StringEscapeUtils.escapeHtml(xdsData.getNetworkName().replaceAll("[\\p{Cntrl}]", "")) + "\"/>"); } if (xdsData.getProgramName() != null) { doc.println("<meta name=\"XDSProgramName\" content=\"" + StringEscapeUtils.escapeHtml(xdsData.getProgramName().replaceAll("[\\p{Cntrl}]", "")) + "\"/>"); if (programTitle == null) { programTitle = xdsData.getProgramName(); } } try { if (xdsData.getProgramType() != null) { doc.println("<meta name=\"XDSProgramType\" content=\"" + XDSData.convertProgramType(xdsData.getProgramType()) + "\"/>"); } if (xdsData.getProgramStartTimeID() != null) { long startTimestamp = XDSData.convertProgramStartTimeID(xdsData.getProgramStartTimeID()); doc.println("<meta name=\"XDSProgramStartTimeID\" content=\"" + startTimestamp + "\"/>"); Date date = new Date(startTimestamp); DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG); df.setTimeZone(TimeZone.getTimeZone("GMT")); doc.println("<meta name=\"XDSProgramStartTime\" content=\"" + df.format(date) + "\"/>"); if (XDSData.convertProgramTapeDelayed(xdsData.getProgramStartTimeID())) { doc.println("<meta name=\"XDSProgramTapeDelayed\" content=\"true\"/>"); } } if (xdsData.getProgramLength() != null) { doc.println("<meta name=\"XDSProgramLength\" content=\"" + XDSData.convertProgramLength(xdsData.getProgramLength()) + "\"/>"); } } catch (Exception e) { //ignore program type errors... probably bad data... such is XDS } if (program != null) { doc.println("<meta name=\"ProgramID\" content=\"" + program.getProgramId() + "\"/>"); doc.println("<meta name=\"ScheduleID\" content=\"" + program.getScheduleId() + "\"/>"); doc.println("<meta name=\"EpisodeTitle\" content=\"" + StringEscapeUtils.escapeHtml(program.getEpisodeTitle()) + "\"/>"); doc.println("<meta name=\"StartTime\" content=\"" + program.getStartTime().getTime() + "\"/>"); doc.println("<meta name=\"EndTime\" content=\"" + program.getEndTime().getTime() + "\"/>"); doc.println("<meta name=\"TVRating\" content=\"" + StringEscapeUtils.escapeHtml(program.getTvRating()) + "\"/>"); if (programTitle == null) { programTitle = Long.toString(program.getStartTime().getTime()); } } doc.println("<title>" + StringEscapeUtils.escapeHtml(programTitle) + "</title>"); doc.println("</head>"); doc.println("<body>"); doc.println("<h1>" + StringEscapeUtils.escapeHtml(programTitle) + "</h1>"); body.close(); body = null; InputStream readBody = new BufferedInputStream(new FileInputStream(filenamePrefix + ".body")); int b = readBody.read(); while (b >= 0) { doc.write(b); b = readBody.read(); } readBody.close(); File f = new File(filenamePrefix + ".body"); f.delete(); writeFoot(); doc.close(); doc = null; } catch (FileNotFoundException e) { log.error("Lost .body file!", e); } catch (IOException e) { log.error("Error copying .body to .html.gz", e); } doc = null; }
From source file:halive.shootinoutside.common.core.game.map.GameMap.java
public byte[] serializeMapToCompressedByteArray() throws IOException { //Compress data ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzipOut = new GZIPOutputStream(out); gzipOut.write(serializeMapToJSONString().getBytes()); gzipOut.flush();/*from w w w . ja v a 2s . c om*/ gzipOut.finish(); return out.toByteArray(); }
From source file:com.github.jasonruckman.gzip.AbstractBenchmark.java
protected byte[] doUnsafeWriteSidney() { try {//from w w w . j a va2s . c o m ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = new GZIPOutputStream(baos); Writer<T> writer = unsafeWriter.get(); writer.open(gzos); for (T t : sampleData) { writer.write(t); } writer.close(); gzos.close(); return baos.toByteArray(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:gzipper.algorithms.Gzip.java
@Override public void run() { /*check whether archive with given name already exists; if so, add index to file name an re-check*/ if (_createArchive) { try {/*from w w w. j av a 2s . c o m*/ File file = new File(_path + _archiveName + ".tar.gz"); while (file.exists()) { ++_nameIndex; _archiveName = _archiveName.substring(0, 7) + _nameIndex; file = new File(_path + _archiveName + ".tar.gz"); } _tos = new TarArchiveOutputStream(new GZIPOutputStream( new BufferedOutputStream(new FileOutputStream(_path + _archiveName + ".tar.gz")))); _tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); _tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR); } catch (IOException ex) { Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, "Error creating output stream", ex); System.exit(1); } } while (_runFlag) { try { long startTime = System.nanoTime(); if (_selectedFiles != null) { if (_createArchive != false) { compress(_selectedFiles, ""); } else { extract(_path, _archiveName); } } else { throw new GZipperException("File selection must not be null"); } _elapsedTime = System.nanoTime() - startTime; } catch (IOException | GZipperException ex) { Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, "Error compressing archive", ex); } finally { stop(); //stop thread after successful operation } } }
From source file:com.parse.ParseHttpClientTest.java
private void doSingleParseHttpClientExecuteWithGzipResponse(int responseCode, String responseStatus, final String responseContent, ParseHttpClient client) throws Exception { MockWebServer server = new MockWebServer(); // Make mock response Buffer buffer = new Buffer(); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); GZIPOutputStream gzipOut = new GZIPOutputStream(byteOut); gzipOut.write(responseContent.getBytes()); gzipOut.close();/* w w w . j a v a 2 s .c om*/ buffer.write(byteOut.toByteArray()); MockResponse mockResponse = new MockResponse().setStatus("HTTP/1.1 " + responseCode + " " + responseStatus) .setBody(buffer).setHeader("Content-Encoding", "gzip"); // Start mock server server.enqueue(mockResponse); server.start(); // We do not need to add Accept-Encoding header manually, httpClient library should do that. String requestUrl = server.getUrl("/").toString(); ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(requestUrl) .setMethod(ParseHttpRequest.Method.GET).build(); // Execute request ParseHttpResponse parseResponse = client.execute(parseRequest); RecordedRequest recordedRequest = server.takeRequest(); // Verify request method assertEquals(ParseHttpRequest.Method.GET.toString(), recordedRequest.getMethod()); // Verify request headers Headers recordedHeaders = recordedRequest.getHeaders(); assertEquals("gzip", recordedHeaders.get("Accept-Encoding")); // Verify response body byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent()); assertArrayEquals(responseContent.getBytes(), content); // Shutdown mock server server.shutdown(); }
From source file:de.xwic.appkit.core.remote.server.RemoteDataAccessServlet.java
/** * @param req/* w ww . jav a 2s . c o m*/ * @param resp * @throws IOException */ private void handleRequest(final HttpServletRequest req, final HttpServletResponse resp) throws IOException { // This method can be called repeatedly to change the character encoding. // This method has no effect if it is called after getWriter has been called or after // the response has been committed. resp.setContentType("text/xml; charset=UTF-8"); try { IParameterProvider pp = new ParameterProvider(req); // the API is taking its arguments from the URL and the parameters final String action = pp.getParameter(PARAM_ACTION); if (action == null || action.isEmpty()) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Missing Parameters"); return; } IRequestHandler handler = handlers.get(action); if (handler != null) { handler.handle(pp, resp); } else { // all responses will now basically be an XML document, so we can do some preparations PrintWriter pwOut = null; if (useCompression && (action.equals(ACTION_GET_ENTITIES) || action.equals(ACTION_GET_COLLECTION))) { pwOut = new PrintWriter(new GZIPOutputStream(resp.getOutputStream())); resp.setHeader("Content-Encoding", "gzip"); } else { pwOut = resp.getWriter(); } String entityType = req.getParameter(PARAM_ENTITY_TYPE); assertValue(entityType, "Entity Type not specified"); if (action.equals(ACTION_GET_ENTITY)) { handleGetEntity(entityType, req, resp, pwOut); } else if (action.equals(ACTION_GET_ENTITIES)) { handleGetEntities(entityType, req, resp, pwOut); } else if (action.equals(ACTION_UPDATE_ENTITY)) { handleUpdateEntity(entityType, req, resp, pwOut); } else if (action.equals(ACTION_GET_COLLECTION)) { handleGetCollection(entityType, req, resp, pwOut); } else if (action.equals(ACTION_DELETE) || action.equals(ACTION_SOFT_DELETE)) { handleDelete(entityType, req, resp, pwOut, action.equals(ACTION_SOFT_DELETE)); } else { throw new IllegalArgumentException("Unknown action"); } pwOut.flush(); pwOut.close(); } } catch (Exception e) { log.error(e.getMessage(), e); resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); } }
From source file:edu.cornell.med.icb.goby.alignments.UpgradeTo1_9_6.java
private void writeIndex(String basename, LongArrayList indexOffsets, LongArrayList indexAbsolutePositions) throws IOException { GZIPOutputStream indexOutput = null; try {//w w w.ja v a 2 s . co m FileUtils.moveFile(new File(basename + ".index"), new File(makeBackFilename(basename + ".index", ".bak"))); indexOutput = new GZIPOutputStream(new FileOutputStream(basename + ".index")); final Alignments.AlignmentIndex.Builder indexBuilder = Alignments.AlignmentIndex.newBuilder(); assert (indexOffsets.size() == indexAbsolutePositions.size()) : "index sizes must be consistent."; indexBuilder.addAllOffsets(indexOffsets); indexBuilder.addAllAbsolutePositions(indexAbsolutePositions); indexBuilder.build().writeTo(indexOutput); } finally { if (indexOutput != null) indexOutput.close(); } }
From source file:net.contrapunctus.rngzip.io.RNGZSettings.java
/** * Filter an output stream through a compressor, as specified by * cm. That is, if cm is <code>GZ</code>, this method will * return <code>new GZIPOutputStream(out)</code>. *///from w w w . jav a 2s .c om public static OutputStream wrapOutput(OutputStream out, DataCompression cm) throws IOException { switch (cm) { case NONE: break; case GZ: out = new GZIPOutputStream(out); break; case BZ2: out = new CBZip2OutputStream(out); break; case LZMA: out = new LzmaOutputStream(out); break; case PPM: out = new ArithCodeOutputStream(out, new PPMModel(PPM_SMALL_LENGTH)); break; case PPMX: out = new ArithCodeOutputStream(out, new PPMModel(PPM_LARGE_LENGTH)); break; // here's how it would work for external stuff: //out = (OutputStream) externalInstance // ("org.apache.commons.compress.bzip2.CBZip2OutputStream", // OutputStream.class, out); //break; default: assert false; } return out; }