2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.io.BufferedInputStream;
24 import java.io.BufferedReader;
25 import java.io.ByteArrayInputStream;
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.io.Reader;
34 import java.io.StringReader;
35 import java.net.HttpURLConnection;
36 import java.net.MalformedURLException;
37 import java.net.SocketTimeoutException;
39 import java.net.URLConnection;
40 import java.net.UnknownHostException;
41 import java.util.zip.GZIPInputStream;
43 import jalview.api.AlignExportSettingsI;
44 import jalview.api.AlignViewportI;
45 import jalview.api.AlignmentViewPanel;
46 import jalview.api.FeatureSettingsModelI;
47 import jalview.bin.Console;
48 import jalview.util.HttpUtils;
49 import jalview.util.MessageManager;
50 import jalview.util.Platform;
53 * implements a random access wrapper around a particular datasource, for
54 * passing to identifyFile and AlignFile objects.
56 public class FileParse
58 protected static final String SPACE = " ";
60 protected static final String TAB = "\t";
63 * text specifying source of data. usually filename or url.
65 private String dataName = "unknown source";
67 public File inFile = null;
69 private byte[] bytes; // from JavaScript
71 public byte[] getBytes()
77 * a viewport associated with the current file operation. May be null. May
78 * move to different object.
80 private AlignViewportI viewport;
83 * specific settings for exporting data from the current context
85 private AlignExportSettingsI exportSettings;
88 * sequence counter for FileParse object created from same data source
93 * separator for extracting specific 'frame' of a datasource for formats that
94 * support multiple records (e.g. BLC, Stockholm, etc)
96 protected char suffixSeparator = '#';
99 * character used to write newlines
101 protected String newline = System.getProperty("line.separator");
103 public void setNewlineString(String nl)
108 public String getNewlineString()
114 * '#' separated string tagged on to end of filename or url that was clipped
115 * off to resolve to valid filename
117 protected String suffix = null;
119 protected DataSourceType dataSourceType = null;
121 protected BufferedReader dataIn = null;
123 protected String errormessage = "UNINITIALISED SOURCE";
125 protected boolean error = true;
127 protected String warningMessage = null;
130 * size of readahead buffer used for when initial stream position is marked.
132 final int READAHEAD_LIMIT = 2048;
139 * Create a new FileParse instance reading from the same datasource starting
140 * at the current position. WARNING! Subsequent reads from either object will
141 * affect the read position of the other, but not the error state.
145 public FileParse(FileParse from) throws IOException
149 throw new Error(MessageManager
150 .getString("error.implementation_error_null_fileparse"));
156 index = ++from.index;
157 inFile = from.inFile;
158 suffixSeparator = from.suffixSeparator;
159 suffix = from.suffix;
160 errormessage = from.errormessage; // inherit potential error messages
161 error = false; // reset any error condition.
162 dataSourceType = from.dataSourceType;
163 dataIn = from.dataIn;
168 dataName = from.dataName;
172 * Attempt to open a file as a datasource. Sets error and errormessage if
173 * fileStr was invalid.
176 * @return this.error (true if the source was invalid)
178 private boolean checkFileSource(String fileStr) throws IOException
181 this.inFile = new File(fileStr);
182 // check to see if it's a Jar file in disguise.
183 if (!inFile.exists())
185 errormessage = "FILE NOT FOUND";
188 if (!inFile.canRead())
190 errormessage = "FILE CANNOT BE OPENED FOR READING";
193 if (inFile.isDirectory())
195 // this is really a 'complex' filetype - but we don't handle directory
197 errormessage = "FILE IS A DIRECTORY";
204 dataIn = checkForGzipStream(new FileInputStream(fileStr));
206 } catch (Exception x)
208 warningMessage = "Failed to resolve " + fileStr
209 + " as a data source. (" + x.getMessage() + ")";
210 // x.printStackTrace();
219 * Recognise the 2-byte magic header for gzip streams
221 * https://recalll.co/ask/v/topic/java-How-to-check-if-InputStream-is-Gzipped/555aadd62bd27354438b90f6
224 * - at least two bytes
226 * @throws IOException
228 public static boolean isGzipStream(InputStream input) throws IOException
230 if (!input.markSupported())
233 "FileParse.izGzipStream: input stream must support mark/reset");
238 // get first 2 bytes or return false
239 byte[] bytes = new byte[2];
240 int read = input.read(bytes);
242 if (read != bytes.length)
247 int header = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
248 return (GZIPInputStream.GZIP_MAGIC == header);
252 * Returns a Reader for the given input after wrapping it in a buffered input
253 * stream, and then checking if it needs to be wrapped by a GZipInputStream
258 private BufferedReader checkForGzipStream(InputStream input)
262 // https://stackoverflow.com/questions/4818468/how-to-check-if-inputstream-is-gzipped
263 // could use a PushBackInputStream rather than a BufferedInputStream
264 if (!input.markSupported())
266 input = new BufferedInputStream(input, 16);
268 if (isGzipStream(input))
270 return getGzipReader(input);
272 // return a buffered reader for the stream.
273 InputStreamReader isReader = new InputStreamReader(input);
274 BufferedReader toReadFrom = new BufferedReader(isReader);
279 * Returns a {@code BufferedReader} which wraps the input stream with a
280 * GZIPInputStream. Throws a {@code ZipException} if a GZIP format error
281 * occurs or the compression method used is unsupported.
287 private BufferedReader getGzipReader(InputStream inputStream)
290 BufferedReader inData = new BufferedReader(
291 new InputStreamReader(new GZIPInputStream(inputStream)));
299 * Tries to read from the given URL. If successful, saves a reader to the
300 * response in field {@code dataIn}, otherwise (on exception, or HTTP response
301 * status not 200), throws an exception.
303 * If the response status includes
306 * Content-Type : application/x-gzip
309 * then tries to read as gzipped content.
312 * @throws IOException
313 * @throws MalformedURLException
315 private void checkURLSource(String urlStr)
316 throws IOException, MalformedURLException
318 errormessage = "URL NOT FOUND";
319 URL url = new URL(urlStr);
320 URLConnection _conn = HttpUtils.openConnection(url);
321 if (_conn instanceof HttpURLConnection)
323 HttpURLConnection conn = HttpUtils
324 .followConnection((HttpURLConnection) _conn);
325 int rc = conn.getResponseCode();
326 if (rc != HttpURLConnection.HTTP_OK)
328 throw new FileNotFoundException("Response status from " + urlStr
329 + " was " + conn.getResponseCode());
337 dataIn = checkForGzipStream(_conn.getInputStream());
339 } catch (IOException ex)
341 throw new IOException("Failed to handle non-HTTP URI stream", ex);
342 } catch (Exception ex)
344 throw new IOException(
345 "Failed to determine type of input stream for given URI",
350 String encoding = _conn.getContentEncoding();
351 String contentType = _conn.getContentType();
352 boolean isgzipped = "application/x-gzip".equalsIgnoreCase(contentType)
353 || contentType.endsWith("gzip") || "gzip".equals(encoding);
355 InputStream inputStream = _conn.getInputStream();
360 dataIn = getGzipReader(inputStream);
362 } catch (Exception e1)
364 throw new IOException(MessageManager
365 .getString("exception.failed_to_resolve_gzip_stream"), e);
370 dataIn = new BufferedReader(new InputStreamReader(inputStream));
376 * sets the suffix string (if any) and returns remainder (if suffix was
380 * @return truncated fileStr or null
382 private String extractSuffix(String fileStr)
384 // first check that there wasn't a suffix string tagged on.
385 int sfpos = fileStr.lastIndexOf(suffixSeparator);
386 if (sfpos > -1 && sfpos < fileStr.length() - 1)
388 suffix = fileStr.substring(sfpos + 1);
389 // jalview.bin.Console.errPrintln("DEBUG: Found Suffix:"+suffix);
390 return fileStr.substring(0, sfpos);
396 * not for general use, creates a fileParse object for an existing reader with
397 * configurable values for the origin and the type of the source
399 public FileParse(BufferedReader source, String originString,
400 DataSourceType sourceType)
402 dataSourceType = sourceType;
405 dataName = originString;
409 if (dataIn.markSupported())
411 dataIn.mark(READAHEAD_LIMIT);
413 } catch (IOException q)
420 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
421 * types of sources that are handled.
424 * - datasource locator/content as File or String
426 * - protocol of source
427 * @throws MalformedURLException
428 * @throws IOException
430 public FileParse(Object file, DataSourceType sourceType)
431 throws MalformedURLException, FileNotFoundException, IOException
433 if (file instanceof File)
435 parse((File) file, ((File) file).getPath(), sourceType, true);
439 parse(null, file.toString(), sourceType, false);
443 private void parse(File file, String fileStr, DataSourceType sourceType,
444 boolean isFileObject) throws FileNotFoundException, IOException
446 bytes = Platform.getFileBytes(file);
447 dataSourceType = sourceType;
449 boolean filenotfound = false;
451 if (sourceType == DataSourceType.FILE)
456 // this will be from JavaScript
458 dataIn = new BufferedReader(
459 new InputStreamReader(new ByteArrayInputStream(bytes)));
462 else if (checkFileSource(fileStr))
464 String suffixLess = extractSuffix(fileStr);
465 if (suffixLess != null)
467 if (checkFileSource(suffixLess))
469 throw new IOException(MessageManager.formatMessage(
470 "exception.problem_opening_file_also_tried",
472 { inFile.getName(), suffixLess, errormessage }));
477 throw new IOException(MessageManager.formatMessage(
478 "exception.problem_opening_file", new String[]
479 { inFile.getName(), errormessage }));
483 else if (sourceType == DataSourceType.RELATIVE_URL)
485 // BH 2018 hack for no support for access-origin
486 bytes = Platform.getFileAsBytes(fileStr);
487 dataIn = new BufferedReader(
488 new InputStreamReader(new ByteArrayInputStream(bytes)));
492 else if (sourceType == DataSourceType.URL)
498 checkURLSource(fileStr);
499 if (suffixSeparator == '#')
501 extractSuffix(fileStr); // URL lref is stored for later reference.
503 } catch (IOException e)
505 String suffixLess = extractSuffix(fileStr);
506 if (suffixLess == null)
508 if (e instanceof FileNotFoundException
509 || e instanceof UnknownHostException
510 || e instanceof SocketTimeoutException)
512 errormessage = "File at URL '" + fileStr + "' not found";
521 checkURLSource(suffixLess);
522 } catch (IOException e2)
524 errormessage = "BAD URL WITH OR WITHOUT SUFFIX '" + fileStr
526 if (e instanceof FileNotFoundException
527 || e instanceof UnknownHostException
528 || e instanceof SocketTimeoutException)
532 throw (e); // just pass back original - everything was wrong.
536 } catch (Exception e)
538 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
539 + e.getMessage() + ")";
543 else if (sourceType == DataSourceType.PASTE)
545 errormessage = "PASTE INACCESSIBLE!";
546 dataIn = new BufferedReader(new StringReader(fileStr));
549 else if (sourceType == DataSourceType.CLASSLOADER)
551 errormessage = "RESOURCE CANNOT BE LOCATED";
552 InputStream is = getClass().getResourceAsStream("/" + fileStr);
555 String suffixLess = extractSuffix(fileStr);
556 if (suffixLess != null)
558 is = getClass().getResourceAsStream("/" + suffixLess);
563 dataIn = new BufferedReader(new InputStreamReader(is));
573 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
574 + (sourceType != null ? sourceType : "null") + "'";
577 if (dataIn == null || error)
579 // pass up the reason why we have no source to read from
582 throw new FileNotFoundException(MessageManager
583 .formatMessage("label.url_not_found", new String[]
586 throw new IOException(MessageManager.formatMessage(
587 "exception.failed_to_read_data_from_source", new String[]
591 dataIn.mark(READAHEAD_LIMIT);
595 * mark the current position in the source as start for the purposes of it
596 * being analysed by IdentifyFile().identify
598 * @throws IOException
600 public void mark() throws IOException
604 dataIn.mark(READAHEAD_LIMIT);
608 throw new IOException(
609 MessageManager.getString("exception.no_init_source_stream"));
613 public String nextLine() throws IOException
617 return dataIn.readLine();
619 throw new IOException(MessageManager
620 .formatMessage("exception.invalid_source_stream", new String[]
626 * @return true if this FileParse is configured for Export only
628 public boolean isExporting()
630 return !error && dataIn == null;
635 * @return true if the data source is valid
637 public boolean isValid()
643 * closes the datasource and tidies up. source will be left in an error state
645 public void close() throws IOException
647 errormessage = "EXCEPTION ON CLOSE";
651 errormessage = "SOURCE IS CLOSED";
655 * Rewinds the datasource to the marked point if possible
660 public void reset(int bytesRead) throws IOException
662 if (bytesRead >= READAHEAD_LIMIT)
664 jalview.bin.Console.errPrintln(String.format(
665 "File reset error: read %d bytes but reset limit is %d",
666 bytesRead, READAHEAD_LIMIT));
668 if (dataIn != null && !error)
674 throw new IOException(MessageManager.getString(
675 "error.implementation_error_reset_called_for_invalid_source"));
681 * @return true if there is a warning for the user
683 public boolean hasWarningMessage()
685 return (warningMessage != null && warningMessage.length() > 0);
690 * @return empty string or warning message about file that was just parsed.
692 public String getWarningMessage()
694 return warningMessage;
697 public String getInFile()
701 return inFile.getAbsolutePath() + " (" + index + ")";
705 return "From Paste + (" + index + ")";
710 * @return the dataName
712 public String getDataName()
718 * set the (human readable) name or URI for this datasource
722 protected void setDataName(String dataname)
728 * get the underlying bufferedReader for this data source.
730 * @return null if no reader available
731 * @throws IOException
733 public Reader getReader()
735 if (dataIn != null) // Probably don't need to test for readiness &&
743 public AlignViewportI getViewport()
748 public void setViewport(AlignViewportI viewport)
750 this.viewport = viewport;
754 * @return the currently configured exportSettings for writing data.
756 public AlignExportSettingsI getExportSettings()
758 return exportSettings;
762 * Set configuration for export of data.
764 * @param exportSettings
765 * the exportSettings to set
767 public void setExportSettings(AlignExportSettingsI exportSettings)
769 this.exportSettings = exportSettings;
773 * method overridden by complex file exporter/importers which support
774 * exporting visualisation and layout settings for a view
778 public void configureForView(AlignmentViewPanel avpanel)
782 setViewport(avpanel.getAlignViewport());
784 // could also set export/import settings
788 * Returns the preferred feature colour configuration if there is one, else
793 public FeatureSettingsModelI getFeatureColourScheme()
798 public DataSourceType getDataSourceType()
800 return dataSourceType;
804 * Returns a buffered reader for the input object. Returns null, or throws
805 * IOException, on failure.
808 * a File, or a String which is a name of a file
811 * @throws IOException
813 public BufferedReader getBufferedReader(Object file,
814 DataSourceType sourceType) throws IOException
816 BufferedReader in = null;
822 if (file instanceof String)
824 return new BufferedReader(new FileReader((String) file));
826 bytes = Platform.getFileBytes((File) file);
829 return new BufferedReader(
830 new InputStreamReader(new ByteArrayInputStream(bytes)));
832 return new BufferedReader(new FileReader((File) file));
834 URL url = new URL(file.toString());
835 in = new BufferedReader(
836 new InputStreamReader(HttpUtils.openStream(url)));
838 case RELATIVE_URL: // JalviewJS only
839 bytes = Platform.getFileAsBytes(file.toString());
842 in = new BufferedReader(
843 new InputStreamReader(new ByteArrayInputStream(bytes)));
847 in = new BufferedReader(new StringReader(file.toString()));
850 InputStream is = getClass().getResourceAsStream("/" + file);
853 in = new BufferedReader(new InputStreamReader(is));