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 jalview.api.AlignExportSettingI;
24 import jalview.api.AlignViewportI;
25 import jalview.api.AlignmentViewPanel;
26 import jalview.api.FeatureSettingsModelI;
27 import jalview.util.MessageManager;
29 import java.io.BufferedInputStream;
30 import java.io.BufferedReader;
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.io.Reader;
37 import java.io.StringReader;
38 import java.net.HttpURLConnection;
39 import java.net.MalformedURLException;
41 import java.net.URLConnection;
42 import java.util.zip.GZIPInputStream;
46 * implements a random access wrapper around a particular datasource, for
47 * passing to identifyFile and AlignFile objects.
49 public class FileParse
51 protected static final String SPACE = " ";
53 protected static final String TAB = "\t";
56 * text specifying source of data. usually filename or url.
58 private String dataName = "unknown source";
60 public File inFile = null;
64 * a viewport associated with the current file operation. May be null. May
65 * move to different object.
67 private AlignViewportI viewport;
70 * specific settings for exporting data from the current context
72 private AlignExportSettingI exportSettings;
75 * sequence counter for FileParse object created from same data source
80 * separator for extracting specific 'frame' of a datasource for formats that
81 * support multiple records (e.g. BLC, Stockholm, etc)
83 protected char suffixSeparator = '#';
86 * character used to write newlines
88 protected String newline = System.getProperty("line.separator");
90 public void setNewlineString(String nl)
95 public String getNewlineString()
101 * '#' separated string tagged on to end of filename or url that was clipped
102 * off to resolve to valid filename
104 protected String suffix = null;
106 protected DataSourceType dataSourceType = null;
108 protected BufferedReader dataIn = null;
110 protected String errormessage = "UNINITIALISED SOURCE";
112 protected boolean error = true;
114 protected String warningMessage = null;
117 * size of readahead buffer used for when initial stream position is marked.
119 final int READAHEAD_LIMIT = 2048;
126 * Create a new FileParse instance reading from the same datasource starting
127 * at the current position. WARNING! Subsequent reads from either object will
128 * affect the read position of the other, but not the error state.
132 public FileParse(FileParse from) throws IOException
136 throw new Error(MessageManager
137 .getString("error.implementation_error_null_fileparse"));
143 index = ++from.index;
144 inFile = from.inFile;
145 suffixSeparator = from.suffixSeparator;
146 suffix = from.suffix;
147 errormessage = from.errormessage; // inherit potential error messages
148 error = false; // reset any error condition.
149 dataSourceType = from.dataSourceType;
150 dataIn = from.dataIn;
155 dataName = from.dataName;
159 * Attempt to open a file as a datasource. Sets error and errormessage if
160 * fileStr was invalid.
163 * @return this.error (true if the source was invalid)
165 private boolean checkFileSource(String fileStr) throws IOException
168 this.inFile = new File(fileStr);
169 // check to see if it's a Jar file in disguise.
170 if (!inFile.exists())
172 errormessage = "FILE NOT FOUND";
175 if (!inFile.canRead())
177 errormessage = "FILE CANNOT BE OPENED FOR READING";
180 if (inFile.isDirectory())
182 // this is really a 'complex' filetype - but we don't handle directory
184 errormessage = "FILE IS A DIRECTORY";
191 dataIn = checkForGzipStream(new FileInputStream(fileStr));
193 } catch (Exception x)
195 warningMessage = "Failed to resolve " + fileStr
196 + " as a data source. (" + x.getMessage() + ")";
197 // x.printStackTrace();
206 * Recognise the 2-byte magic header for gzip streams
208 * https://recalll.co/ask/v/topic/java-How-to-check-if-InputStream-is-Gzipped/555aadd62bd27354438b90f6
210 * @param bytes - at least two bytes
213 private static boolean isGzipStream(byte[] bytes) {
214 int head = ((int) bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
215 return (GZIPInputStream.GZIP_MAGIC == head);
219 * Returns a Reader for the given input after wrapping it in a buffered input
220 * stream, and then checking if it needs to be wrapped by a GZipInputStream
225 private BufferedReader checkForGzipStream(InputStream input) throws Exception {
227 // NB: stackoverflow https://stackoverflow.com/questions/4818468/how-to-check-if-inputstream-is-gzipped
228 // could use a PushBackInputStream rather than a BufferedInputStream
230 BufferedInputStream bufinput;
231 if (!input.markSupported()) {
232 bufinput= new BufferedInputStream(input,16);
236 byte[] bytes=input.readNBytes(2);
238 if (bytes.length==2 && isGzipStream(bytes)) {
239 return getGzipReader(input);
241 // return a buffered reader for the stream.
242 InputStreamReader isReader= new InputStreamReader(input);
243 BufferedReader toReadFrom=new BufferedReader(isReader);
247 * Returns a {@code BufferedReader} which wraps the input stream with a
248 * GZIPInputStream. Throws a {@code ZipException} if a GZIP format error
249 * occurs or the compression method used is unsupported.
255 private BufferedReader getGzipReader(InputStream inputStream)
258 BufferedReader inData = new BufferedReader(
259 new InputStreamReader(new GZIPInputStream(inputStream)));
267 * Tries to read from the given URL. If successful, saves a reader to the
268 * response in field {@code dataIn}, otherwise (on exception, or HTTP response
269 * status not 200), throws an exception.
271 * If the response status includes
274 * Content-Type : application/x-gzip
277 * then tries to read as gzipped content.
280 * @throws IOException
281 * @throws MalformedURLException
283 private void checkURLSource(String urlStr)
284 throws IOException, MalformedURLException
286 errormessage = "URL NOT FOUND";
287 URL url = new URL(urlStr);
288 URLConnection _conn = url.openConnection();
289 if (_conn instanceof HttpURLConnection)
291 HttpURLConnection conn = (HttpURLConnection) _conn;
292 int rc = conn.getResponseCode();
293 if (rc != HttpURLConnection.HTTP_OK)
295 throw new IOException(
296 "Response status from " + urlStr + " was " + rc);
300 dataIn = checkForGzipStream(_conn.getInputStream());
302 } catch (IOException ex)
304 throw new IOException("Failed to handle non-HTTP URI stream",ex);
305 } catch (Exception ex)
307 throw new IOException("Failed to determine type of input stream for given URI",ex);
311 String encoding = _conn.getContentEncoding();
312 String contentType = _conn.getContentType();
313 boolean isgzipped = "application/x-gzip".equalsIgnoreCase(contentType)
314 || "gzip".equals(encoding);
316 InputStream inputStream = _conn.getInputStream();
321 dataIn = getGzipReader(inputStream);
323 } catch (Exception e1)
325 throw new IOException(MessageManager
326 .getString("exception.failed_to_resolve_gzip_stream"), e);
331 dataIn = new BufferedReader(new InputStreamReader(inputStream));
337 * sets the suffix string (if any) and returns remainder (if suffix was
341 * @return truncated fileStr or null
343 private String extractSuffix(String fileStr)
345 // first check that there wasn't a suffix string tagged on.
346 int sfpos = fileStr.lastIndexOf(suffixSeparator);
347 if (sfpos > -1 && sfpos < fileStr.length() - 1)
349 suffix = fileStr.substring(sfpos + 1);
350 // System.err.println("DEBUG: Found Suffix:"+suffix);
351 return fileStr.substring(0, sfpos);
357 * not for general use, creates a fileParse object for an existing reader with
358 * configurable values for the origin and the type of the source
360 public FileParse(BufferedReader source, String originString,
361 DataSourceType sourceType)
363 dataSourceType = sourceType;
366 dataName = originString;
370 if (dataIn.markSupported())
372 dataIn.mark(READAHEAD_LIMIT);
374 } catch (IOException q)
381 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
382 * types of sources that are handled.
385 * - datasource locator/content
387 * - protocol of source
388 * @throws MalformedURLException
389 * @throws IOException
391 public FileParse(String fileStr, DataSourceType sourceType)
392 throws MalformedURLException, IOException
394 this.dataSourceType = sourceType;
397 if (sourceType == DataSourceType.FILE)
399 if (checkFileSource(fileStr))
401 String suffixLess = extractSuffix(fileStr);
402 if (suffixLess != null)
404 if (checkFileSource(suffixLess))
406 throw new IOException(MessageManager.formatMessage(
407 "exception.problem_opening_file_also_tried",
409 { inFile.getName(), suffixLess, errormessage }));
414 throw new IOException(MessageManager.formatMessage(
415 "exception.problem_opening_file", new String[]
416 { inFile.getName(), errormessage }));
420 else if (sourceType == DataSourceType.URL)
426 checkURLSource(fileStr);
427 if (suffixSeparator == '#')
429 extractSuffix(fileStr); // URL lref is stored for later reference.
431 } catch (IOException e)
433 String suffixLess = extractSuffix(fileStr);
434 if (suffixLess == null)
442 checkURLSource(suffixLess);
443 } catch (IOException e2)
445 errormessage = "BAD URL WITH OR WITHOUT SUFFIX";
446 throw (e); // just pass back original - everything was wrong.
450 } catch (Exception e)
452 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
453 + e.getMessage() + ")";
457 else if (sourceType == DataSourceType.PASTE)
459 errormessage = "PASTE INACCESSIBLE!";
460 dataIn = new BufferedReader(new StringReader(fileStr));
463 else if (sourceType == DataSourceType.CLASSLOADER)
465 errormessage = "RESOURCE CANNOT BE LOCATED";
466 java.io.InputStream is = getClass()
467 .getResourceAsStream("/" + fileStr);
470 String suffixLess = extractSuffix(fileStr);
471 if (suffixLess != null)
473 is = getClass().getResourceAsStream("/" + suffixLess);
478 dataIn = new BufferedReader(new java.io.InputStreamReader(is));
488 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
489 + (sourceType != null ? sourceType : "null") + "'";
492 if (dataIn == null || error)
494 // pass up the reason why we have no source to read from
495 throw new IOException(MessageManager.formatMessage(
496 "exception.failed_to_read_data_from_source", new String[]
500 dataIn.mark(READAHEAD_LIMIT);
504 * mark the current position in the source as start for the purposes of it
505 * being analysed by IdentifyFile().identify
507 * @throws IOException
509 public void mark() throws IOException
513 dataIn.mark(READAHEAD_LIMIT);
517 throw new IOException(
518 MessageManager.getString("exception.no_init_source_stream"));
522 public String nextLine() throws IOException
526 return dataIn.readLine();
528 throw new IOException(MessageManager
529 .formatMessage("exception.invalid_source_stream", new String[]
535 * @return true if this FileParse is configured for Export only
537 public boolean isExporting()
539 return !error && dataIn == null;
544 * @return true if the data source is valid
546 public boolean isValid()
552 * closes the datasource and tidies up. source will be left in an error state
554 public void close() throws IOException
556 errormessage = "EXCEPTION ON CLOSE";
560 errormessage = "SOURCE IS CLOSED";
564 * Rewinds the datasource to the marked point if possible
569 public void reset(int bytesRead) throws IOException
571 if (bytesRead >= READAHEAD_LIMIT)
573 System.err.println(String.format(
574 "File reset error: read %d bytes but reset limit is %d",
575 bytesRead, READAHEAD_LIMIT));
577 if (dataIn != null && !error)
583 throw new IOException(MessageManager.getString(
584 "error.implementation_error_reset_called_for_invalid_source"));
590 * @return true if there is a warning for the user
592 public boolean hasWarningMessage()
594 return (warningMessage != null && warningMessage.length() > 0);
599 * @return empty string or warning message about file that was just parsed.
601 public String getWarningMessage()
603 return warningMessage;
606 public String getInFile()
610 return inFile.getAbsolutePath() + " (" + index + ")";
614 return "From Paste + (" + index + ")";
619 * @return the dataName
621 public String getDataName()
627 * set the (human readable) name or URI for this datasource
631 protected void setDataName(String dataname)
637 * get the underlying bufferedReader for this data source.
639 * @return null if no reader available
640 * @throws IOException
642 public Reader getReader()
644 if (dataIn != null) // Probably don't need to test for readiness &&
652 public AlignViewportI getViewport()
657 public void setViewport(AlignViewportI viewport)
659 this.viewport = viewport;
663 * @return the currently configured exportSettings for writing data.
665 public AlignExportSettingI getExportSettings()
667 return exportSettings;
671 * Set configuration for export of data.
673 * @param exportSettings
674 * the exportSettings to set
676 public void setExportSettings(AlignExportSettingI exportSettings)
678 this.exportSettings = exportSettings;
682 * method overridden by complex file exporter/importers which support
683 * exporting visualisation and layout settings for a view
687 public void configureForView(AlignmentViewPanel avpanel)
691 setViewport(avpanel.getAlignViewport());
693 // could also set export/import settings
697 * Returns the preferred feature colour configuration if there is one, else
702 public FeatureSettingsModelI getFeatureColourScheme()
707 public DataSourceType getDataSourceType()
709 return dataSourceType;