/** * Inflate the given byte array by {@link #INFLATED_ARRAY_LENGTH}. * * @param bytes the bytes * @return the array as a string with <code>UTF-8</code> encoding */ public static String inflate(final byte[] bytes) { try (ByteArrayInputStream inb = new ByteArrayInputStream(bytes); ByteArrayOutputStream out = new ByteArrayOutputStream(); InflaterOutputStream ios = new InflaterOutputStream(out);) { IOUtils.copy(inb, ios); return new String(out.toByteArray(), UTF8_ENCODING); } catch (final Exception e) { throw new RuntimeException(e.getMessage(), e); } }
public static String decompressData(String encdata) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); InflaterOutputStream zos = new InflaterOutputStream(bos); zos.write(getdeBASE64inCodec(encdata)); zos.close(); return new String(bos.toByteArray()); } catch (Exception ex) { ex.printStackTrace(); return "UNZIP_ERR"; } }
public static String decompressData(String encdata, String charset) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); InflaterOutputStream zos = new InflaterOutputStream(bos); zos.write(getdeBASE64inCodec(encdata)); zos.close(); return new String(bos.toByteArray(), charset); } catch (Exception ex) { ex.printStackTrace(); return "UNZIP_ERR"; } }
private String inflate(byte[] decodedBytes) throws IOException { ByteArrayOutputStream inflatedBytes = new ByteArrayOutputStream(); Inflater inflater = new Inflater(true); InflaterOutputStream inflaterStream = new InflaterOutputStream(inflatedBytes, inflater); inflaterStream.write(decodedBytes); inflaterStream.finish(); return new String(inflatedBytes.toByteArray()); }
public static byte[] decompressData(String encdata) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); InflaterOutputStream zos = new InflaterOutputStream(bos); byte[] bs = decryptBASE64(encdata); zos.write(bs); zos.close(); return bos.toByteArray(); }
public static byte[] decompressZlib(byte[] input) { try { Inflater decompress = new Inflater(); decompress.setInput(input); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(byteArrayOutputStream, decompress); inflaterOutputStream.flush(); return byteArrayOutputStream.toByteArray(); } catch (Exception e) { System.out.println("An error occured while decompressing data"); System.exit(-2); } return null; }
public static TileMap deserialize(JSONObject object) { TileMap tileMap = new TileMap(World.WORLD_SIZE, World.WORLD_SIZE); byte[] compressedBytes = Base64.getDecoder().decode((String) object.get("z")); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Inflater decompressor = new Inflater(true); InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(baos, decompressor); inflaterOutputStream.write(compressedBytes); inflaterOutputStream.close(); byte[] terrain = baos.toByteArray(); for (int x = 0; x < World.WORLD_SIZE; x++) { for (int y = 0; y < World.WORLD_SIZE; y++) { tileMap.tiles[x][y] = terrain[x * World.WORLD_SIZE + y]; } } return tileMap; } catch (IOException e) { e.printStackTrace(); } return null; }
public static Memory deserialize(DBObject obj) { Memory memory = new Memory(0); String zipBytesStr = (String) obj.get("zipBytes"); if (zipBytesStr != null) { byte[] compressedBytes = Base64.getDecoder().decode((String) obj.get("zipBytes")); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Inflater decompressor = new Inflater(true); InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(baos, decompressor); inflaterOutputStream.write(compressedBytes); inflaterOutputStream.close(); memory.setBytes(baos.toByteArray()); } catch (IOException e) { e.printStackTrace(); } } else { LogManager.LOGGER.severe("Memory was manually deleted"); memory = new Memory(GameServer.INSTANCE.getConfig().getInt("memory_size")); } return memory; }
public static byte[] zlib_decompress(byte[] encdata,int offset) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); InflaterOutputStream inf = new InflaterOutputStream(out); inf.write(encdata,offset, encdata.length-offset); inf.close(); return out.toByteArray(); } catch (Exception ex) { ex.printStackTrace(); return "ERR".getBytes(); } }
public static byte[] zlib_decompress(byte[] encdata,int offset,int size) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); InflaterOutputStream inf = new InflaterOutputStream(out); inf.write(encdata,offset, size); inf.close(); return out.toByteArray(); } catch (Exception ex) { ex.printStackTrace(); return "ERR".getBytes(); } }
public static String zlib_decompress_to_str(byte[] encdata,int offset) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); InflaterOutputStream inf = new InflaterOutputStream(out); inf.write(encdata,offset, encdata.length-offset); inf.close(); return out.toString("ASCII"); } catch (Exception ex) { ex.printStackTrace(); return "ERR"; } }
public static byte[] zlib_decompress(byte[] encdata,int offset,int size) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); InflaterOutputStream inf = new InflaterOutputStream(out); inf.write(encdata,offset, size); inf.close(); return out.toByteArray(); } catch (Exception ex) { ex.printStackTrace(); show(emptyStr); return "ERR".getBytes(); } }
private JsonNode receiveCompressedMessage() throws IOException { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(bos); inflaterOutputStream.write(server.getReceivedData()); inflaterOutputStream.close(); return new ObjectMapper().readTree(bos.toByteArray()); }
private byte[] inflate(byte[] uncompressed) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try (InflaterOutputStream deflaterOutputStream = new InflaterOutputStream(byteArrayOutputStream)) { deflaterOutputStream.write(uncompressed); } catch (IOException e) { throw new RuntimeException(e); } return byteArrayOutputStream.toByteArray(); }
private Object[] inflate(byte[] data) throws LuaException { if (data.length >= Config.APIs.Data.limit) throw new LuaException("Data is too long"); ByteArrayOutputStream baos = new ByteArrayOutputStream(512); InflaterOutputStream inos = new InflaterOutputStream(baos, new Inflater(true)); try { inos.write(data); inos.finish(); } catch (IOException e) { throw LuaHelpers.rewriteException(e, "Inflating error"); } return new Object[]{baos.toByteArray()}; }
protected void readCompressedData(byte[] readData) throws IOException { if (readData.length == originalLength) { this.tableData = readData; return; } ByteArrayOutputStream out = new ByteArrayOutputStream(); InflaterOutputStream compressStream = new InflaterOutputStream(out); compressStream.write(readData); compressStream.close(); tableData = out.toByteArray(); }
public static byte[] gunzipBytes(byte[] compressedBytes) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream((int)(compressedBytes.length * 1.5)); InflaterOutputStream dos = new InflaterOutputStream(bos); dos.write(compressedBytes); dos.close(); return bos.toByteArray(); }
@Override public void onBinaryMessage(WebSocket websocket, byte[] binary) throws IOException, DataFormatException { if (!onBufferMessage(binary)) return; //Thanks to ShadowLordAlpha and Shredder121 for code and debugging. //Get the compressed message and inflate it final int size = readBuffer != null ? readBuffer.size() : binary.length; ByteArrayOutputStream out = new ByteArrayOutputStream(size * 2); try (InflaterOutputStream decompressor = new InflaterOutputStream(out, zlibContext)) { if (readBuffer != null) readBuffer.writeTo(decompressor); else decompressor.write(binary); // send the inflated message to the TextMessage method onTextMessage(websocket, out.toString("UTF-8")); } catch (IOException e) { throw (DataFormatException) new DataFormatException("Malformed").initCause(e); } finally { readBuffer = null; } }
@Test public void addItem() throws IOException { victim.addItem(context.createIndirectReferenceFor(COSInteger.ZERO)); victim.addItem(context.createIndirectReferenceFor(COSInteger.THREE)); victim.prepareForWriting(); assertEquals(COSName.OBJ_STM.getName(), victim.getNameAsString(COSName.TYPE)); assertEquals(2, victim.getInt(COSName.N)); assertEquals(8, victim.getInt(COSName.FIRST)); ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(victim.getFilteredStream(), new InflaterOutputStream(out)); byte[] data = new byte[] { 49, 32, 48, 32, 50, 32, 50, 32, 48, 32, 51, 32 }; assertArrayEquals(data, out.toByteArray()); }
/** * @tests java.util.zip.InflaterOutputStream#InflaterOutputStream(java.io.OutputStream) */ public void test_ConstructorLjava_io_OutputStream() throws IOException { new InflaterOutputStream(os); try { new InflaterOutputStream(null); fail("Should throw NullPointerException"); } catch (NullPointerException e) { // expected } }
/** * @tests java.util.zip.InflaterOutputStream#close() */ public void test_close() throws IOException { InflaterOutputStream ios = new InflaterOutputStream(os); ios.close(); // multiple close ios.close(); }
/** * @tests java.util.zip.InflaterOutputStream#write(int) */ public void test_write_I() throws IOException { int length = compressToBytes(testString); // uncompress the data stored in the compressedBytes InflaterOutputStream ios = new InflaterOutputStream(os); for (int i = 0; i < length; i++) { ios.write(compressedBytes[i]); } String result = new String(os.toByteArray()); assertEquals(testString, result); }
/** * @tests java.util.zip.InflaterOutputStream#write(int) */ public void test_write_I_Illegal() throws IOException { // write after close InflaterOutputStream ios = new InflaterOutputStream(os); ios.close(); try { ios.write(-1); fail("Should throw IOException"); } catch (IOException e) { // expected } }
/** * @tests java.util.zip.InflaterOutputStream#write(byte[],int,int) */ public void test_write_$BII() throws IOException { int length = compressToBytes(testString); // uncompress the data stored in the compressedBytes InflaterOutputStream ios = new InflaterOutputStream(os); ios.write(compressedBytes, 0, length); String result = new String(os.toByteArray()); assertEquals(testString, result); }
@Override public byte[] decompress(final byte[] data) { if (data == null) { return null; } final ByteArrayOutputStream buffer = new ByteArrayOutputStream(data.length); final InflaterOutputStream inflaterStream = new InflaterOutputStream(buffer, new Inflater()); try { inflaterStream.write(data); inflaterStream.close(); buffer.close(); } catch (final IOException e) { throw new RuntimeException("failed compressing using deflate", e); } return buffer.toByteArray(); }
/** * {@inheritDoc} * * @throws IOException */ @Override public byte[] decompress(byte[] compressedData) throws IOException { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { try (InflaterOutputStream ios = new InflaterOutputStream(baos)) { ios.write(compressedData); ios.finish(); return baos.toByteArray(); } } }
private static final byte[] inflate(byte[] data, boolean raw) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); InflaterOutputStream inflaterOut = new InflaterOutputStream(baos, new Inflater(raw)); inflaterOut.write(data); inflaterOut.close(); return baos.toByteArray(); } catch(Exception e) { throw Throwables.propagate(e); } }
public byte[] getDocument(final DocumentInfo metadata) throws IOException { final Map<String, String> pointer = this.index.getFilePosition(metadata); final byte[] buffer = this.readAndDecrypt(pointer.get("f")); final int offset = Integer.parseInt(pointer.get("o")); final int size = Integer.parseInt(pointer.get("s")); final ByteArrayOutputStream result = new ByteArrayOutputStream(); final InflaterOutputStream inflate = new InflaterOutputStream(result); inflate.write(buffer, offset, size); inflate.close(); return result.toByteArray(); }
public InflaterEndableWriteStream(BufferEndableWriteStream delegate) { this.delegate = delegate; this.bufferEndableWriteStreamOutputStream = new BufferEndableWriteStreamOutputStream(delegate); this.inflaterOutputStream = new InflaterOutputStream(bufferEndableWriteStreamOutputStream); }
/** * 读取qq词库文件(qpyd),返回一个包含所以词的list * @param inputPath : qpyd文件的路径 * @return: 包含词库文件中所有词的一个List<String> * @throws Exception */ public static List<String> readQpydFile(String inputPath) throws Exception { List<String> wordList = new ArrayList<String>(); // read qpyd into byte array ByteArrayOutputStream dataOut = new ByteArrayOutputStream(); FileChannel fChannel = new RandomAccessFile(inputPath, "r").getChannel(); fChannel.transferTo(0, fChannel.size(), Channels.newChannel(dataOut)); fChannel.close(); // qpyd as bytes ByteBuffer dataRawBytes = ByteBuffer.wrap(dataOut.toByteArray()); dataRawBytes.order(ByteOrder.LITTLE_ENDIAN); // read info of compressed data int startZippedDictAddr = dataRawBytes.getInt(0x38); int zippedDictLength = dataRawBytes.limit() - startZippedDictAddr; // read zipped qqyd dictionary into byte array dataOut.reset(); Channels.newChannel(new InflaterOutputStream(dataOut)).write( ByteBuffer.wrap(dataRawBytes.array(), startZippedDictAddr, zippedDictLength)); // uncompressed qqyd dictionary as bytes ByteBuffer dataUnzippedBytes = ByteBuffer.wrap(dataOut.toByteArray()); dataUnzippedBytes.order(ByteOrder.LITTLE_ENDIAN); // for debugging: save unzipped data to *.unzipped file Channels.newChannel(new FileOutputStream(inputPath + ".unzipped")).write(dataUnzippedBytes); // stores the start address of actual dictionary data int unzippedDictStartAddr = -1; int idx = 0; byte[] byteArray = dataUnzippedBytes.array(); while (unzippedDictStartAddr == -1 || idx < unzippedDictStartAddr) { // read word int pinyinStartAddr = dataUnzippedBytes.getInt(idx + 0x6); int pinyinLength = dataUnzippedBytes.get(idx + 0x0) & 0xff; int wordStartAddr = pinyinStartAddr + pinyinLength; int wordLength = dataUnzippedBytes.get(idx + 0x1) & 0xff; if (unzippedDictStartAddr == -1) { unzippedDictStartAddr = pinyinStartAddr; } String word = new String(Arrays.copyOfRange(byteArray, wordStartAddr, wordStartAddr + wordLength), "UTF-16LE"); wordList.add(word); // step up idx += 0xa; } return wordList; }