private void parseProperties() throws IOException { String line; stream.seek(0); propertiesByteSize = 0; long posInStream = 0l; while ((line = stream.readLine()) != null) { if (!line.startsWith("#")) { stream.seek(posInStream); break; } propertiesByteSize += (stream.getStreamPosition() - posInStream); posInStream = stream.getStreamPosition(); line = line.substring(1); int pos = line.indexOf('='); if (pos == -1) { throw new IOException("Missing '=' in '" + line + "'"); } String name = line.substring(0, pos).trim(); if (name.isEmpty()) { throw new IOException("Empty property name in '" + line + "'"); } String value = line.substring(pos + 1).trim(); try { if (contains(Constants.CRS_IDENTIFIERS, name) && crs != null) { crs = CRS.parseWKT(value); } } catch (FactoryException e) { throw new IOException(e); } properties.put(name, value); } propertiesParsed = true; }
private void skipToLine(long lineOffset) throws IOException { if (bytePositionForOffset.containsKey(lineOffset)) { stream.seek(bytePositionForOffset.get(lineOffset)); return; } Map.Entry<Long, Long> entry = getBestOffset(lineOffset); stream.seek(entry.getValue()); long linesToSkip = lineOffset - entry.getKey(); for (int i = 0; i < linesToSkip; i++) { stream.readLine(); } bytePositionForOffset.put(lineOffset, stream.getStreamPosition()); }
@Override public void checkReadingFirstRecord() throws IOException { skipToLine(0); final String line = stream.readLine(); final String[] tokens = getTokens(line); for (int i = 0; i < converters.length; i++) { Converter<?> converter = converters[i]; try { converter.parse(tokens[i + (hasFeatureId ? 1 : 0)]); } catch (ConversionException e) { throw new IOException(e); } } }
@Override public void parseRecords(int offset, int numRecords) throws IOException { featureCollection = new ListFeatureCollection(simpleFeatureType); SimpleFeatureBuilder builder = new SimpleFeatureBuilder(simpleFeatureType); skipToLine(offset); String line; long featureCount = offset; while ((numRecords == -1 || featureCount < offset + numRecords) && (line = stream.readLine()) != null) { String[] tokens = getTokens(line); if (tokens == null) { break; } int expectedTokenCount = simpleFeatureType.getAttributeCount(); expectedTokenCount += hasFeatureId ? 1 : 0; if (tokens.length != expectedTokenCount) { continue; } builder.reset(); String featureId = "" + featureCount++; for (int i = 0; i < tokens.length; i++) { String token = tokens[i]; if (i == 0 && hasFeatureId) { featureId = token; } else { try { Object value = null; int currentIndex = i; currentIndex -= hasFeatureId ? 1 : 0; if (!VectorDataNodeIO.NULL_TEXT.equals(token)) { value = converters[currentIndex].parse(token); } builder.set(simpleFeatureType.getDescriptor(currentIndex).getLocalName(), value); } catch (ConversionException e) { BeamLogManager.getSystemLogger() .warning(String.format("Problem in '%s': %s", csv.getPath(), e.getMessage())); } } } SimpleFeature simpleFeature = builder.buildFeature(featureId); featureCollection.add(simpleFeature); bytePositionForOffset.put(featureCount, stream.getStreamPosition()); } recordsParsed = true; }
@Override public Object[] parseRecords(final int offset, final int numRecords, final String rowName) throws IOException { AttributeDescriptor attributeDescriptor = simpleFeatureType.getDescriptor(rowName); int expectedTokenCount = simpleFeatureType.getAttributeCount(); expectedTokenCount += hasFeatureId ? 1 : 0; int rowIndex = simpleFeatureType.getAttributeDescriptors().indexOf(attributeDescriptor); int tokenIndex = rowIndex + (hasFeatureId ? 1 : 0); List<Object> values = new ArrayList<>(numRecords); skipToLine(offset); String line; long featureCount = offset; while ((numRecords == -1 || featureCount < offset + numRecords) && (line = stream.readLine()) != null) { String[] tokens = getTokens(line); if (tokens == null) { break; } if (tokens.length != expectedTokenCount) { continue; } featureCount++; String token = tokens[tokenIndex]; try { Object value = null; if (!VectorDataNodeIO.NULL_TEXT.equals(token)) { value = converters[rowIndex].parse(token); } values.add(value); } catch (ConversionException e) { BeamLogManager.getSystemLogger() .warning(String.format("Problem in '%s': %s", csv.getPath(), e.getMessage())); } bytePositionForOffset.put(featureCount, stream.getStreamPosition()); } return values.toArray(); }
private void parseHeader() throws IOException { if (!propertiesParsed) { throw new IllegalStateException("Properties need to be parsed before header."); } stream.seek(propertiesByteSize); String line; long posInStream = stream.getStreamPosition(); while ((line = stream.readLine()) != null) { if (line.startsWith("#")) { propertiesByteSize += (stream.getStreamPosition() - posInStream); posInStream = stream.getStreamPosition(); continue; } headerByteSize += (stream.getStreamPosition() - posInStream); final String separator = properties.get("separator") != null ? properties.get("separator") : Constants.DEFAULT_SEPARATOR; createFeatureType(line.split(separator)); break; } }
@Override public String readLine() throws IOException { return iis.readLine(); }