325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.org.jvnet.mimepull;
325N/A
325N/Aimport java.io.File;
325N/Aimport java.io.IOException;
325N/Aimport java.io.RandomAccessFile;
325N/Aimport java.lang.ref.ReferenceQueue;
325N/Aimport java.lang.ref.WeakReference;
325N/Aimport java.util.ArrayList;
325N/Aimport java.util.List;
325N/Aimport java.util.logging.Logger;
325N/A
325N/A/**
325N/A * Removing files based on this
325N/A * <a href="http://java.sun.com/developer/technicalArticles/javase/finalization/">article</a>
325N/A *
325N/A * @author Jitendra Kotamraju
325N/A */
325N/Afinal class WeakDataFile extends WeakReference<DataFile> {
325N/A
325N/A private static final Logger LOGGER = Logger.getLogger(WeakDataFile.class.getName());
325N/A private static final int MAX_ITERATIONS = 2;
325N/A private static ReferenceQueue<DataFile> refQueue = new ReferenceQueue<DataFile>();
325N/A private static List<WeakDataFile> refList = new ArrayList<WeakDataFile>();
325N/A private final File file;
325N/A private final RandomAccessFile raf;
325N/A
325N/A WeakDataFile(DataFile df, File file) {
325N/A super(df, refQueue);
325N/A refList.add(this);
325N/A this.file = file;
325N/A try {
325N/A raf = new RandomAccessFile(file, "rw");
325N/A } catch(IOException ioe) {
325N/A throw new MIMEParsingException(ioe);
325N/A }
325N/A drainRefQueueBounded();
325N/A }
325N/A
325N/A synchronized void read(long pointer, byte[] buf, int offset, int length ) {
325N/A try {
325N/A raf.seek(pointer);
325N/A raf.readFully(buf, offset, length);
325N/A } catch(IOException ioe) {
325N/A throw new MIMEParsingException(ioe);
325N/A }
325N/A }
325N/A
325N/A synchronized long writeTo(long pointer, byte[] data, int offset, int length) {
325N/A try {
325N/A raf.seek(pointer);
325N/A raf.write(data, offset, length);
325N/A return raf.getFilePointer(); // Update pointer for next write
325N/A } catch(IOException ioe) {
325N/A throw new MIMEParsingException(ioe);
325N/A }
325N/A }
325N/A
325N/A void close() {
325N/A LOGGER.fine("Deleting file = "+file.getName());
325N/A refList.remove(this);
325N/A try {
325N/A raf.close();
325N/A file.delete();
325N/A } catch(IOException ioe) {
325N/A throw new MIMEParsingException(ioe);
325N/A }
325N/A }
325N/A
325N/A void renameTo(File f) {
325N/A LOGGER.fine("Moving file="+file+" to="+f);
325N/A refList.remove(this);
325N/A try {
325N/A raf.close();
325N/A file.renameTo(f);
325N/A } catch(IOException ioe) {
325N/A throw new MIMEParsingException(ioe);
325N/A }
325N/A
325N/A }
325N/A
325N/A static void drainRefQueueBounded() {
325N/A int iterations = 0;
325N/A WeakDataFile weak = (WeakDataFile) refQueue.poll();
325N/A while (weak != null && iterations < MAX_ITERATIONS) {
325N/A LOGGER.fine("Cleaning file = "+weak.file+" from reference queue.");
325N/A weak.close();
325N/A ++iterations;
325N/A weak = (WeakDataFile) refQueue.poll();
325N/A }
325N/A }
325N/A
325N/A}