-
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 all commits
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
175 changes: 175 additions & 0 deletions
175
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,175 @@ | ||
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.OutputStream; | ||
import java.nio.Buffer; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
import java.nio.file.attribute.FileTime; | ||
import java.time.Instant; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Caching OutputStream to avoid overwriting a file with | ||
* the same content. | ||
*/ | ||
public class CachingOutputStream extends OutputStream | ||
{ | ||
private final Path path; | ||
gnodet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private FileChannel channel; | ||
private ByteBuffer readBuffer; | ||
private ByteBuffer writeBuffer; | ||
private boolean modified; | ||
|
||
public CachingOutputStream( File path ) throws IOException | ||
{ | ||
this( Objects.requireNonNull( path ).toPath() ); | ||
} | ||
|
||
public CachingOutputStream( Path path ) throws IOException | ||
{ | ||
this( path, 32 * 1024 ); | ||
} | ||
|
||
public CachingOutputStream( Path path, int bufferSize ) throws IOException | ||
{ | ||
this.path = Objects.requireNonNull( path ); | ||
this.channel = FileChannel.open( path, | ||
StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE ); | ||
this.readBuffer = ByteBuffer.allocate( bufferSize ); | ||
this.writeBuffer = ByteBuffer.allocate( bufferSize ); | ||
} | ||
|
||
@Override | ||
public void write( int b ) throws IOException | ||
{ | ||
if ( writeBuffer.remaining() < 1 ) | ||
{ | ||
( ( Buffer ) writeBuffer ).flip(); | ||
flushBuffer( writeBuffer ); | ||
( ( Buffer ) writeBuffer ).clear(); | ||
} | ||
writeBuffer.put( ( byte ) b ); | ||
} | ||
|
||
@Override | ||
public void write( byte[] b ) throws IOException | ||
{ | ||
write( b, 0, b.length ); | ||
} | ||
|
||
@Override | ||
public void write( byte[] b, int off, int len ) throws IOException | ||
{ | ||
if ( writeBuffer.remaining() < len ) | ||
{ | ||
( ( Buffer ) writeBuffer ).flip(); | ||
flushBuffer( writeBuffer ); | ||
( ( Buffer ) writeBuffer ).clear(); | ||
} | ||
int capacity = writeBuffer.capacity(); | ||
while ( len >= capacity ) | ||
{ | ||
flushBuffer( ByteBuffer.wrap( b, off, capacity ) ); | ||
off += capacity; | ||
len -= capacity; | ||
} | ||
if ( len > 0 ) | ||
{ | ||
writeBuffer.put( b, off, len ); | ||
} | ||
} | ||
|
||
@Override | ||
public void flush() throws IOException | ||
{ | ||
( ( Buffer ) writeBuffer ).flip(); | ||
flushBuffer( writeBuffer ); | ||
( ( Buffer ) writeBuffer ).clear(); | ||
super.flush(); | ||
} | ||
|
||
private void flushBuffer( ByteBuffer writeBuffer ) throws IOException | ||
{ | ||
if ( modified ) | ||
{ | ||
channel.write( writeBuffer ); | ||
} | ||
else | ||
{ | ||
int len = writeBuffer.remaining(); | ||
ByteBuffer readBuffer; | ||
if ( this.readBuffer.capacity() >= len ) | ||
{ | ||
readBuffer = this.readBuffer; | ||
( ( Buffer ) readBuffer ).clear(); | ||
} | ||
else | ||
{ | ||
readBuffer = ByteBuffer.allocate( len ); | ||
} | ||
while ( len > 0 ) | ||
{ | ||
int read = channel.read( readBuffer ); | ||
if ( read <= 0 ) | ||
{ | ||
modified = true; | ||
channel.position( channel.position() - readBuffer.position() ); | ||
channel.write( writeBuffer ); | ||
return; | ||
} | ||
len -= read; | ||
} | ||
( ( Buffer ) readBuffer ).flip(); | ||
if ( readBuffer.compareTo( writeBuffer ) != 0 ) | ||
{ | ||
modified = true; | ||
channel.position( channel.position() - readBuffer.remaining() ); | ||
channel.write( writeBuffer ); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws IOException | ||
{ | ||
flush(); | ||
long position = channel.position(); | ||
if ( position != channel.size() ) | ||
{ | ||
if ( !modified ) | ||
{ | ||
FileTime now = FileTime.from( Instant.now() ); | ||
Files.setLastModifiedTime( path, now ); | ||
modified = true; | ||
} | ||
channel.truncate( position ); | ||
} | ||
channel.close(); | ||
} | ||
|
||
public boolean isModified() | ||
{ | ||
return modified; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
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,62 @@ | ||
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.OutputStreamWriter; | ||
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 OutputStreamWriter | ||
{ | ||
private final CachingOutputStream cos; | ||
|
||
public CachingWriter( File path, Charset charset ) throws IOException | ||
{ | ||
this( Objects.requireNonNull( path ).toPath(), charset ); | ||
} | ||
|
||
public CachingWriter( Path path, Charset charset ) throws IOException | ||
{ | ||
this( path, charset, 32 * 1024 ); | ||
} | ||
|
||
public CachingWriter( Path path, Charset charset, int bufferSize ) throws IOException | ||
{ | ||
this( new CachingOutputStream( path, bufferSize ), charset ); | ||
} | ||
|
||
private CachingWriter( CachingOutputStream outputStream, Charset charset ) throws IOException | ||
{ | ||
super( outputStream, charset ); | ||
this.cos = outputStream; | ||
} | ||
|
||
public boolean isModified() | ||
{ | ||
return cos.isModified(); | ||
} | ||
} |
145 changes: 145 additions & 0 deletions
145
src/test/java/org/codehaus/plexus/util/io/CachingOutputStreamTest.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,145 @@ | ||
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.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.attribute.FileTime; | ||
import java.util.Objects; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class CachingOutputStreamTest | ||
{ | ||
|
||
Path tempDir; | ||
Path checkLastModified; | ||
FileTime lm; | ||
|
||
@Before | ||
public void setup() throws IOException | ||
{ | ||
Path dir = Paths.get( "target/io" ); | ||
Files.createDirectories( dir ); | ||
tempDir = Files.createTempDirectory( dir, "temp-" ); | ||
checkLastModified = tempDir.resolve( ".check" ); | ||
Files.newOutputStream( checkLastModified ).close(); | ||
lm = Files.getLastModifiedTime( checkLastModified ); | ||
} | ||
|
||
private void waitLastModified() throws IOException, InterruptedException | ||
{ | ||
while ( true ) | ||
{ | ||
Files.newOutputStream( checkLastModified ).close(); | ||
FileTime nlm = Files.getLastModifiedTime( checkLastModified ); | ||
if ( !Objects.equals( nlm, lm ) ) | ||
{ | ||
lm = nlm; | ||
break; | ||
} | ||
Thread.sleep( 10 ); | ||
} | ||
} | ||
|
||
@Test | ||
public void testWriteNoExistingFile() throws IOException, InterruptedException | ||
{ | ||
byte[] data = "Hello world!".getBytes( StandardCharsets.UTF_8 ); | ||
Path path = tempDir.resolve( "file.txt" ); | ||
assertFalse( Files.exists( path ) ); | ||
|
||
try ( CachingOutputStream cos = new CachingOutputStream( path, 4 ) ) | ||
{ | ||
cos.write( data ); | ||
} | ||
assertTrue( Files.exists( path ) ); | ||
byte[] read = Files.readAllBytes( path ); | ||
assertArrayEquals( data, read ); | ||
FileTime modified = Files.getLastModifiedTime( path ); | ||
|
||
waitLastModified(); | ||
|
||
try ( CachingOutputStream cos = new CachingOutputStream( path, 4 ) ) | ||
{ | ||
cos.write( data ); | ||
} | ||
assertTrue( Files.exists( path ) ); | ||
read = Files.readAllBytes( path ); | ||
assertArrayEquals( data, read ); | ||
FileTime newModified = Files.getLastModifiedTime( path ); | ||
assertEquals( modified, newModified ); | ||
modified = newModified; | ||
|
||
waitLastModified(); | ||
|
||
// write longer data | ||
data = "Good morning!".getBytes( StandardCharsets.UTF_8 ); | ||
try ( CachingOutputStream cos = new CachingOutputStream( path, 4 ) ) | ||
{ | ||
cos.write( data ); | ||
} | ||
assertTrue( Files.exists( path ) ); | ||
read = Files.readAllBytes( path ); | ||
assertArrayEquals( data, read ); | ||
newModified = Files.getLastModifiedTime( path ); | ||
assertNotEquals( modified, newModified ); | ||
modified = newModified; | ||
|
||
waitLastModified(); | ||
|
||
// different data same size | ||
data = "Good mornong!".getBytes( StandardCharsets.UTF_8 ); | ||
try ( CachingOutputStream cos = new CachingOutputStream( path, 4 ) ) | ||
{ | ||
cos.write( data ); | ||
} | ||
assertTrue( Files.exists( path ) ); | ||
read = Files.readAllBytes( path ); | ||
assertArrayEquals( data, read ); | ||
newModified = Files.getLastModifiedTime( path ); | ||
assertNotEquals( modified, newModified ); | ||
modified = newModified; | ||
|
||
waitLastModified(); | ||
|
||
// same data but shorter | ||
data = "Good mornon".getBytes( StandardCharsets.UTF_8 ); | ||
try ( CachingOutputStream cos = new CachingOutputStream( path, 4 ) ) | ||
{ | ||
cos.write( data ); | ||
} | ||
assertTrue( Files.exists( path ) ); | ||
read = Files.readAllBytes( path ); | ||
assertArrayEquals( data, read ); | ||
newModified = Files.getLastModifiedTime( path ); | ||
assertNotEquals( modified, newModified ); | ||
modified = newModified; | ||
} | ||
|
||
} |
Oops, something went wrong.
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.