List of usage examples for org.apache.commons.io.output NullOutputStream nullOutputStream
public static OutputStream nullOutputStream()
From source file:org.kalypso.commons.java.util.zip.ZipUtilities.java
/** Unzips a zip archive into a directory using the apache zip classes. */ public static void unzipApache(final File zip, final File targetDir, final boolean overwriteExisting, final String encoding) throws IOException { org.apache.tools.zip.ZipFile file = null; try {/*w ww.j a v a2 s . c o m*/ file = new org.apache.tools.zip.ZipFile(zip, encoding); final Enumeration<?> entries = file.getEntries(); while (entries.hasMoreElements()) { final org.apache.tools.zip.ZipEntry entry = (org.apache.tools.zip.ZipEntry) entries.nextElement(); if (entry == null) break; final File newfile = new File(targetDir, entry.getName()); if (entry.isDirectory()) newfile.mkdirs(); else { if (!newfile.getParentFile().exists()) newfile.getParentFile().mkdirs(); OutputStream os = null; InputStream zis = null; try { if (!overwriteExisting && newfile.exists()) os = new NullOutputStream(); else os = new BufferedOutputStream(new FileOutputStream(newfile)); zis = file.getInputStream(entry); IOUtils.copy(zis, os); } finally { IOUtils.closeQuietly(os); IOUtils.closeQuietly(zis); } } } file.close(); } finally { if (file != null) file.close(); } }
From source file:org.kalypso.commons.java.util.zip.ZipUtilities.java
public static void unzip(final InputStream inputStream, final String pattern, final File targetdir, final boolean overwriteExisting) throws IOException { final ZipInputStream zis = new ZipInputStream(inputStream); while (true) { final ZipEntry entry = zis.getNextEntry(); if (entry == null) break; final String name = entry.getName(); if (!SelectorUtils.matchPath(pattern, name)) continue; final File newfile = new File(targetdir, name); if (entry.isDirectory()) newfile.mkdirs();/*from ww w . ja v a 2 s . c om*/ else { if (!newfile.getParentFile().exists()) newfile.getParentFile().mkdirs(); OutputStream os = null; try { if (!overwriteExisting && newfile.exists()) os = new NullOutputStream(); else os = new BufferedOutputStream(new FileOutputStream(newfile)); IOUtils.copy(zis, os); } finally { IOUtils.closeQuietly(os); } } } }
From source file:org.kalypso.commons.java.util.zip.ZipUtilities.java
/** * unzips a zip-stream into a target dir. * //w w w .jav a 2 s .c o m * @param overwriteExisting * if false, existing files will not be overwritten. Folders are always created. */ public static void unzip(final InputStream inputStream, final File targetdir, final boolean overwriteExisting) throws IOException { final ZipInputStream zis = new ZipInputStream(inputStream); while (true) { final ZipEntry entry = zis.getNextEntry(); if (entry == null) break; final File newfile = new File(targetdir, entry.getName()); if (entry.isDirectory()) newfile.mkdirs(); else { if (!newfile.getParentFile().exists()) newfile.getParentFile().mkdirs(); OutputStream os = null; try { if (!overwriteExisting && newfile.exists()) os = new NullOutputStream(); else os = new BufferedOutputStream(new FileOutputStream(newfile)); IOUtils.copy(zis, os); } finally { IOUtils.closeQuietly(os); zis.closeEntry(); } } } }
From source file:org.kitodo.filemanagement.locking.LockManagementTest.java
/** * If a user has an exclusive lock on one URI, the other user can not get * another lock on that URI. The exception is a fixed read lock, which is not * granted unless the file is being written. */// w w w.j ava 2 s . c om @Test public void testExclusiveLockIsExclusive() throws IOException { underTest.clear(); LockResult alicesAccess = underTest.tryLock(createRequest(ALICE, AN_URI, LockingMode.EXCLUSIVE), null); assertTrue(MESSAGE_ALICE_ALLOWED, alicesAccess instanceof GrantedAccess); LockResult noAccessForBob = underTest.tryLock(createRequest(BOB, AN_URI, LockingMode.UPGRADEABLE_READ), null); assertTrue(MESSAGE_BOB_NOT_ALLOWED, noAccessForBob instanceof DeniedAccess); noAccessForBob = underTest.tryLock(createRequest(BOB, AN_URI, LockingMode.UPGRADE_WRITE_ONCE), null); assertTrue(MESSAGE_BOB_NOT_ALLOWED, noAccessForBob instanceof DeniedAccess); try (VigilantOutputStream streamGuard = underTest.reportGrant(AN_URI, new NullOutputStream(), alicesAccess)) { noAccessForBob = underTest.tryLock(createRequest(BOB, AN_URI, LockingMode.IMMUTABLE_READ), null); assertTrue(MESSAGE_BOB_NOT_ALLOWED, noAccessForBob instanceof DeniedAccess); } }
From source file:org.kitodo.filemanagement.locking.LockManagementTest.java
/** * A user can return a lock on a file if it has closed all streams through that * lock. If not, he gets an IllegalStateException. *///from w ww . jav a 2 s . c o m @Test(expected = IllegalStateException.class) public void testLockCanOnlyBeReturnedIfAllStreamsAreClosedForOpenOutputStream() throws IOException { underTest.clear(); LockResult alicesAccess = underTest.tryLock(createRequest(ALICE, AN_URI, LockingMode.EXCLUSIVE), null); assertTrue(MESSAGE_ALICE_ALLOWED, alicesAccess instanceof GrantedAccess); try (OutputStream openOutputStream = underTest.reportGrant(AN_URI, new NullOutputStream(), alicesAccess)) { alicesAccess.close(); } }
From source file:org.kitodo.filemanagement.locking.LockManagementTest.java
/** * If a user requests multiple locks, they will only be granted if all locks are * possible, otherwise no locks will be granted. *//*www . j av a 2 s . c om*/ @Test public void testMultiLocking() throws IOException { final URI existingURI1 = File.createTempFile("an_existing_file", ".xml").toURI(); final URI existingURI2 = File.createTempFile("another_existing_file", ".xml").toURI(); for (LockingMode firstLock : LockingMode.values()) { for (LockingMode secondLock : LockingMode.values()) { Map<URI, LockingMode> request = new TreeMap<>(); request.put(existingURI1, firstLock); request.put(existingURI2, secondLock); underTest.clear(); LockResult alicesAccess = underTest.tryLock(new LockRequests(ALICE, request), null); assertTrue(MESSAGE_ALICE_ALLOWED, alicesAccess instanceof GrantedAccess); } } for (LockingMode firstLock : LockingMode.values()) { for (LockingMode secondLock : LockingMode.values()) { underTest.clear(); LockResult alicesAccess = underTest .tryLock(createRequest(ALICE, existingURI1, LockingMode.EXCLUSIVE), null); assertTrue(MESSAGE_ALICE_ALLOWED, alicesAccess instanceof GrantedAccess); Map<URI, LockingMode> requestTwoURIs = new TreeMap<>(); requestTwoURIs.put(existingURI1, firstLock); requestTwoURIs.put(existingURI2, secondLock); LockResult noAccessForBob; try (OutputStream aliceIsWriting = underTest.reportGrant(existingURI1, new NullOutputStream(), alicesAccess)) { noAccessForBob = underTest.tryLock(new LockRequests(BOB, requestTwoURIs), null); } assertTrue(MESSAGE_BOB_NOT_ALLOWED, noAccessForBob instanceof DeniedAccess); assertTrue("Bob should know which URI failed", noAccessForBob.getConflicts().containsKey(existingURI1)); assertFalse("Bob should know which URI not failed", noAccessForBob.getConflicts().containsKey(existingURI2)); assertTrue(MESSAGE_PROBLEM, noAccessForBob.getConflicts().get(existingURI1).contains(ALICE)); } } }
From source file:org.kitodo.filemanagement.locking.LockManagementTest.java
/** * A user cannot open a read stream to a URI for which he wanted to get * permission before, but was rejected./* www. j a v a 2s . co m*/ */ @Test(expected = AccessDeniedException.class) public void testReadingRequiresGrantedPermissionForImmutableReadLock() throws IOException { underTest.clear(); LockResult alicesAccess = underTest.tryLock(createRequest(ALICE, AN_URI, LockingMode.EXCLUSIVE), null); assertTrue(MESSAGE_ALICE_ALLOWED, alicesAccess instanceof GrantedAccess); LockResult noAccessForBob; try (OutputStream aliceIsWriting = underTest.reportGrant(AN_URI, new NullOutputStream(), alicesAccess)) { noAccessForBob = underTest.tryLock(createRequest(BOB, AN_URI, LockingMode.IMMUTABLE_READ), null); assertTrue(MESSAGE_BOB_NOT_ALLOWED, noAccessForBob instanceof DeniedAccess); } new FileManagement().write(AN_URI, noAccessForBob); }
From source file:org.kitodo.filemanagement.locking.LockManagementTest.java
/** * Multiple users can get an upgradeable read lock on a file, but only one user * at a time can expand its read lock for a one-time write. As part of the * contract, the user who wants to rewrite the file must read it first. While at * least one user has an extensible read lock on a URI, no user can get * exclusive access to the URI. If he tries, he gets back the names of the lock * owners./*w w w. ja va 2 s. c om*/ */ @Test public void testUpgradeableReadLocking() throws IOException { underTest.clear(); LockResult alicesAccess = underTest.tryLock(createRequest(ALICE, AN_URI, LockingMode.UPGRADEABLE_READ), null); assertTrue(MESSAGE_ALICE_ALLOWED, alicesAccess instanceof GrantedAccess); LockResult bobsAccess = underTest.tryLock(createRequest(BOB, AN_URI, LockingMode.UPGRADEABLE_READ), null); assertTrue(MESSAGE_BOB_ALLOWED, bobsAccess instanceof GrantedAccess); Map<URI, Collection<String>> noConflictForAlice = alicesAccess .tryLock(createRequest(AN_URI, LockingMode.UPGRADE_WRITE_ONCE)); assertTrue(MESSAGE_ALICE_LOCK, noConflictForAlice.entrySet().isEmpty()); Map<URI, Collection<String>> bobsConflict = bobsAccess .tryLock(createRequest(AN_URI, LockingMode.UPGRADE_WRITE_ONCE)); assertFalse(MESSAGE_BOB_LOCK, bobsConflict.entrySet().isEmpty()); assertTrue(MESSAGE_PROBLEM, bobsConflict.get(AN_URI).contains(ALICE)); try (InputStream aliceIsReading = underTest.reportGrant(AN_URI, new NullInputStream(0), alicesAccess)) { bobsConflict = bobsAccess.tryLock(createRequest(AN_URI, LockingMode.UPGRADE_WRITE_ONCE)); assertFalse(MESSAGE_BOB_LOCK, bobsConflict.entrySet().isEmpty()); assertTrue(MESSAGE_PROBLEM, bobsConflict.get(AN_URI).contains(ALICE)); } bobsConflict = bobsAccess.tryLock(createRequest(AN_URI, LockingMode.UPGRADE_WRITE_ONCE)); assertFalse(MESSAGE_BOB_LOCK, bobsConflict.entrySet().isEmpty()); assertTrue(MESSAGE_PROBLEM, bobsConflict.get(AN_URI).contains(ALICE)); try (OutputStream aliceIsWriting = underTest.reportGrant(AN_URI, new NullOutputStream(), alicesAccess)) { bobsConflict = bobsAccess.tryLock(createRequest(AN_URI, LockingMode.UPGRADE_WRITE_ONCE)); assertFalse(MESSAGE_BOB_LOCK, bobsConflict.entrySet().isEmpty()); assertTrue(MESSAGE_PROBLEM, bobsConflict.get(AN_URI).contains(ALICE)); } Map<URI, Collection<String>> bobsMove = bobsAccess .tryLock(createRequest(AN_URI, LockingMode.UPGRADE_WRITE_ONCE)); assertTrue("Bob should have been able to extend his lock", bobsMove.entrySet().isEmpty()); Map<URI, Collection<String>> alicesConflict = alicesAccess .tryLock(createRequest(AN_URI, LockingMode.UPGRADE_WRITE_ONCE)); assertFalse("Alice should not have been able to extend her lock", alicesConflict.entrySet().isEmpty()); assertTrue("Alice should have learned that Bob is her problem", alicesConflict.get(AN_URI).contains(BOB)); }
From source file:org.kitodo.filemanagement.locking.LockManagementTest.java
/** * Mimic the lock management that the file would be written. The lock management * generates a stream guard for the transferred stream. When the stream guard is * closed, the lock management believes the file was written. * * @param lockManagement/*from ww w . ja va2 s. c om*/ * lock management to fool * @param uri * URI of stream * @param access * granted access */ private static void mimicWriting(LockManagement lockManagement, URI uri, GrantedAccess access) throws IOException { lockManagement.reportGrant(uri, new NullOutputStream(), access).close(); }
From source file:org.lightjason.agentspeak.consistency.metric.CNCD.java
/** * compression algorithm/* w w w . jav a2 s .com*/ * * @param p_input input string * @return number of compression bytes * @warning counting stream returns the correct number of bytes after flushing */ private double compress(final String p_input) { final DataOutputStream l_counting = new DataOutputStream(new NullOutputStream()); try (final InputStream l_input = new ByteArrayInputStream(p_input.getBytes(StandardCharsets.UTF_8)); final OutputStream l_compress = m_compression.get(l_counting)) { IOUtils.copy(l_input, l_compress); } catch (final IOException l_exception) { return 0; } return l_counting.size(); }