Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/demo/zipfs/ReleaseDeflater.java
38833 views
/*1* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/*25* @test26* @bug 823401127* @summary Check that jdk.nio.zipfs.ZipFileSystem doesn't cache more than ZipFileSystem.MAX_FLATER Inflater/Deflater objects28* @run main ReleaseDeflater29* @author Volker Simonis30*/3132import java.io.InputStream;33import java.io.OutputStream;34import java.lang.reflect.Field;35import java.net.URI;36import java.nio.file.FileSystem;37import java.nio.file.FileSystems;38import java.nio.file.Files;39import java.nio.file.Path;40import java.nio.file.Paths;41import java.nio.file.spi.FileSystemProvider;42import java.util.List;43import java.util.Map;44import java.util.ArrayList;45import java.util.HashMap;4647public class ReleaseDeflater {48public static void main(String[] args) throws Throwable {49Path zipFile = Paths.get("ReleaseDeflaterTest.zip").toAbsolutePath();50URI zipURI = URI.create("jar:" + zipFile.toUri());51Map<String, String> env = new HashMap<>();52env.put("create", "true");53try (FileSystem fs = FileSystems.newFileSystem(zipURI, env)) {54FileSystemProvider zprov = fs.provider();55Path test = fs.getPath("test.txt");56int STREAMS = 100;57List<OutputStream> ostreams = new ArrayList<>(STREAMS);58List<InputStream> istreams = new ArrayList<>(STREAMS);59for (int i = 0; i < STREAMS; i++) {60OutputStream zos = zprov.newOutputStream(test);61ostreams.add(zos);62zos.write("Hello".getBytes());63}64for (OutputStream os : ostreams) {65os.close();66}67for (int i = 0; i < STREAMS; i++) {68InputStream zis = zprov.newInputStream(test);69istreams.add(zis);70}71for (InputStream is : istreams) {72is.close();73}74try {75Field max_flaters = fs.getClass().getDeclaredField("MAX_FLATER");76max_flaters.setAccessible(true);77int MAX_FLATERS = max_flaters.getInt(fs);78Field inflaters = fs.getClass().getDeclaredField("inflaters");79inflaters.setAccessible(true);80int inflater_count = ((List<?>) inflaters.get(fs)).size();81if (inflater_count > MAX_FLATERS) {82throw new Exception("Too many inflaters " + inflater_count);83}84Field deflaters = fs.getClass().getDeclaredField("deflaters");85deflaters.setAccessible(true);86int deflater_count = ((List<?>) deflaters.get(fs)).size();87if (deflater_count > MAX_FLATERS) {88throw new Exception("Too many deflaters " + deflater_count);89}90} catch (NoSuchFieldException nsfe) {91// Probably the implementation has changed, so there's not much we can do...92throw new RuntimeException("Implementation of jdk.nio.zipfs.ZipFileSystem changed - disable or fix the test");93}94} finally {95Files.deleteIfExists(zipFile);96}9798}99}100101102