-
Notifications
You must be signed in to change notification settings - Fork 43
Provides a CachingOuptutStream and a CachingWriter #184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1f0fef0
Provides a CachingOuptutStream and a CachingWriter
gnodet 8ac7811
Better implementation of the caching outputstream / writer
gnodet f70e38b
Fix problem with jdk8 / jdk9 signatures
gnodet 566c4f4
Fix test
gnodet 0751e0f
Raise the sleep time to make sure tests do pass
gnodet 6cc3084
Fix tests
gnodet 4473d24
Improve tests
gnodet 0c03b46
Fix FileChannel#truncate not updating the last modified time
gnodet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/main/java/org/codehaus/plexus/util/io/CachingOutputStream.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.codehaus.plexus.util.io; | ||
|
||
/* | ||
* Copyright The Codehaus Foundation. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Caching OutputStream to avoid overwriting a file with | ||
* the same content. | ||
*/ | ||
public class CachingOutputStream extends ByteArrayOutputStream | ||
{ | ||
private final Path path; | ||
gnodet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private boolean modified; | ||
|
||
public CachingOutputStream( File path ) | ||
{ | ||
this( Objects.requireNonNull( path ).toPath() ); | ||
} | ||
|
||
public CachingOutputStream( Path path ) | ||
{ | ||
this.path = Objects.requireNonNull( path ); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException | ||
{ | ||
byte[] data = toByteArray(); | ||
if ( Files.exists( path ) && Files.size( path ) == data.length ) | ||
{ | ||
byte[] old = Files.readAllBytes( path ); | ||
if ( Arrays.equals( old, data ) ) | ||
gnodet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return; | ||
} | ||
} | ||
Files.write( path, data ); | ||
modified = true; | ||
} | ||
|
||
public boolean isModified() | ||
{ | ||
return modified; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/org/codehaus/plexus/util/io/CachingWriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.codehaus.plexus.util.io; | ||
|
||
/* | ||
* Copyright The Codehaus Foundation. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Caching Writer to avoid overwriting a file with | ||
* the same content. | ||
*/ | ||
public class CachingWriter extends StringWriter | ||
{ | ||
private final Path path; | ||
private final Charset charset; | ||
private boolean modified; | ||
|
||
public CachingWriter( File path, Charset charset ) | ||
{ | ||
this( Objects.requireNonNull( path ).toPath(), charset ); | ||
} | ||
|
||
public CachingWriter( Path path, Charset charset ) | ||
{ | ||
this.path = Objects.requireNonNull( path ); | ||
this.charset = Objects.requireNonNull( charset ); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException | ||
{ | ||
byte[] data = getBuffer().toString().getBytes( charset ); | ||
if ( Files.exists( path ) && Files.size( path ) == data.length ) | ||
{ | ||
byte[] ba = Files.readAllBytes( path ); | ||
if ( Arrays.equals( data, ba ) ) | ||
gnodet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return; | ||
} | ||
} | ||
Files.write( path, data ); | ||
modified = true; | ||
} | ||
|
||
public boolean isModified() | ||
{ | ||
return modified; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.