001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.client.model;
013
014import java.io.File;
015import java.io.FileInputStream;
016import java.io.IOException;
017import java.io.InputStream;
018
019/**
020 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
021 */
022public class FileBlob extends Blob implements HasFile {
023
024    private static final long serialVersionUID = 1L;
025
026    protected final File file;
027
028    public FileBlob(File file) {
029        super(file.getName(), getMimeTypeFromExtension(file.getPath()));
030        this.file = file;
031    }
032
033    @Override
034    public InputStream getStream() throws IOException {
035        return new FileInputStream(file);
036    }
037
038    @Override
039    public int getLength() {
040        long length = file.length();
041        if (length > (long) Integer.MAX_VALUE) {
042            return -1;
043        }
044        return (int) length;
045    }
046
047    public File getFile() {
048        return file;
049    }
050
051    public static String getMimeTypeFromExtension(String path) {
052        return "application/octet-stream";
053    }
054
055}