001/* 002 * #%L 003 * Netarchivesuite - common - test 004 * %% 005 * Copyright (C) 2005 - 2018 The Royal Danish Library, 006 * the National Library of France and the Austrian National Library. 007 * %% 008 * This program is free software: you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation, either version 2.1 of the 011 * License, or (at your option) any later version. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Lesser Public License for more details. 017 * 018 * You should have received a copy of the GNU General Lesser Public 019 * License along with this program. If not, see 020 * <http://www.gnu.org/licenses/lgpl-2.1.html>. 021 * #L% 022 */ 023 024package dk.netarkivet.common.distribute; 025 026import java.io.ByteArrayInputStream; 027import java.io.File; 028import java.io.IOException; 029import java.io.InputStream; 030import java.io.OutputStream; 031 032import dk.netarkivet.common.exceptions.IOFailure; 033import dk.netarkivet.common.utils.ChecksumCalculator; 034import dk.netarkivet.common.utils.FileUtils; 035 036/** 037 * A RemoteFile implementation that just takes a string. 038 */ 039 040@SuppressWarnings({"serial"}) 041public class StringRemoteFile implements RemoteFile { 042 /** the contents. */ 043 String contents; 044 /** the filename. */ 045 String filename; 046 047 public StringRemoteFile(String s) { 048 this.filename = "unnamed string"; 049 contents = s; 050 } 051 052 public StringRemoteFile(String filename, String s) { 053 this.filename = filename; 054 contents = s; 055 } 056 057 /** 058 * Copy remotefile to local disk storage. Used by the data recipient. 059 * 060 * @param destFile local File 061 */ 062 public void copyTo(File destFile) { 063 FileUtils.writeBinaryFile(destFile, contents.getBytes()); 064 } 065 066 /** 067 * Write the contents of this remote file to an output stream. 068 * 069 * @param out OutputStream that the data will be written to. This stream will not be closed by this operation. 070 * @throws IOFailure If append operation fails 071 */ 072 public void appendTo(OutputStream out) { 073 try { 074 out.write(contents.getBytes()); 075 } catch (IOException e) { 076 throw new IOFailure("Could not write string to " + out); 077 } 078 } 079 080 public InputStream getInputStream() { 081 return new ByteArrayInputStream(contents.getBytes()); 082 } 083 084 /** 085 * Return the file name. 086 * 087 * @return the file name 088 */ 089 public String getName() { 090 return filename; 091 } 092 093 /** 094 * Returns a MD5 Checksum on the file. 095 * 096 * @return MD5 checksum 097 */ 098 public String getChecksum() { 099 return ChecksumCalculator.calculateMd5(contents.getBytes()); 100 } 101 102 /** 103 * Deletes the local file to which this remote file refers. 104 */ 105 public void cleanup() { 106 // Inaccessible after this. 107 contents = null; 108 } 109 110 /** 111 * Returns the total size of the remote file. 112 * 113 * @return Size of the remote file. 114 */ 115 public long getSize() { 116 return contents.length(); 117 } 118}