Skip to content

Commit

Permalink
yegor256#254 - TempInputStream + couple of puzzles
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Eliseev committed Jan 22, 2016
1 parent ca5b26b commit 67697cc
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 26 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/takes/rq/CapInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 0.16
* @todo #254:30min CapInputStream should delegate all standard InputStream
* calls to it's origin. It's very important in context of closing stream -
* right code should close the stream but default InputStream implementation
* just throws IOException
*/
final class CapInputStream extends InputStream {

Expand Down
52 changes: 26 additions & 26 deletions src/main/java/org/takes/rq/RqMultipart.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,40 +230,40 @@ private ConcurrentMap<String, List<Request>> buildRequests(
* @param boundary Boundary
* @return Request
* @throws IOException If fails
* @todo #254:30min in order to delete temporary files InputStream
* instance on Request.body should be closed. In context of multipart
* requests that means that body of all parts should be closed once
* they are not needed anymore.
*/
private Request make(final byte[] boundary) throws IOException {
final File file = File.createTempFile(
RqMultipart.class.getName(), ".tmp"
);
final FileChannel channel = new RandomAccessFile(
file, "rw"
).getChannel();
try {
final FileChannel channel = new RandomAccessFile(
file, "rw"
).getChannel();
try {
channel.write(
ByteBuffer.wrap(
this.head().iterator().next().getBytes()
)
);
// @checkstyle MultipleStringLiteralsCheck (1 line)
channel.write(ByteBuffer.wrap("\r\n".getBytes()));
this.copy(channel, boundary);
} finally {
channel.close();
}
final InputStream input = new FileInputStream(file);
try {
return new RqWithHeader(
new RqLive(new FileInputStream(file)),
"Content-Length",
String.valueOf(file.length())
);
} finally {
input.close();
}
channel.write(
ByteBuffer.wrap(
this.head().iterator().next().getBytes()
)
);
// @checkstyle MultipleStringLiteralsCheck (1 line)
channel.write(ByteBuffer.wrap("\r\n".getBytes()));
this.copy(channel, boundary);
} finally {
file.delete();
channel.close();
}
return new RqWithHeader(
new RqLive(
new TempInputStream(
new FileInputStream(file),
file
)
),
"Content-Length",
String.valueOf(file.length())
);
}
/**
* Copy until boundary reached.
Expand Down
100 changes: 100 additions & 0 deletions src/main/java/org/takes/rq/TempInputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.rq;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
* Input stream using a temporary cache file.
*
* <p>All implementations of this interface must be immutable and thread-safe.
*
* @author Andrey Eliseev (aeg.exper0@gmail.com)
* @version $Id$
* @since 0.31
*/
final class TempInputStream extends InputStream {

/**
* Original stream.
*/
private final transient InputStream origin;

/**
* Temporary file used as a cache.
*/
private final transient File file;

/**
* Ctor.
* @param stream Original stream
* @param temp Temporary file used as a cache.
*/
TempInputStream(final InputStream stream, final File temp) {
super();
this.origin = stream;
this.file = temp;
}

/**
* Closes the Input stream, deleting the now useless temporary file.
* @throws IOException if some problem occurs.
*/
@Override
public void close() throws IOException {
super.close();
this.origin.close();
if (!this.file.delete()) {
throw new IOException("unable to delete file");
}
}

@Override
public int read() throws IOException {
return this.origin.read();
}

@Override
public int read(final byte[] buf) throws IOException {
return this.origin.read(buf);
}

@Override
public int read(final byte[] buf, final int off,
final int len) throws IOException {
return this.origin.read(buf, off, len);
}

@Override
public long skip(final long num) throws IOException {
return this.origin.skip(num);
}

@Override
public int available() throws IOException {
return this.origin.available();
}
}
64 changes: 64 additions & 0 deletions src/test/java/org/takes/rq/TempInputStreamTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.rq;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Test case for {@link TempInputStream}.
* @author Andrey Eliseev (aeg.exper@gmail.com)
* @version $Id$
* @since 0.31
*/
public final class TempInputStreamTest {

/**
* TempInputStream can delete the underlying temporary file.
* @throws IOException if some problem occurs.
*/
@Test
public void deletesTempFile() throws IOException {
final File file = File.createTempFile("tempfile", ".tmp");
final BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write("Temp file deletion test");
out.close();
final InputStream body = new TempInputStream(
new FileInputStream(file), file
);
body.close();
MatcherAssert.assertThat(
"File exists after stream closure",
file.exists(),
Matchers.is(false)
);
}
}

0 comments on commit 67697cc

Please sign in to comment.