]>
git.argeo.org Git - gpl/argeo-slc.git/blob - ext/javax.mail.mbox/src/com/sun/mail/mbox/TempFile.java
2 * Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
17 package com
.sun
.mail
.mbox
;
22 import java
.security
.*;
24 import com
.sun
.mail
.util
.PropUtil
;
25 import javax
.mail
.util
.SharedFileInputStream
;
28 * A temporary file used to cache messages.
32 private File file
; // the temp file name
33 private WritableSharedFile sf
;
36 * Create a temp file in the specified directory (if not null).
37 * The file will be deleted when the JVM exits.
39 public TempFile(File dir
) throws IOException
{
40 file
= File
.createTempFile("mbox.", ".mbox", dir
);
41 // XXX - need JDK 6 to set permissions on the file to owner-only
43 sf
= new WritableSharedFile(file
);
47 * Return a stream for appending to the temp file.
49 public AppendStream
getAppendStream() throws IOException
{
50 return sf
.getAppendStream();
54 * Return a stream for reading from part of the file.
56 public InputStream
newStream(long start
, long end
) {
57 return sf
.newStream(start
, end
);
60 public long length() {
65 * Close and remove this temp file.
70 } catch (IOException ex
) {
76 protected void finalize() throws Throwable
{
86 * A subclass of SharedFileInputStream that also allows writing.
88 class WritableSharedFile
extends SharedFileInputStream
{
89 private RandomAccessFile raf
;
90 private AppendStream af
;
92 public WritableSharedFile(File file
) throws IOException
{
95 raf
= new RandomAccessFile(file
, "rw");
96 } catch (IOException ex
) {
97 // if anything goes wrong opening the writable file,
98 // close the readable file too
104 * Return the writable version of this file.
106 public RandomAccessFile
getWritableFile() {
111 * Close the readable and writable files.
113 public void close() throws IOException
{
122 * Update the size of the readable file after writing
123 * to the file. Updates the length to be the current
126 synchronized long updateLength() throws IOException
{
127 datalen
= in
.length();
133 * Return a new AppendStream, but only if one isn't in active use.
135 public synchronized AppendStream
getAppendStream() throws IOException
{
137 throw new IOException(
138 "file cache only supports single threaded access");
139 af
= new AppendStream(this);
145 * A stream for writing to the temp file, and when done
146 * can return a stream for reading the data just written.
147 * NOTE: We assume that only one thread is writing to the
150 class AppendStream
extends OutputStream
{
151 private final WritableSharedFile tf
;
152 private RandomAccessFile raf
;
153 private final long start
;
156 public AppendStream(WritableSharedFile tf
) throws IOException
{
158 raf
= tf
.getWritableFile();
159 start
= raf
.length();
163 public void write(int b
) throws IOException
{
167 public void write(byte[] b
) throws IOException
{
171 public void write(byte[] b
, int off
, int len
) throws IOException
{
172 raf
.write(b
, off
, len
);
175 public synchronized void close() throws IOException
{
176 end
= tf
.updateLength();
177 raf
= null; // no more writing allowed
180 public synchronized InputStream
getInputStream() throws IOException
{
181 return tf
.newStream(start
, end
);