public static String decompress(byte[] data) { int length = ByteBuffer.wrap(Arrays.copyOfRange(data, 0, 4)).getInt(); if (length > 100000) { // This is a sanity check. More than 100kb of password settings make no sense. System.out.println("Decompression error: The trasferred length is too big."); return ""; } Inflater inflater = new Inflater(); inflater.setInput(data, 4, data.length - 4); byte[] decompressedBytes = new byte[length]; try { if (inflater.inflate(decompressedBytes) != length) { throw new AssertionError(); } } catch (DataFormatException e) { e.printStackTrace(); } inflater.end(); try { return new String(decompressedBytes, "UTF-8"); } catch (UnsupportedEncodingException e) { System.out.println( "Decompression error: UTF-8 is not supported. " + "Using default encoding."); return new String(decompressedBytes); } }
/* (non-Javadoc) * @see org.eclipse.jetty.websocket.AbstractExtension#onFrame(byte, byte, org.eclipse.jetty.io.Buffer) */ @Override public void onFrame(byte flags, byte opcode, Buffer buffer) { if (getConnection().isControl(opcode) || !isFlag(flags, 1)) { super.onFrame(flags, opcode, buffer); return; } if (buffer.array() == null) buffer = buffer.asMutableBuffer(); int length = 0xff & buffer.get(); if (length >= 0x7e) { int b = (length == 0x7f) ? 8 : 2; length = 0; while (b-- > 0) length = 0x100 * length + (0xff & buffer.get()); } // TODO check a max framesize _inflater.setInput(buffer.array(), buffer.getIndex(), buffer.length()); ByteArrayBuffer buf = new ByteArrayBuffer(length); try { while (_inflater.getRemaining() > 0) { int inflated = _inflater.inflate(buf.array(), buf.putIndex(), buf.space()); if (inflated == 0) throw new DataFormatException("insufficient data"); buf.setPutIndex(buf.putIndex() + inflated); } super.onFrame(clearFlag(flags, 1), opcode, buf); } catch (DataFormatException e) { LOG.warn(e); getConnection().close(WebSocketConnectionRFC6455.CLOSE_BAD_PAYLOAD, e.toString()); } }
/** Start the thread. Exits when the client has been completely handled. */ @Override public void run() { try { GELFClientHandlerIF client = null; if (GELF.isChunkedMessage(this.receivedGelfSentence)) { LOG.info("Received message is chunked. Handling now."); client = new ChunkedGELFClientHandler(this.receivedGelfSentence); } else { LOG.info("Received message is not chunked. Handling now."); client = new SimpleGELFClientHandler(this.receivedGelfSentence); } client.handle(); } catch (InvalidGELFTypeException e) { LOG.error("Invalid GELF type in message: " + e.getMessage(), e); } catch (InvalidGELFHeaderException e) { LOG.error("Invalid GELF header in message: " + e.getMessage(), e); } catch (InvalidGELFCompressionMethodException e) { LOG.error("Invalid compression method of GELF message: " + e.getMessage(), e); } catch (java.util.zip.DataFormatException e) { LOG.error("Invalid compression data format in GELF message: " + e.getMessage(), e); } catch (java.io.UnsupportedEncodingException e) { LOG.error("Invalid enconding of GELF message: " + e.getMessage(), e); } catch (java.io.EOFException e) { LOG.error("EOF Exception while handling GELF message: " + e.getMessage(), e); } catch (java.net.SocketException e) { LOG.error("SocketException while handling GELF message: " + e.getMessage(), e); } catch (java.io.IOException e) { LOG.error("IO Error while handling GELF message: " + e.getMessage(), e); } catch (Exception e) { LOG.error("Exception caught while handling GELF message: " + e.getMessage(), e); } }
/** * Reads uncompressed data into an array of bytes. If <code>len</code> is not zero, the method * will block until some input can be decompressed; otherwise, no bytes are read and <code>0 * </code> is returned. * * @param b the buffer into which the data is read * @param off the start offset in the destination array <code>b</code> * @param len the maximum number of bytes read * @return the actual number of bytes read, or -1 if the end of the compressed input is reached or * a preset dictionary is needed * @exception NullPointerException If <code>b</code> is <code>null</code>. * @exception IndexOutOfBoundsException If <code>off</code> is negative, <code>len</code> is * negative, or <code>len</code> is greater than <code>b.length - off</code> * @exception ZipException if a ZIP format error has occurred * @exception IOException if an I/O error has occurred */ public int read(byte[] b, int off, int len) throws IOException { ensureOpen(); if (b == null) { throw new NullPointerException(); } else if (off < 0 || len < 0 || len > b.length - off) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return 0; } try { int n; while ((n = inf.inflate(b, off, len)) == 0) { if (inf.finished() || inf.needsDictionary()) { reachEOF = true; return -1; } if (inf.needsInput()) { fill(); } } return n; } catch (DataFormatException e) { String s = e.getMessage(); throw new ZipException(s != null ? s : "Invalid ZLIB data format"); } }
public synchronized int decompress(byte[] b, int off, int len) throws IOException { try { return super.inflate(b, off, len); } catch (DataFormatException dfe) { throw new IOException(dfe.getMessage()); } }
private int inflate(byte[] bytes) throws ZipException { try { return inflater.inflate(bytes); } catch (DataFormatException x) { throw new ZipException(x.getMessage()); } }
private final byte[] uncompress(final byte[] input) throws IOException { Inflater decompressor = new Inflater(); decompressor.setInput(input); // Create an expandable byte array to hold the decompressed data ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); // Decompress the data byte[] buf = new byte[1024]; while (!decompressor.finished()) { try { int count = decompressor.inflate(buf); bos.write(buf, 0, count); } catch (DataFormatException e) { // this will happen if the field is not compressed IOException newException = new IOException("field data are in wrong format: " + e.toString()); newException.initCause(e); throw newException; } } decompressor.end(); // Get the decompressed data return bos.toByteArray(); }
/** TIFF Adobe ZIP support contributed by Jason Newton. */ public byte[] zipUncompress(byte[] input) { ByteArrayOutputStream imageBuffer = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; Inflater decompressor = new Inflater(); decompressor.setInput(input); try { while (!decompressor.finished()) { int rlen = decompressor.inflate(buffer); imageBuffer.write(buffer, 0, rlen); } } catch (DataFormatException e) { IJ.log(e.toString()); } decompressor.end(); return imageBuffer.toByteArray(); }
/** * Reads from the compressed stream and stores the resulting uncompressed data into the byte * array. * * @return number of bytes read, or -1 upon EOF */ public int read(byte[] b, int off, int len) throws IOException { if (len <= 0 || off < 0 || off + len > b.length) return 0; if (_eof) return -1; // Read from uncompressed stream if (!_isGzip) return _in.read(b, off, len); try { int sublen; int length = 0; while (length < len) { if (_inflater.needsInput()) { _readBufferSize = _in.read(_readBuffer, 0, _readBuffer.length); if (_readBufferSize < 0) break; _inflater.setInput(_readBuffer, 0, _readBufferSize); } sublen = _inflater.inflate(b, off + length, len - length); _crc.update(b, off + length, sublen); _inputSize += sublen; _totalInputSize += sublen; length += sublen; // Unread gzip trailer and possibly beginning of appended gzip data. if (_inflater.finished()) { int remaining = _inflater.getRemaining(); _in.unread(_readBuffer, _readBufferSize - remaining, remaining); readTrailer(); int secondPart = read(b, off + length, len - length); return secondPart > 0 ? length + secondPart : length; } } return length; } catch (DataFormatException e) { throw new IOException(e.getMessage()); } }
/** Parse out and decompress the data part of a fileblock helper function. */ FileBlock parseData(byte buf[]) throws InvalidProtocolBufferException { FileBlock out = FileBlock.newInstance(type, indexdata); Fileformat.Blob blob = Fileformat.Blob.parseFrom(buf); if (blob.hasRaw()) { out.data = blob.getRaw(); } else if (blob.hasZlibData()) { byte buf2[] = new byte[blob.getRawSize()]; Inflater decompresser = new Inflater(); decompresser.setInput(blob.getZlibData().toByteArray()); // decompresser.getRemaining(); try { decompresser.inflate(buf2); } catch (DataFormatException e) { e.printStackTrace(); throw new Error(e); } assert (decompresser.finished()); decompresser.end(); out.data = ByteString.copyFrom(buf2); } return out; }
public int read() throws IOException { if (pointer == -1) { try { length = doRead(readBuffer, 0, readBuffer.length); if (length == 0) { return -1; } pointer = 0; } catch (DataFormatException e) { IOException e2 = new IOException(e.getMessage()); e2.initCause(e); throw e2; } } int value = readBuffer[pointer] & 0xFF; pointer++; if (pointer == length) { pointer = -1; } return value; }
public String processCreate() { try { if (!this.checkAccess()) { throw new PermissionException( SessionManager.getCurrentSessionUserId(), "syllabus_access_athz", ""); } } catch (PermissionException e) { // logger.info(this + ".getEntries() in PostemTool " + e); FacesContext.getCurrentInstance() .addMessage( null, MessageUtils.getMessage( FacesMessage.SEVERITY_ERROR, "error_permission", (new Object[] {e.toString()}), FacesContext.getCurrentInstance())); this.currentGradebook = null; this.csv = null; this.newTemplate = null; // this.release = null; return "permission_error"; } if (currentGradebook.getId() == null) { ArrayList gb = getGradebooks(); Iterator gi = gb.iterator(); while (gi.hasNext()) { if (((Gradebook) gi.next()).getTitle().equals(currentGradebook.getTitle())) { // To stay consistent, remove current messages when adding a new message // so as to only display one error message before returning PostemTool.clearMessages(); PostemTool.populateMessage( FacesMessage.SEVERITY_ERROR, "duplicate_title", new Object[] {}); return "create_gradebook"; } } } if (currentGradebook.getTitle() == null || currentGradebook.getTitle().equals("")) { // To stay consistent, remove current messages when adding a new message // so as to only display one error message before returning PostemTool.clearMessages(); PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR, "missing_title", new Object[] {}); return "create_gradebook"; } else if (currentGradebook.getTitle().trim().length() > TITLE_MAX_LENGTH) { PostemTool.clearMessages(); PostemTool.populateMessage( FacesMessage.SEVERITY_ERROR, "title_too_long", new Object[] { new Integer(currentGradebook.getTitle().trim().length()), new Integer(TITLE_MAX_LENGTH) }); return "create_gradebook"; } Reference attachment = getAttachmentReference(); if (attachment == null) { return "create_gradebook"; } if (!this.delimiter.equals(COMMA_DELIM_STR) && !this.delimiter.equals(TAB_DELIM_STR)) { PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR, "invalid_delim", new Object[] {}); return "create_gradebook"; } if (attachment != null) { // logger.info("*** Non-Empty CSV!"); try { char csv_delim = CSV.COMMA_DELIM; if (this.delimiter.equals(TAB_DELIM_STR)) { csv_delim = CSV.TAB_DELIM; } // Read the data ContentResource cr = contentHostingService.getResource(attachment.getId()); // Check the type if (ResourceProperties.TYPE_URL.equalsIgnoreCase(cr.getContentType())) { // Going to need to read from a stream String csvURL = new String(cr.getContent()); // Load the URL csv = URLConnectionReader.getText(csvURL); if (LOG.isDebugEnabled()) { LOG.debug(csv); } } else { csv = new String(cr.getContent()); if (LOG.isDebugEnabled()) { LOG.debug(csv); } } CSV grades = new CSV(csv, withHeader, csv_delim); if (withHeader == true) { if (grades.getHeaders() != null) { List headingList = grades.getHeaders(); for (int col = 0; col < headingList.size(); col++) { String heading = (String) headingList.get(col).toString().trim(); // Make sure there are no blank headings if (heading == null || heading.equals("")) { PostemTool.populateMessage( FacesMessage.SEVERITY_ERROR, "blank_headings", new Object[] {}); return "create_gradebook"; } // Make sure the headings don't exceed max limit if (heading.length() > HEADING_MAX_LENGTH) { PostemTool.populateMessage( FacesMessage.SEVERITY_ERROR, "heading_too_long", new Object[] {new Integer(HEADING_MAX_LENGTH)}); return "create_gradebook"; } } } } if (grades.getStudents() != null) { if (!usernamesValid(grades)) { return "create_gradebook"; } if (hasADuplicateUsername(grades)) { return "create_gradebook"; } } if (this.newTemplate != null && this.newTemplate.trim().length() > 0) { if (this.newTemplate.trim().length() > TEMPLATE_MAX_LENGTH) { PostemTool.populateMessage( FacesMessage.SEVERITY_ERROR, "template_too_long", new Object[] { new Integer(this.newTemplate.trim().length()), new Integer(TEMPLATE_MAX_LENGTH) }); return "create_gradebook"; } } if (withHeader == true) { if (grades.getHeaders() != null) { PostemTool.populateMessage(FacesMessage.SEVERITY_INFO, "has_headers", new Object[] {}); } } if (grades.getStudents() != null) { PostemTool.populateMessage( FacesMessage.SEVERITY_INFO, "has_students", new Object[] {new Integer(grades.getStudents().size())}); } if (withHeader == true) { currentGradebook.setHeadings(grades.getHeaders()); } List slist = grades.getStudents(); if (oldGradebook.getId() != null && !this.userPressedBack) { Set oldStudents = currentGradebook.getStudents(); oldGradebook.setStudents(oldStudents); } currentGradebook.setStudents(new TreeSet()); // gradebookManager.saveGradebook(currentGradebook); Iterator si = slist.iterator(); while (si.hasNext()) { List ss = (List) si.next(); String uname = ((String) ss.remove(0)).trim(); // logger.info("[POSTEM] processCreate -- adding student " + // uname); gradebookManager.createStudentGradesInGradebook(uname, ss, currentGradebook); if (currentGradebook.getStudents().size() == 1) { currentGradebook.setFirstUploadedUsername( uname); // otherwise, the verify screen shows first in ABC order } } } catch (DataFormatException exception) { /* * TODO: properly subclass exception in order to allow for localized * messages (add getRowNumber/setRowNumber). Set exception message to be * key in .properties file */ PostemTool.populateMessage( FacesMessage.SEVERITY_ERROR, exception.getMessage(), new Object[] {}); return "create_gradebook"; } catch (IdUnusedException e) { PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), new Object[] {}); return "create_gradebook"; } catch (TypeException e) { PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), new Object[] {}); return "create_gradebook"; } catch (PermissionException e) { PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), new Object[] {}); return "create_gradebook"; } catch (ServerOverloadException e) { PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), new Object[] {}); return "create_gradebook"; } catch (IOException e) { PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), new Object[] {}); return "create_gradebook"; } } else if (this.csv != null) { // logger.info("**** Non Null Empty CSV!"); PostemTool.populateMessage( FacesMessage.SEVERITY_ERROR, "has_students", new Object[] {new Integer(0)}); currentGradebook.setHeadings(new ArrayList()); if (oldGradebook.getId() != null) { Set oldStudents = currentGradebook.getStudents(); oldGradebook.setStudents(oldStudents); } currentGradebook.setStudents(new TreeSet()); } if (this.newTemplate != null && this.newTemplate.trim().length() > 0) { currentGradebook.setTemplate(gradebookManager.createTemplate(newTemplate.trim())); } else if (this.newTemplate != null) { // logger.info("*** Non Null Empty Template!"); currentGradebook.setTemplate(null); } /* * if("No".equals(this.release)) { currentGradebook.setReleased(new * Boolean(false)); //logger.info("Set to No, " + * currentGradebook.getReleased()); } else { * currentGradebook.setReleased(new Boolean(true)); //logger.info("Set to * Yes, " + currentGradebook.getReleased()); } */ // gradebookManager.saveGradebook(currentGradebook); // logger.info(currentGradebook.getId()); // currentGradebook = null; if ((this.csv != null && this.csv.trim().length() > 0) || (this.newTemplate != null && this.newTemplate.trim().length() > 0)) { this.csv = null; this.newTemplate = null; return "verify"; } Iterator oi = oldGradebook.getStudents().iterator(); while (oi.hasNext()) { gradebookManager.deleteStudentGrades((StudentGrades) oi.next()); } this.userId = SessionManager.getCurrentSessionUserId(); currentGradebook.setLastUpdated(new Timestamp(new Date().getTime())); currentGradebook.setLastUpdater(this.userId); gradebookManager.saveGradebook(currentGradebook); this.currentGradebook = null; this.oldGradebook = null; this.withHeader = true; // this.gradebooks = null; return "main"; }
@Override public CompressedChunkMessage decode(ChannelBuffer buffer) throws IOException { int x = buffer.readInt(); int z = buffer.readInt(); boolean contiguous = buffer.readByte() == 1; short primaryBitMap = buffer.readShort(); short addBitMap = buffer.readShort(); int compressedSize = buffer.readInt(); int unused = buffer.readInt(); byte[] compressedData = new byte[compressedSize]; buffer.readBytes(compressedData); boolean[] hasAdditionalData = new boolean[MAX_SECTIONS]; byte[][] data = new byte[MAX_SECTIONS][]; int size = 0; for (int i = 0; i < MAX_SECTIONS; ++i) { if ((primaryBitMap & 1 << i) != 0) { // This chunk exists! Let's initialize the data for it. int sectionSize = SIXTEEN_CUBED * 5 / 2; if ((addBitMap & 1 << i) != 0) { hasAdditionalData[i] = true; sectionSize += SIXTEEN_CUBED / 2; } data[i] = new byte[sectionSize]; size += sectionSize; } } if (contiguous) { size += Chunk.CHUNK_SIZE * Chunk.CHUNK_SIZE; } byte[] uncompressedData = new byte[size]; Inflater inflater = new Inflater(); inflater.setInput(compressedData); inflater.getRemaining(); try { int uncompressed = inflater.inflate(uncompressedData); if (uncompressed == 0) { throw new IOException("Not all bytes uncompressed."); } } catch (DataFormatException e) { e.printStackTrace(); throw new IOException("Bad compressed data."); } finally { inflater.end(); } size = 0; for (byte[] sectionData : data) { if (sectionData != null && sectionData.length + size < uncompressedData.length) { System.arraycopy(uncompressedData, size, sectionData, 0, sectionData.length); size += sectionData.length; } } byte[] biomeData = new byte[Chunk.CHUNK_SIZE * Chunk.CHUNK_SIZE]; if (contiguous) { System.arraycopy(uncompressedData, size, biomeData, 0, biomeData.length); size += biomeData.length; } return new CompressedChunkMessage(x, z, contiguous, hasAdditionalData, unused, data, biomeData); }
private void readRecord(int recordSize) throws IOException { int uid = PdbUtil.readShort(myStream); if (uid == 1) { myCompressionVersion = (short) PdbUtil.readShort(myStream); } else { int paragraphs = PdbUtil.readShort(myStream); int size = PdbUtil.readShort(myStream); // TODO ?????? int type = myStream.read(); int flags = myStream.read(); switch (type) { case 0: // text (TODO: found sample file and test this code) case 1: // compressed text { ArrayList /*<Integer>*/ pars = new ArrayList(); for (int i = 0; i < paragraphs; ++i) { int pSize = PdbUtil.readShort(myStream); pars.add(pSize); myStream.skip(2); } boolean doProcess = false; if (type == 0) { // ? byte[] buf = new byte[size]; doProcess = myStream.read(buf, 0, (int) size) == size; if (doProcess) { // TODO: use encoding!!!! // TODO: don't create any new objects!!!! myCharBuffer = new String(buf).toCharArray(); } } else if (myCompressionVersion == 1) { byte[] buf = new byte[size]; doProcess = DocDecompressor.decompress(myStream, buf, recordSize - 8 - 4 * paragraphs) == size; if (doProcess) { myCharBuffer = new String(buf).toCharArray(); } } else if (myCompressionVersion == 2) { byte input[] = new byte[(int) (recordSize - 10 - 4 * paragraphs)]; final int inputSize = myStream.read(input); Inflater decompressor = new Inflater(); decompressor.setInput(input, 0, inputSize); byte output[] = new byte[size]; try { doProcess = decompressor.inflate(output) == size; decompressor.end(); myCharBuffer = new String(output, 0, size).toCharArray(); } catch (DataFormatException e) { // TODO Auto-generated catch block // e.printStackTrace(); System.out.println(e.getMessage()); } // doProcess = // ZLZDecompressor(recordSize - 10 - 4 * paragraphs). // decompress(myStream, myCharBuffer, size) == size; } if (doProcess) { addHyperlinkLabel(fromNumber(uid)); myParagraphMap.put(uid, new ArrayList()); myParagraphVector = (ArrayList) myParagraphMap.get(uid); processTextRecord(size, pars); if ((flags & 0x1) == 0) { // insertEndOfTextParagraph(); // setNewTextModel(); } } break; } case 2: // image case 3: // compressed image { final String mime = "image/palm"; ZLImage image = null; if (type == 2) { System.out.println("non-compressed image"); image = new PluckerFileImage(mime, myFile, myStream.offset(), recordSize - 8); } else if (myCompressionVersion == 1) { System.out.println("DocCompressedImage"); image = new DocCompressedFileImage(mime, myFile, myStream.offset(), recordSize - 8); } else if (myCompressionVersion == 2) { System.out.println("ZCompressedImage"); image = new ZCompressedFileImage(mime, myFile, myStream.offset() + 2, recordSize - 10); } if (image != null) { addImage(fromNumber(uid), image); } break; } case 9: // category record is ignored break; case 10: short typeCode = (short) PdbUtil.readShort(myStream); break; case 11: // style sheet record is ignored break; case 12: // font page record is ignored break; case 13: // TODO: process tables case 14: // TODO: process tables break; case 15: // multiimage { short columns = (short) PdbUtil.readShort(myStream); short rows = (short) PdbUtil.readShort(myStream); System.out.println("multiimage"); /*PluckerMultiImage image = new PluckerMultiImage(rows, columns, Model.getImageMap()); for (int i = 0; i < size / 2 - 2; ++i) { short us = (short)myStream.read(); PdbUtil.readShort(myStream, us); image.addId(fromNumber(us)); } addImage(fromNumber(uid), image); */ break; } default: // std::cerr << "type = " << (int)type << "\n"; break; } } }