@Override public boolean removeMetadataBlob( UserPublicKey owner, byte[] encodedWritingPublicKey, byte[] writingKeySignedMapKeyPlusBlob) { UserPublicKey writingKey = new UserPublicKey(encodedWritingPublicKey); try { byte[] payload = writingKey.unsignMessage(writingKeySignedMapKeyPlusBlob); byte[] mapKey = Arrays.copyOfRange(payload, 0, 32); byte[] metadataBlob = Arrays.copyOfRange( payload, 32, payload .length); // will be zero length for now // TODO change to the current blob hash MetadataBlob blob = new MetadataBlob(writingKey.getPublicKeys(), mapKey, metadataBlob); return blob.delete(); } catch (TweetNaCl.InvalidSignatureException e) { System.err.println( "Invalid signature during removeMetadataBlob for owner: " + owner + " and sharer: " + writingKey); return false; } }
static double[] similarity(LatentDirichletAllocation lda0, LatentDirichletAllocation lda1) { int numTopics = lda0.numTopics(); int numPairs = numTopics * (numTopics - 1); @SuppressWarnings({"unchecked", "rawtypes"}) // ok given use w. erasure ScoredObject<int[]>[] pairs = (ScoredObject<int[]>[]) new ScoredObject[numPairs]; int pos = 0; for (int i = 0; i < numTopics; ++i) { for (int j = 0; j < numTopics; ++j) { if (i == j) continue; double divergence = Statistics.symmetrizedKlDivergence( lda0.wordProbabilities(i), lda1.wordProbabilities(j)); pairs[pos++] = new ScoredObject<int[]>(new int[] {i, j}, divergence); } } Arrays.sort(pairs, ScoredObject.comparator()); boolean[] taken0 = new boolean[numTopics]; Arrays.fill(taken0, false); boolean[] taken1 = new boolean[numTopics]; Arrays.fill(taken1, false); double[] scores = new double[numTopics]; int scorePos = 0; for (pos = 0; pos < numPairs && scorePos < numTopics; ++pos) { int[] pair = pairs[pos].getObject(); if (!taken0[pair[0]] && !taken1[pair[1]]) { taken0[pair[0]] = true; taken1[pair[1]] = true; scores[scorePos++] = pairs[pos].score(); } } return scores; }
public Iterable<ZipEntry> getEntriesInPhysicalOrder(final String name) { ZipEntry[] entriesOfThatName = new ZipEntry[0]; if (this.nameMap.containsKey(name)) { entriesOfThatName = this.nameMap.get(name).toArray(entriesOfThatName); Arrays.sort(entriesOfThatName, this.OFFSET_COMPARATOR); } return Arrays.asList(entriesOfThatName); }
// pretty print Matrix(2D array of doubles) public static String pprint(double[][] arr,DecimalFormat dformat) { int colDim = 0; for( double[] line : arr ) colDim = Math.max(colDim, line.length); StringBuilder sb = new StringBuilder(); int max_width = 0; int[] ilengths = new int[colDim]; Arrays.fill(ilengths, -1); for( double[] line : arr ) { for( int c = 0; c < line.length; ++c ) { double d = line[c]; String dStr = dformat.format(d); if( dStr.indexOf('.') == -1 ) dStr += ".0"; ilengths[c] = Math.max(ilengths[c], dStr.indexOf('.')); int prefix = (d >= 0 ? 1 : 2); max_width = Math.max(dStr.length() + prefix, max_width); } } for( double[] line : arr ) { for( int c = 0; c < line.length; ++c ) { double d = line[c]; String dStr = dformat.format(d); if( dStr.indexOf('.') == -1 ) dStr += ".0"; for( int x = dStr.indexOf('.'); x < ilengths[c] + 1; ++x ) sb.append(' '); sb.append(dStr); if( dStr.indexOf('.') == -1 ) sb.append('.'); for( int i = dStr.length() - Math.max(0, dStr.indexOf('.')); i <= 5; ++i ) sb.append('0'); } sb.append("\n"); } return sb.toString(); }
static private List getCommandCompilerCPP(String avrBasePath, List includePaths, String sourceName, String objectName, Map<String, String> boardPreferences) { List baseCommandCompilerCPP = new ArrayList(Arrays.asList(new String[] { avrBasePath + "avr-g++", "-c", // compile, don't link "-g", // include debugging info (so errors include line numbers) "-Os", // optimize for size Preferences.getBoolean("build.verbose") ? "-Wall" : "-w", // show warnings if verbose "-fno-exceptions", "-ffunction-sections", // place each function in its own section "-fdata-sections", "-mmcu=" + boardPreferences.get("build.mcu"), "-DF_CPU=" + boardPreferences.get("build.f_cpu"), "-MMD", // output dependancy info "-DUSB_VID=" + boardPreferences.get("build.vid"), "-DUSB_PID=" + boardPreferences.get("build.pid"), "-DARDUINO=" + Base.REVISION, })); for (int i = 0; i < includePaths.size(); i++) { baseCommandCompilerCPP.add("-I" + (String) includePaths.get(i)); } baseCommandCompilerCPP.add(sourceName); baseCommandCompilerCPP.add("-o"); baseCommandCompilerCPP.add(objectName); return baseCommandCompilerCPP; }
static public int[] difference(int a[], int b[]) { int[] r = new int[a.length]; int cnt = 0; for (int i=0; i<a.length; i++) { if (!contains(b, a[i])) r[cnt++] = a[i]; } return Arrays.copyOf(r, cnt); }
@Override public boolean setMetadataBlob( UserPublicKey owner, byte[] encodedWritingPublicKey, byte[] writingKeySignedMapKey) { UserPublicKey writingKey = new UserPublicKey(encodedWritingPublicKey); try { byte[] payload = writingKey.unsignMessage(writingKeySignedMapKey); byte[] mapKey = Arrays.copyOfRange(payload, 0, 32); byte[] metadataBlob = Arrays.copyOfRange(payload, 32, payload.length); MetadataBlob blob = new MetadataBlob(writingKey.getPublicKeys(), mapKey, metadataBlob); return blob.insert(); } catch (TweetNaCl.InvalidSignatureException e) { System.err.println( "Invalid signature during setMetadataBlob for owner: " + owner + " and sharer: " + writingKey); return false; } }
private void positionAtCentralDirectory64() throws IOException { this.skipBytes(4); this.archive.readFully(this.DWORD_BUF); this.archive.seek(ZipEightByteInteger.getLongValue(this.DWORD_BUF)); this.archive.readFully(this.WORD_BUF); if (!Arrays.equals(this.WORD_BUF, ZipOutputStream.ZIP64_EOCD_SIG)) { throw new ZipException("archive's ZIP64 end of central directory locator is corrupt."); } this.skipBytes(44); this.archive.readFully(this.DWORD_BUF); this.archive.seek(ZipEightByteInteger.getLongValue(this.DWORD_BUF)); }
/** * Returns compact class host. * * @param obj Object to compact. * @return String. */ @Nullable public static Object compactObject(Object obj) { if (obj == null) return null; if (obj instanceof Enum) return obj.toString(); if (obj instanceof String || obj instanceof Boolean || obj instanceof Number) return obj; if (obj instanceof Collection) { Collection col = (Collection) obj; Object[] res = new Object[col.size()]; int i = 0; for (Object elm : col) res[i++] = compactObject(elm); return res; } if (obj.getClass().isArray()) { Class<?> arrType = obj.getClass().getComponentType(); if (arrType.isPrimitive()) { if (obj instanceof boolean[]) return Arrays.toString((boolean[]) obj); if (obj instanceof byte[]) return Arrays.toString((byte[]) obj); if (obj instanceof short[]) return Arrays.toString((short[]) obj); if (obj instanceof int[]) return Arrays.toString((int[]) obj); if (obj instanceof long[]) return Arrays.toString((long[]) obj); if (obj instanceof float[]) return Arrays.toString((float[]) obj); if (obj instanceof double[]) return Arrays.toString((double[]) obj); } Object[] arr = (Object[]) obj; int iMax = arr.length - 1; StringBuilder sb = new StringBuilder("["); for (int i = 0; i <= iMax; i++) { sb.append(compactObject(arr[i])); if (i != iMax) sb.append(", "); } sb.append("]"); return sb.toString(); } return U.compact(obj.getClass().getName()); }
public static byte [] unzipBytes(byte [] bs, Compression cmp) { InputStream is = null; int off = 0; try { switch(cmp) { case NONE: // No compression return bs; case ZIP: { ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(bs)); ZipEntry ze = zis.getNextEntry(); // Get the *FIRST* entry // There is at least one entry in zip file and it is not a directory. if( ze != null && !ze.isDirectory() ) { is = zis; break; } zis.close(); return bs; // Don't crash, ignore file if cannot unzip } case GZIP: is = new GZIPInputStream(new ByteArrayInputStream(bs)); break; default: assert false:"cmp = " + cmp; } // If reading from a compressed stream, estimate we can read 2x uncompressed assert( is != null ):"is is NULL, cmp = " + cmp; bs = new byte[bs.length * 2]; // Now read from the (possibly compressed) stream while( off < bs.length ) { int len = is.read(bs, off, bs.length - off); if( len < 0 ) break; off += len; if( off == bs.length ) { // Dataset is uncompressing alot! Need more space... if( bs.length >= ValueArray.CHUNK_SZ ) break; // Already got enough bs = Arrays.copyOf(bs, bs.length * 2); } } } catch( IOException ioe ) { // Stop at any io error Log.err(ioe); } finally { Utils.close(is); } return bs; }
/** * Concat arrays in one. * * @param arrays Arrays. * @return Summary array. */ public static int[] concat(int[]... arrays) { assert arrays != null; assert arrays.length > 1; int len = 0; for (int[] a : arrays) len += a.length; int[] r = Arrays.copyOf(arrays[0], len); for (int i = 1, shift = 0; i < arrays.length; i++) { shift += arrays[i - 1].length; System.arraycopy(arrays[i], 0, r, shift, arrays[i].length); } return r; }
private void positionAtCentralDirectory() throws IOException { this.positionAtEndOfCentralDirectoryRecord(); boolean found = false; final boolean searchedForZip64EOCD = this.archive.getFilePointer() > 20L; if (searchedForZip64EOCD) { this.archive.seek(this.archive.getFilePointer() - 20L); this.archive.readFully(this.WORD_BUF); found = Arrays.equals(ZipOutputStream.ZIP64_EOCD_LOC_SIG, this.WORD_BUF); } if (!found) { if (searchedForZip64EOCD) { this.skipBytes(16); } this.positionAtCentralDirectory32(); } else { this.positionAtCentralDirectory64(); } }
public static String sampleToString(int[] val, int max) { if (val == null || val.length < max) return Arrays.toString(val); StringBuilder b = new StringBuilder(); b.append('['); max -= 10; int valMax = val.length -1; for (int i = 0; ; i++) { b.append(val[i]); if (i == max) { b.append(", ..."); i = val.length - 10; } if ( i == valMax) { return b.append(']').toString(); } b.append(", "); } }
public void actionPerformed(ActionEvent e) { if (!this.isEnabled()) { return; } if (NEW_AIRSPACE.equals(e.getActionCommand())) { this.createNewEntry(this.getView().getSelectedFactory()); } else if (CLEAR_SELECTION.equals(e.getActionCommand())) { this.selectEntry(null, true); } else if (SIZE_NEW_SHAPES_TO_VIEWPORT.equals(e.getActionCommand())) { if (e.getSource() instanceof AbstractButton) { boolean selected = ((AbstractButton) e.getSource()).isSelected(); this.setResizeNewShapesToViewport(selected); } } else if (ENABLE_EDIT.equals(e.getActionCommand())) { if (e.getSource() instanceof AbstractButton) { boolean selected = ((AbstractButton) e.getSource()).isSelected(); this.setEnableEdit(selected); } } else if (OPEN.equals(e.getActionCommand())) { this.openFromFile(); } else if (OPEN_URL.equals(e.getActionCommand())) { this.openFromURL(); } else if (OPEN_DEMO_AIRSPACES.equals(e.getActionCommand())) { this.openFromPath(DEMO_AIRSPACES_PATH); this.zoomTo( LatLon.fromDegrees(47.6584074779224, -122.3059199579634), Angle.fromDegrees(-152), Angle.fromDegrees(75), 750); } else if (REMOVE_SELECTED.equals(e.getActionCommand())) { this.removeEntries(Arrays.asList(this.getSelectedEntries())); } else if (SAVE.equals(e.getActionCommand())) { this.saveToFile(); } else if (SELECTION_CHANGED.equals(e.getActionCommand())) { this.viewSelectionChanged(); } }
/** * Run command in separated console. * * @param workFolder Work folder for command. * @param args A string array containing the program and its arguments. * @return Started process. * @throws IOException If failed to start process. */ public static Process openInConsole(@Nullable File workFolder, String... args) throws IOException { String[] commands = args; String cmd = F.concat(Arrays.asList(args), " "); if (U.isWindows()) commands = F.asArray("cmd", "/c", String.format("start %s", cmd)); if (U.isMacOs()) commands = F.asArray( "osascript", "-e", String.format("tell application \"Terminal\" to do script \"%s\"", cmd)); if (U.isUnix()) commands = F.asArray("xterm", "-sl", "1024", "-geometry", "200x50", "-e", cmd); ProcessBuilder pb = new ProcessBuilder(commands); if (workFolder != null) pb.directory(workFolder); return pb.start(); }
static private List getCommandCompilerS(String avrBasePath, List includePaths, String sourceName, String objectName, Map<String, String> boardPreferences) { List baseCommandCompiler = new ArrayList(Arrays.asList(new String[] { avrBasePath + "avr-gcc", "-c", // compile, don't link "-g", // include debugging info (so errors include line numbers) "-assembler-with-cpp", "-mmcu=" + boardPreferences.get("build.mcu"), "-DF_CPU=" + boardPreferences.get("build.f_cpu"), "-DARDUINO=" + Base.REVISION, "-DUSB_VID=" + boardPreferences.get("build.vid"), "-DUSB_PID=" + boardPreferences.get("build.pid"), })); for (int i = 0; i < includePaths.size(); i++) { baseCommandCompiler.add("-I" + (String) includePaths.get(i)); } baseCommandCompiler.add(sourceName); baseCommandCompiler.add("-o"+ objectName); return baseCommandCompiler; }
/** * Compile with avr-gcc. * * @param sketch Sketch object to be compiled. * @param buildPath Where the temporary files live and will be built from. * @param primaryClassName the name of the combined sketch file w/ extension * @return true if successful. * @throws RunnerException Only if there's a problem. Only then. * * [ROBOTIS]Changed prototype to support ARM Cortex-M3 based CM-900 Pandora project * 2012-09-26 [email protected] * */ public boolean compile(Sketch sketch, //change return type[ROBOTIS] String buildPath, String primaryClassName, boolean verbose, List<String> ignored) throws RunnerException { this.sketch = sketch; this.buildPath = buildPath; this.primaryClassName = primaryClassName; //예를 들면 cpp파일로 변환된 AnalogReadSerial.cpp this.verbose = verbose; this.sketchIsCompiled = false; System.out.println("Compiler.compile() sketch ="+sketch.getName()+"buildpath ="+buildPath+"primaryClassName ="+primaryClassName); // the pms object isn't used for anything but storage MessageStream pms = new MessageStream(this); String avrBasePath = Base.getAvrBasePath(); System.out.println("[ROBOTIS]avrBasePath ="+avrBasePath); Map<String, String> boardPreferences = Base.getBoardPreferences(); String core = boardPreferences.get("build.core"); System.out.println("[ROBOTIS]build.core ="+core); if (core == null) { RunnerException re = new RunnerException(_("No board selected; please choose a board from the Tools > Board menu.")); re.hideStackTrace(); throw re; } String corePath; if (core.indexOf(':') == -1) { Target t = Base.getTarget(); File coreFolder = new File(new File(t.getFolder(), "cores"), core); corePath = coreFolder.getAbsolutePath(); } else { Target t = Base.targetsTable.get(core.substring(0, core.indexOf(':'))); File coreFolder = new File(t.getFolder(), "cores"); coreFolder = new File(coreFolder, core.substring(core.indexOf(':') + 1)); corePath = coreFolder.getAbsolutePath(); } System.out.println("[ROBOTIS]corePath ="+corePath); String variant = boardPreferences.get("build.variant"); String variantPath = null; if (variant != null) { if (variant.indexOf(':') == -1) { Target t = Base.getTarget(); File variantFolder = new File(new File(t.getFolder(), "variants"), variant); variantPath = variantFolder.getAbsolutePath(); } else { Target t = Base.targetsTable.get(variant.substring(0, variant.indexOf(':'))); File variantFolder = new File(t.getFolder(), "variants"); variantFolder = new File(variantFolder, variant.substring(variant.indexOf(':') + 1)); variantPath = variantFolder.getAbsolutePath(); } } List<File> objectFiles = new ArrayList<File>(); // 0. include paths for core + all libraries sketch.setCompilingProgress(20); List includePaths = new ArrayList(); includePaths.add(corePath); if (variantPath != null) includePaths.add(variantPath); for (File file : sketch.getImportedLibraries()) { includePaths.add(file.getPath()); } // 1. compile the sketch (already in the buildPath) sketch.setCompilingProgress(30); objectFiles.addAll( compileFiles(avrBasePath, buildPath, includePaths, findFilesInPath(buildPath, "S", false), findFilesInPath(buildPath, "c", false), findFilesInPath(buildPath, "cpp", false), boardPreferences)); sketchIsCompiled = true; // 2. compile the libraries, outputting .o files to: <buildPath>/<library>/ sketch.setCompilingProgress(40); for (File libraryFolder : sketch.getImportedLibraries()) { File outputFolder = new File(buildPath, libraryFolder.getName()); File utilityFolder = new File(libraryFolder, "utility"); createFolder(outputFolder); // this library can use includes in its utility/ folder includePaths.add(utilityFolder.getAbsolutePath()); objectFiles.addAll( compileFiles(avrBasePath, outputFolder.getAbsolutePath(), includePaths, findFilesInFolder(libraryFolder, "S", false), findFilesInFolder(libraryFolder, "c", false), findFilesInFolder(libraryFolder, "cpp", false), boardPreferences)); outputFolder = new File(outputFolder, "utility"); createFolder(outputFolder); objectFiles.addAll( compileFiles(avrBasePath, outputFolder.getAbsolutePath(), includePaths, findFilesInFolder(utilityFolder, "S", false), findFilesInFolder(utilityFolder, "c", false), findFilesInFolder(utilityFolder, "cpp", false), boardPreferences)); // other libraries should not see this library's utility/ folder includePaths.remove(includePaths.size() - 1); } // 3. compile the core, outputting .o files to <buildPath> and then // collecting them into the core.a library file. sketch.setCompilingProgress(50); includePaths.clear(); includePaths.add(corePath); // include path for core only if (variantPath != null) includePaths.add(variantPath); List<File> coreObjectFiles = compileFiles(avrBasePath, buildPath, includePaths, findFilesInPath(corePath, "S", true), findFilesInPath(corePath, "c", true), findFilesInPath(corePath, "cpp", true), boardPreferences); String runtimeLibraryName = buildPath + File.separator + "core.a"; List baseCommandAR = new ArrayList(Arrays.asList(new String[] { avrBasePath + "avr-ar", "rcs", runtimeLibraryName })); for(File file : coreObjectFiles) { List commandAR = new ArrayList(baseCommandAR); commandAR.add(file.getAbsolutePath()); execAsynchronously(commandAR); } // 4. link it all together into the .elf file // For atmega2560, need --relax linker option to link larger // programs correctly. String optRelax = ""; String atmega2560 = new String ("atmega2560"); if ( atmega2560.equals(boardPreferences.get("build.mcu")) ) { optRelax = new String(",--relax"); } sketch.setCompilingProgress(60); List baseCommandLinker = new ArrayList(Arrays.asList(new String[] { avrBasePath + "avr-gcc", "-Os", "-Wl,--gc-sections"+optRelax, "-mmcu=" + boardPreferences.get("build.mcu"), "-o", buildPath + File.separator + primaryClassName + ".elf" })); for (File file : objectFiles) { baseCommandLinker.add(file.getAbsolutePath()); } baseCommandLinker.add(runtimeLibraryName); baseCommandLinker.add("-L" + buildPath); baseCommandLinker.add("-lm"); execAsynchronously(baseCommandLinker); List baseCommandObjcopy = new ArrayList(Arrays.asList(new String[] { avrBasePath + "avr-objcopy", "-O", "-R", })); List commandObjcopy; // 5. extract EEPROM data (from EEMEM directive) to .eep file. sketch.setCompilingProgress(70); commandObjcopy = new ArrayList(baseCommandObjcopy); commandObjcopy.add(2, "ihex"); commandObjcopy.set(3, "-j"); commandObjcopy.add(".eeprom"); commandObjcopy.add("--set-section-flags=.eeprom=alloc,load"); commandObjcopy.add("--no-change-warnings"); commandObjcopy.add("--change-section-lma"); commandObjcopy.add(".eeprom=0"); commandObjcopy.add(buildPath + File.separator + primaryClassName + ".elf"); commandObjcopy.add(buildPath + File.separator + primaryClassName + ".eep"); execAsynchronously(commandObjcopy); // 6. build the .hex file sketch.setCompilingProgress(80); commandObjcopy = new ArrayList(baseCommandObjcopy); commandObjcopy.add(2, "ihex"); commandObjcopy.add(".eeprom"); // remove eeprom data commandObjcopy.add(buildPath + File.separator + primaryClassName + ".elf"); commandObjcopy.add(buildPath + File.separator + primaryClassName + ".hex"); execAsynchronously(commandObjcopy); sketch.setCompilingProgress(90); return true; }
/** * Implementations of this abstract class can add compression of a particular type to a given {@link * OutputStream}. They each return a {@link CompressingOutputStream}, which is just a thin wrapper * on top of an {@link OutputStream} that adds the ability to "finish" a stream (see {@link * CompressingOutputStream}). * * <p> * * <p>This class contains implementations based on several popular compression algorithms, such as * gzip. For example, the gzip implementation can decorate an {@link OutputStream} using an instance * of {@link GZIPOutputStream} and in that way add gzip compression to the stream. * * @author Sean Owen */ abstract class CompressingStreamFactory { /** "No encoding" content type: "identity". */ static final String NO_ENCODING = "identity"; private static final Logger LOG = LoggerFactory.getLogger(CompressingStreamFactory.class); /** Implementation based on {@link GZIPOutputStream} and {@link GZIPInputStream}. */ private static final CompressingStreamFactory GZIP_CSF = new GZIPCompressingStreamFactory(); /** Implementation based on {@link ZipOutputStream} and {@link ZipInputStream}. */ private static final CompressingStreamFactory ZIP_CSF = new ZipCompressingStreamFactory(); /** Implementation based on {@link DeflaterOutputStream}. */ private static final CompressingStreamFactory DEFLATE_CSF = new DeflateCompressingStreamFactory(); private static final String GZIP_ENCODING = "gzip"; private static final String X_GZIP_ENCODING = "x-gzip"; private static final String DEFLATE_ENCODING = "deflate"; private static final String COMPRESS_ENCODING = "compress"; private static final String X_COMPRESS_ENCODING = "x-compress"; static final List<String> ALL_COMPRESSION_ENCODINGS = Collections.unmodifiableList( Arrays.asList( GZIP_ENCODING, DEFLATE_ENCODING, COMPRESS_ENCODING, X_GZIP_ENCODING, X_COMPRESS_ENCODING)); /** "Any encoding" content type: the "*" wildcard. */ private static final String ANY_ENCODING = "*"; /** Ordered list of preferred encodings, from most to least preferred */ private static final List<String> SUPPORTED_ENCODINGS; /** * Cache mapping previously seen "Accept-Encoding" header Strings to an appropriate instance of * {@link CompressingStreamFactory}. */ private static final Map<String, String> BEST_ENCODING_CACHE = Collections.synchronizedMap(new HashMap<String, String>(101)); /** Maps content type String to appropriate implementation of {@link CompressingStreamFactory}. */ private static final Map<String, CompressingStreamFactory> FACTORY_MAP; private static final Pattern COMMA = Pattern.compile(","); static { List<String> temp = new ArrayList<>(6); temp.add(GZIP_ENCODING); temp.add(DEFLATE_ENCODING); temp.add(COMPRESS_ENCODING); temp.add(X_GZIP_ENCODING); temp.add(X_COMPRESS_ENCODING); temp.add(NO_ENCODING); SUPPORTED_ENCODINGS = Collections.unmodifiableList(temp); } static { Map<String, CompressingStreamFactory> temp = new HashMap<>(11); temp.put(GZIP_ENCODING, GZIP_CSF); temp.put(X_GZIP_ENCODING, GZIP_CSF); temp.put(COMPRESS_ENCODING, ZIP_CSF); temp.put(X_COMPRESS_ENCODING, ZIP_CSF); temp.put(DEFLATE_ENCODING, DEFLATE_CSF); FACTORY_MAP = Collections.unmodifiableMap(temp); } private static OutputStream maybeWrapStatsOutputStream( OutputStream outputStream, CompressingFilterContext context, CompressingFilterStats.StatsField field) { assert outputStream != null; OutputStream result; if (context.isStatsEnabled()) { CompressingFilterStats stats = context.getStats(); CompressingFilterStats.OutputStatsCallback callbackOutput = stats.getOutputStatsCallback(field); result = new StatsOutputStream(outputStream, callbackOutput); } else { result = outputStream; } return result; } private static InputStream maybeWrapStatsInputStream( InputStream inputStream, CompressingFilterContext context, CompressingFilterStats.StatsField field) { assert inputStream != null; InputStream result; if (context.isStatsEnabled()) { CompressingFilterStats stats = context.getStats(); CompressingFilterStats.InputStatsCallback callbackInput = stats.getInputStatsCallback(field); result = new StatsInputStream(inputStream, callbackInput); } else { result = inputStream; } return result; } private static boolean isSupportedResponseContentEncoding(String contentEncoding) { return NO_ENCODING.equals(contentEncoding) || FACTORY_MAP.containsKey(contentEncoding); } static boolean isSupportedRequestContentEncoding(String contentEncoding) { return NO_ENCODING.equals(contentEncoding) || FACTORY_MAP.containsKey(contentEncoding); } /** * Returns the instance associated to the given content encoding. * * @param contentEncoding content encoding (e.g. "gzip") * @return instance for content encoding */ static CompressingStreamFactory getFactoryForContentEncoding(String contentEncoding) { assert FACTORY_MAP.containsKey(contentEncoding); return FACTORY_MAP.get(contentEncoding); } /** * Determines best content encoding for the response, based on the request -- in particular, based * on its "Accept-Encoding" header. * * @param httpRequest request * @return best content encoding */ static String getBestContentEncoding(HttpServletRequest httpRequest) { String forcedEncoding = (String) httpRequest.getAttribute(CompressingFilter.FORCE_ENCODING_KEY); String bestEncoding; if (forcedEncoding != null) { bestEncoding = forcedEncoding; } else { String acceptEncodingHeader = httpRequest.getHeader(CompressingHttpServletResponse.ACCEPT_ENCODING_HEADER); if (acceptEncodingHeader == null) { bestEncoding = NO_ENCODING; } else { bestEncoding = BEST_ENCODING_CACHE.get(acceptEncodingHeader); if (bestEncoding == null) { // No cached value; must parse header to determine best encoding // I don't synchronize on bestEncodingCache; it's not worth it to avoid the rare case // where // two thread get in here and both parse the header. It's only a tiny bit of extra work, // and // avoids the synchronization overhead. if (acceptEncodingHeader.indexOf((int) ',') >= 0) { // multiple encodings are accepted bestEncoding = selectBestEncoding(acceptEncodingHeader); } else { // one encoding is accepted bestEncoding = parseBestEncoding(acceptEncodingHeader); } BEST_ENCODING_CACHE.put(acceptEncodingHeader, bestEncoding); } } } // User-specified encoding might not be supported if (!isSupportedResponseContentEncoding(bestEncoding)) { bestEncoding = NO_ENCODING; } return bestEncoding; } private static String parseBestEncoding(String acceptEncodingHeader) { ContentEncodingQ contentEncodingQ = parseContentEncodingQ(acceptEncodingHeader); String contentEncoding = contentEncodingQ.getContentEncoding(); if (contentEncodingQ.getQ() > 0.0) { if (ANY_ENCODING.equals(contentEncoding)) { return SUPPORTED_ENCODINGS.get(0); } else if (SUPPORTED_ENCODINGS.contains(contentEncoding)) { return contentEncoding; } } return NO_ENCODING; } @SuppressWarnings("squid:S1244") private static String selectBestEncoding(String acceptEncodingHeader) { // multiple encodings are accepted; determine best one Collection<String> bestEncodings = new HashSet<>(3); double bestQ = 0.0; Collection<String> unacceptableEncodings = new HashSet<>(3); boolean willAcceptAnything = false; for (String token : COMMA.split(acceptEncodingHeader)) { ContentEncodingQ contentEncodingQ = parseContentEncodingQ(token); String contentEncoding = contentEncodingQ.getContentEncoding(); double q = contentEncodingQ.getQ(); if (ANY_ENCODING.equals(contentEncoding)) { willAcceptAnything = q > 0.0; } else if (SUPPORTED_ENCODINGS.contains(contentEncoding)) { if (q > 0.0) { // This is a header quality comparison. // So it is safe to suppress warning squid:S1244 if (q == bestQ) { bestEncodings.add(contentEncoding); } else if (q > bestQ) { bestQ = q; bestEncodings.clear(); bestEncodings.add(contentEncoding); } } else { unacceptableEncodings.add(contentEncoding); } } } if (bestEncodings.isEmpty()) { // nothing was acceptable to us if (willAcceptAnything) { if (unacceptableEncodings.isEmpty()) { return SUPPORTED_ENCODINGS.get(0); } else { for (String encoding : SUPPORTED_ENCODINGS) { if (!unacceptableEncodings.contains(encoding)) { return encoding; } } } } } else { for (String encoding : SUPPORTED_ENCODINGS) { if (bestEncodings.contains(encoding)) { return encoding; } } } return NO_ENCODING; } private static ContentEncodingQ parseContentEncodingQ(String contentEncodingString) { double q = 1.0; int qvalueStartIndex = contentEncodingString.indexOf((int) ';'); String contentEncoding; if (qvalueStartIndex >= 0) { contentEncoding = contentEncodingString.substring(0, qvalueStartIndex).trim(); String qvalueString = contentEncodingString.substring(qvalueStartIndex + 1).trim(); if (qvalueString.startsWith("q=")) { try { q = Double.parseDouble(qvalueString.substring(2)); } catch (NumberFormatException ignored) { // That's bad -- browser sent an invalid number. All we can do is ignore it, and // pretend that no q value was specified, so that it effectively defaults to 1.0 LOG.trace("Couldn't parse a Double from {}.", qvalueString.substring(2), ignored); } } } else { contentEncoding = contentEncodingString.trim(); } return new ContentEncodingQ(contentEncoding, q); } abstract CompressingOutputStream getCompressingStream( OutputStream servletOutputStream, CompressingFilterContext context) throws IOException; abstract CompressingInputStream getCompressingStream( InputStream servletInputStream, CompressingFilterContext context) throws IOException; private static final class ContentEncodingQ { private final String contentEncoding; private final double q; private ContentEncodingQ(String contentEncoding, double q) { assert contentEncoding != null && contentEncoding.length() > 0; this.contentEncoding = contentEncoding; this.q = q; } String getContentEncoding() { return contentEncoding; } double getQ() { return q; } @Override public String toString() { return contentEncoding + ";q=" + q; } } private static class GZIPCompressingStreamFactory extends CompressingStreamFactory { @Override CompressingOutputStream getCompressingStream( final OutputStream outputStream, final CompressingFilterContext context) throws IOException { return new CompressingOutputStream() { private final DeflaterOutputStream gzipOutputStream = new GZIPOutputStream( CompressingStreamFactory.maybeWrapStatsOutputStream( outputStream, context, CompressingFilterStats.StatsField.RESPONSE_COMPRESSED_BYTES)); private final OutputStream statsOutputStream = CompressingStreamFactory.maybeWrapStatsOutputStream( gzipOutputStream, context, CompressingFilterStats.StatsField.RESPONSE_INPUT_BYTES); @Override public OutputStream getCompressingOutputStream() { return statsOutputStream; } @Override public void finish() throws IOException { gzipOutputStream.finish(); } }; } @Override CompressingInputStream getCompressingStream( final InputStream inputStream, final CompressingFilterContext context) { return new CompressingInputStream() { @Override public InputStream getCompressingInputStream() throws IOException { return CompressingStreamFactory.maybeWrapStatsInputStream( new GZIPInputStream( CompressingStreamFactory.maybeWrapStatsInputStream( inputStream, context, CompressingFilterStats.StatsField.REQUEST_COMPRESSED_BYTES)), context, CompressingFilterStats.StatsField.REQUEST_INPUT_BYTES); } }; } } private static class ZipCompressingStreamFactory extends CompressingStreamFactory { @Override CompressingOutputStream getCompressingStream( final OutputStream outputStream, final CompressingFilterContext context) { return new CompressingOutputStream() { private final DeflaterOutputStream zipOutputStream = new ZipOutputStream( CompressingStreamFactory.maybeWrapStatsOutputStream( outputStream, context, CompressingFilterStats.StatsField.RESPONSE_COMPRESSED_BYTES)); private final OutputStream statsOutputStream = CompressingStreamFactory.maybeWrapStatsOutputStream( zipOutputStream, context, CompressingFilterStats.StatsField.RESPONSE_INPUT_BYTES); @Override public OutputStream getCompressingOutputStream() { return statsOutputStream; } @Override public void finish() throws IOException { zipOutputStream.finish(); } }; } @Override CompressingInputStream getCompressingStream( final InputStream inputStream, final CompressingFilterContext context) { return new CompressingInputStream() { @Override public InputStream getCompressingInputStream() { return CompressingStreamFactory.maybeWrapStatsInputStream( new ZipInputStream( CompressingStreamFactory.maybeWrapStatsInputStream( inputStream, context, CompressingFilterStats.StatsField.REQUEST_COMPRESSED_BYTES)), context, CompressingFilterStats.StatsField.REQUEST_INPUT_BYTES); } }; } } private static class DeflateCompressingStreamFactory extends CompressingStreamFactory { @Override CompressingOutputStream getCompressingStream( final OutputStream outputStream, final CompressingFilterContext context) { return new CompressingOutputStream() { private final DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream( CompressingStreamFactory.maybeWrapStatsOutputStream( outputStream, context, CompressingFilterStats.StatsField.RESPONSE_COMPRESSED_BYTES)); private final OutputStream statsOutputStream = CompressingStreamFactory.maybeWrapStatsOutputStream( deflaterOutputStream, context, CompressingFilterStats.StatsField.RESPONSE_INPUT_BYTES); @Override public OutputStream getCompressingOutputStream() { return statsOutputStream; } @Override public void finish() throws IOException { deflaterOutputStream.finish(); } }; } @Override CompressingInputStream getCompressingStream( final InputStream inputStream, final CompressingFilterContext context) { return new CompressingInputStream() { @Override public InputStream getCompressingInputStream() { return CompressingStreamFactory.maybeWrapStatsInputStream( new InflaterInputStream( CompressingStreamFactory.maybeWrapStatsInputStream( inputStream, context, CompressingFilterStats.StatsField.REQUEST_COMPRESSED_BYTES)), context, CompressingFilterStats.StatsField.REQUEST_INPUT_BYTES); } }; } } }
private boolean startsWithLocalFileHeader() throws IOException { this.archive.seek(0L); this.archive.readFully(this.WORD_BUF); return Arrays.equals(this.WORD_BUF, ZipOutputStream.LFH_SIG); }
public static String join(char sep, Object[] array) { return join(sep, Arrays.asList(array)); }
public static float[] div(float[] nums, float n) { assert !Float.isInfinite(n) : "Trying to divide " + Arrays.toString(nums) + " by " + n; // Almost surely not what you want for (int i=0; i<nums.length; i++) nums[i] = nums[i] / n; return nums; }
public Enumeration<ZipEntry> getEntriesInPhysicalOrder() { final ZipEntry[] allEntries = this.entries.toArray(new ZipEntry[0]); Arrays.sort(allEntries, this.OFFSET_COMPARATOR); return Collections.enumeration(Arrays.asList(allEntries)); }
public static double[] append(double[] a, double e) { a = Arrays.copyOf(a,a.length+1); a[a.length-1] = e; return a; }
public static long[][][] append(long[][][] a, long[][] e) { a = Arrays.copyOf(a,a.length+1); a[a.length-1] = e; return a; }
public static <T> T[] append(T[] a, T... b) { if( a==null ) return b; T[] tmp = Arrays.copyOf(a,a.length+b.length); System.arraycopy(b,0,tmp,a.length,b.length); return tmp; }
public static <T> T[] subarray(T[] a, int off, int len) { return Arrays.copyOfRange(a,off,off+len); }
public static int[] remove(int[] a, int i) { int[] tmp = Arrays.copyOf(a,a.length-1); System.arraycopy(a,i+1,tmp,i,tmp.length-i); return tmp; }