List of usage examples for java.io DataOutputStream writeDouble
public final void writeDouble(double v) throws IOException
long
using the doubleToLongBits
method in class Double
, and then writes that long
value to the underlying output stream as an 8-byte quantity, high byte first. From source file:org.bdval.cache.TableCache.java
/** * Given the specified paramters, save the table to the cache. * * @param splitId The split id//from w w w.jav a 2 s.c o m * @param splitType The Split type * @param datasetName The dataset name * @param table the Table to save. */ public void saveTableToCache(final int splitId, final String splitType, final String datasetName, final Table table) { DataOutputStream dataOutput = null; try { if (!checkTableConfiguration(table)) { return; } final int numColumns = table.getColumnNumber(); final File cachedTableFile = getCachedTableFile(splitId, splitType, datasetName); dataOutput = new DataOutputStream(new FastBufferedOutputStream(new FileOutputStream(cachedTableFile))); // Write the number of columns dataOutput.writeInt(numColumns); LOG.info("Writing " + numColumns + " columns"); int numWritten = 0; for (int i = 0; i < numColumns; i++) { // For each column write if it is a "d"ouble or "s"tring column final String id = table.getIdentifier(i); if (table.getType(i) == String.class) { dataOutput.writeUTF("s"); dataOutput.writeUTF(id); final String[] stringColumnData = table.getStrings(id); final int numStrings = stringColumnData.length; dataOutput.writeInt(numStrings); for (final String stringColumnItem : stringColumnData) { // Each String dataOutput.writeUTF(stringColumnItem); } numWritten++; } else if (table.getType(i) == double.class) { dataOutput.writeUTF("d"); dataOutput.writeUTF(id); final double[] doubleColumnData = table.getDoubles(id); final int numDoubles = doubleColumnData.length; dataOutput.writeInt(numDoubles); for (final double doubleColumnItem : doubleColumnData) { // Each double dataOutput.writeDouble(doubleColumnItem); } numWritten++; } } dataOutput.flush(); LOG.info("Wrote " + numWritten + " columns"); LOG.info("++ SAVED TABLE TO CACHE for split-id=" + splitId + ", split-type=" + splitType + ", dataset-name=" + datasetName); } catch (IOException e) { LOG.error("Cannot cache table. ", e); } catch (InvalidColumnException e) { LOG.error("Invalid table data", e); } finally { IOUtils.closeQuietly(dataOutput); } }
From source file:org.apache.jmeter.protocol.mqtt.client.MqttPublisher.java
public byte[] createRandomPayload(String Seed, String min, String max, String type_random, String useTimeStamp, String useNumSeq, String type_value, String format, String charset) throws IOException, NumberFormatException { ByteArrayOutputStream b = new ByteArrayOutputStream(); DataOutputStream d = new DataOutputStream(b); // flags byte flags = 0x00; if ("TRUE".equals(useTimeStamp)) flags |= 0x80;/* w w w . jav a2 s .co m*/ if ("TRUE".equals(useNumSeq)) flags |= 0x40; if (MQTTPublisherGui.INT.equals(type_value)) flags |= 0x20; if (MQTTPublisherGui.LONG.equals(type_value)) flags |= 0x10; if (MQTTPublisherGui.FLOAT.equals(type_value)) flags |= 0x08; if (MQTTPublisherGui.DOUBLE.equals(type_value)) flags |= 0x04; if (MQTTPublisherGui.STRING.equals(type_value)) flags |= 0x02; if (!"TEXT".equals(type_value)) { d.writeByte(flags); } // TimeStamp if ("TRUE".equals(useTimeStamp)) { Date date = new java.util.Date(); d.writeLong(date.getTime()); } // Number Sequence if ("TRUE".equals(useNumSeq)) { d.writeInt(numSeq++); } // Value if (MQTTPublisherGui.PSEUDO.equals(type_random)) { generator.setSeed(Long.parseLong(Seed)); if (MQTTPublisherGui.INT.equals(type_value)) { d.writeInt( generator.nextInt(Integer.parseInt(max) - Integer.parseInt(min)) + Integer.parseInt(min)); } else if (MQTTPublisherGui.LONG.equals(type_value)) { long Max = Long.parseLong(max); long Min = Long.parseLong(min); d.writeLong((Math.abs(generator.nextLong() % (Max - Min)) + Min)); } else if (MQTTPublisherGui.DOUBLE.equals(type_value)) { double Max = Double.parseDouble(max); double Min = Double.parseDouble(min); d.writeDouble((Min + (Max - Min) * generator.nextDouble())); } else if (MQTTPublisherGui.FLOAT.equals(type_value)) { float Max = Float.parseFloat(max); float Min = Float.parseFloat(min); d.writeFloat((Min + (Max - Min) * generator.nextFloat())); } } else if (MQTTPublisherGui.SECURE.equals(type_random)) { secureGenerator.setSeed(Long.parseLong(Seed)); if (MQTTPublisherGui.INT.equals(type_value)) { d.writeInt(secureGenerator.nextInt(Integer.parseInt(max) - Integer.parseInt(min)) + Integer.parseInt(min)); } else if (MQTTPublisherGui.LONG.equals(type_value)) { long Max = Long.parseLong(max); long Min = Long.parseLong(min); d.writeLong((Math.abs(secureGenerator.nextLong() % (Max - Min)) + Min)); } else if (MQTTPublisherGui.DOUBLE.equals(type_value)) { double Max = Double.parseDouble(max); double Min = Double.parseDouble(min); d.writeDouble((Min + (Max - Min) * secureGenerator.nextDouble())); } else if (MQTTPublisherGui.FLOAT.equals(type_value)) { float Max = Float.parseFloat(max); float Min = Float.parseFloat(min); d.writeFloat((Min + (Max - Min) * secureGenerator.nextFloat())); } } // Format: Encoding if (MQTTPublisherGui.BINARY.equals(format)) { BinaryCodec encoder = new BinaryCodec(); return encoder.encode(b.toByteArray()); } else if (MQTTPublisherGui.BASE64.equals(format)) { return Base64.encodeBase64(b.toByteArray()); } else if (MQTTPublisherGui.BINHEX.equals(format)) { Hex encoder = new Hex(); return encoder.encode(b.toByteArray()); } else if (MQTTPublisherGui.PLAIN_TEXT.equals(format)) { String s = new String(b.toByteArray(), charset); return s.getBytes(); } else return b.toByteArray(); }
From source file:se.llbit.chunky.renderer.scene.Scene.java
private synchronized void saveDump(RenderContext context, ProgressListener progressListener) { String fileName = name + ".dump"; DataOutputStream out = null; try {//from ww w . j a v a2s . c o m String task = "Saving render dump"; progressListener.setProgress(task, 1, 0, 2); Log.info("Saving render dump " + fileName); out = new DataOutputStream(new GZIPOutputStream(context.getSceneFileOutputStream(fileName))); out.writeInt(width); out.writeInt(height); out.writeInt(spp); out.writeLong(renderTime); for (int x = 0; x < width; ++x) { progressListener.setProgress(task, x + 1, 0, width); for (int y = 0; y < height; ++y) { out.writeDouble(samples[(y * width + x) * 3 + 0]); out.writeDouble(samples[(y * width + x) * 3 + 1]); out.writeDouble(samples[(y * width + x) * 3 + 2]); } } Log.info("Render dump saved"); } catch (IOException e) { Log.warn("IO exception while saving render dump!", e); } finally { if (out != null) { try { out.close(); } catch (IOException e) { } } } }
From source file:au.org.ala.spatial.util.RecordsSmall.java
private void makeSmallFile(String filename) throws Exception { FileWriter outputSpecies = new FileWriter(filename + "records.csv.small.species"); DataOutputStream outputPoints = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(filename + "records.csv.small.points"))); DataOutputStream outputPointsToSpecies = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(filename + "records.csv.small.pointsToSpecies"))); Map<String, Integer> lsidMap = new HashMap<String, Integer>(200000); byte start = 0; BufferedReader br = new BufferedReader(new FileReader(filename + "records.csv")); int[] header = new int[3]; int row = start; int currentCount = 0; String[] line = new String[3]; String rawline;/*from w w w. ja va 2s . co m*/ while ((rawline = br.readLine()) != null) { currentCount++; int p1 = rawline.indexOf(44); int p2 = rawline.indexOf(44, p1 + 1); if (p1 >= 0 && p2 >= 0) { line[0] = rawline.substring(0, p1); line[1] = rawline.substring(p1 + 1, p2); line[2] = rawline.substring(p2 + 1, rawline.length()); if (currentCount % 100000 == 0) { System.out.print("\rreading row: " + currentCount); } if (row == 0) { for (int e = 0; e < line.length; e++) { if (line[e].equals("names_and_lsid")) { header[0] = e; } if (line[e].equals("longitude")) { header[1] = e; } if (line[e].equals("latitude")) { header[2] = e; } } logger.debug("header: " + header[0] + "," + header[1] + "," + header[2]); } else if (line.length >= 3) { try { double lat = Double.parseDouble(line[header[2]]); double lng = Double.parseDouble(line[header[1]]); String species = line[header[0]]; Integer idx = lsidMap.get(species); if (idx == null) { idx = lsidMap.size(); lsidMap.put(species, idx); outputSpecies.write(species); outputSpecies.write("\n"); } outputPoints.writeDouble(lat); outputPoints.writeDouble(lng); outputPointsToSpecies.writeInt(idx); } catch (Exception e) { logger.error("failed to read records.csv row: " + row, e); } } row++; } } br.close(); outputPointsToSpecies.flush(); outputPointsToSpecies.close(); outputPoints.flush(); outputPoints.close(); outputSpecies.flush(); outputSpecies.close(); FileUtils.writeStringToFile(new File(filename + "records.csv.small.speciesCount"), String.valueOf(lsidMap.size())); }
From source file:org.ramadda.repository.database.DatabaseManager.java
/** * _more_//from w ww . j a va 2 s. co m * * @param dos _more_ * @param i _more_ * * @throws Exception _more_ */ private void writeDouble(DataOutputStream dos, Double i) throws Exception { if (i == null) { dos.writeDouble(Double.NaN); } else { dos.writeDouble(i.doubleValue()); } }
From source file:com.ricemap.spateDB.core.RTree.java
/** * Builds the RTree given a serialized list of elements. It uses the given * stockObject to deserialize these elements and build the tree. Also writes * the created tree to the disk directly. * // w w w. j a va 2s .com * @param elements * - serialization of elements to be written * @param offset * - index of the first element to use in the elements array * @param len * - number of bytes to user from the elements array * @param bytesAvailable * - size available (in bytes) to store the tree structures * @param dataOut * - an output to use for writing the tree to * @param fast_sort * - setting this to <code>true</code> allows the method to run * faster by materializing the offset of each element in the list * which speeds up the comparison. However, this requires an * additional 16 bytes per element. So, for each 1M elements, the * method will require an additional 16 M bytes (approximately). */ public void bulkLoadWrite(final byte[] element_bytes, final int offset, final int len, final int degree, DataOutput dataOut, final boolean fast_sort, final boolean columnarStorage) { try { columnar = columnarStorage; //TODO: the order of fields should be stable under Oracle JVM, but not guaranteed Field[] fields = stockObject.getClass().getDeclaredFields(); // Count number of elements in the given text int i_start = offset; final Text line = new Text(); while (i_start < offset + len) { int i_end = skipToEOL(element_bytes, i_start); // Extract the line without end of line character line.set(element_bytes, i_start, i_end - i_start - 1); stockObject.fromText(line); elementCount++; i_start = i_end; } LOG.info("Bulk loading an RTree with " + elementCount + " elements"); // It turns out the findBestDegree returns the best degree when the // whole // tree is loaded to memory when processed. However, as current // algorithms // process the tree while it's on disk, a higher degree should be // selected // such that a node fits one file block (assumed to be 4K). // final int degree = findBestDegree(bytesAvailable, elementCount); LOG.info("Writing an RTree with degree " + degree); int height = Math.max(1, (int) Math.ceil(Math.log(elementCount) / Math.log(degree))); int leafNodeCount = (int) Math.pow(degree, height - 1); if (elementCount < 2 * leafNodeCount && height > 1) { height--; leafNodeCount = (int) Math.pow(degree, height - 1); } int nodeCount = (int) ((Math.pow(degree, height) - 1) / (degree - 1)); int nonLeafNodeCount = nodeCount - leafNodeCount; // Keep track of the offset of each element in the text final int[] offsets = new int[elementCount]; final int[] ids = new int[elementCount]; final double[] ts = fast_sort ? new double[elementCount] : null; final double[] xs = fast_sort ? new double[elementCount] : null; final double[] ys = fast_sort ? new double[elementCount] : null; //initialize columnar data output ByteArrayOutputStream index_bos = new ByteArrayOutputStream(); DataOutputStream index_dos = new DataOutputStream(index_bos); ByteArrayOutputStream[] bos = new ByteArrayOutputStream[fields.length]; DataOutputStream[] dos = new DataOutputStream[fields.length]; for (int i = 0; i < bos.length; i++) { bos[i] = new ByteArrayOutputStream(); dos[i] = new DataOutputStream(bos[i]); } i_start = offset; line.clear(); for (int i = 0; i < elementCount; i++) { offsets[i] = i_start; ids[i] = i; int i_end = skipToEOL(element_bytes, i_start); if (xs != null) { // Extract the line with end of line character line.set(element_bytes, i_start, i_end - i_start - 1); stockObject.fromText(line); // Sample center of the shape ts[i] = (stockObject.getMBR().t1 + stockObject.getMBR().t2) / 2; xs[i] = (stockObject.getMBR().x1 + stockObject.getMBR().x2) / 2; ys[i] = (stockObject.getMBR().y1 + stockObject.getMBR().y2) / 2; //build columnar storage if (stockObject instanceof Point3d) { index_dos.writeDouble(ts[i]); index_dos.writeDouble(xs[i]); index_dos.writeDouble(ys[i]); } else { throw new RuntimeException("Indexing non-point shape with RTREE is not supported yet"); } for (int j = 0; j < fields.length; j++) { if (fields[j].getType().equals(Integer.TYPE)) { dos[j].writeInt(fields[j].getInt(stockObject)); } else if (fields[j].getType().equals(Double.TYPE)) { dos[j].writeDouble(fields[j].getDouble(stockObject)); } else if (fields[j].getType().equals(Long.TYPE)) { dos[j].writeLong(fields[j].getLong(stockObject)); } else { continue; //throw new RuntimeException("Field type is not supported yet"); } } } i_start = i_end; } index_dos.close(); for (int i = 0; i < dos.length; i++) { dos[i].close(); } /** A struct to store information about a split */ class SplitStruct extends Prism { /** Start and end index for this split */ int index1, index2; /** Direction of this split */ byte direction; /** Index of first element on disk */ int offsetOfFirstElement; static final byte DIRECTION_T = 0; static final byte DIRECTION_X = 1; static final byte DIRECTION_Y = 2; SplitStruct(int index1, int index2, byte direction) { this.index1 = index1; this.index2 = index2; this.direction = direction; } @Override public void write(DataOutput out) throws IOException { // if (columnarStorage) out.writeInt(index1); else out.writeInt(offsetOfFirstElement); super.write(out); } void partition(Queue<SplitStruct> toBePartitioned) { IndexedSortable sortableT; IndexedSortable sortableX; IndexedSortable sortableY; if (fast_sort) { // Use materialized xs[] and ys[] to do the comparisons sortableT = new IndexedSortable() { @Override public void swap(int i, int j) { // Swap ts double tempt = ts[i]; ts[i] = ts[j]; ts[j] = tempt; // Swap xs double tempx = xs[i]; xs[i] = xs[j]; xs[j] = tempx; // Swap ys double tempY = ys[i]; ys[i] = ys[j]; ys[j] = tempY; // Swap id int tempid = offsets[i]; offsets[i] = offsets[j]; offsets[j] = tempid; tempid = ids[i]; ids[i] = ids[j]; ids[j] = tempid; } @Override public int compare(int i, int j) { if (ts[i] < ts[j]) return -1; if (ts[i] > ts[j]) return 1; return 0; } }; sortableX = new IndexedSortable() { @Override public void swap(int i, int j) { // Swap ts double tempt = ts[i]; ts[i] = ts[j]; ts[j] = tempt; // Swap xs double tempx = xs[i]; xs[i] = xs[j]; xs[j] = tempx; // Swap ys double tempY = ys[i]; ys[i] = ys[j]; ys[j] = tempY; // Swap id int tempid = offsets[i]; offsets[i] = offsets[j]; offsets[j] = tempid; tempid = ids[i]; ids[i] = ids[j]; ids[j] = tempid; } @Override public int compare(int i, int j) { if (ts[i] < ts[j]) return -1; if (xs[i] < xs[j]) return -1; if (xs[i] > xs[j]) return 1; return 0; } }; sortableY = new IndexedSortable() { @Override public void swap(int i, int j) { // Swap ts double tempt = ts[i]; ts[i] = ts[j]; ts[j] = tempt; // Swap xs double tempx = xs[i]; xs[i] = xs[j]; xs[j] = tempx; // Swap ys double tempY = ys[i]; ys[i] = ys[j]; ys[j] = tempY; // Swap id int tempid = offsets[i]; offsets[i] = offsets[j]; offsets[j] = tempid; tempid = ids[i]; ids[i] = ids[j]; ids[j] = tempid; } @Override public int compare(int i, int j) { if (ys[i] < ys[j]) return -1; if (ys[i] > ys[j]) return 1; return 0; } }; } else { // No materialized xs and ys. Always deserialize objects // to compare sortableT = new IndexedSortable() { @Override public void swap(int i, int j) { // Swap id int tempid = offsets[i]; offsets[i] = offsets[j]; offsets[j] = tempid; tempid = ids[i]; ids[i] = ids[j]; ids[j] = tempid; } @Override public int compare(int i, int j) { // Get end of line int eol = skipToEOL(element_bytes, offsets[i]); line.set(element_bytes, offsets[i], eol - offsets[i] - 1); stockObject.fromText(line); double ti = (stockObject.getMBR().t1 + stockObject.getMBR().t2) / 2; eol = skipToEOL(element_bytes, offsets[j]); line.set(element_bytes, offsets[j], eol - offsets[j] - 1); stockObject.fromText(line); double tj = (stockObject.getMBR().t1 + stockObject.getMBR().t2) / 2; if (ti < tj) return -1; if (ti > tj) return 1; return 0; } }; sortableX = new IndexedSortable() { @Override public void swap(int i, int j) { // Swap id int tempid = offsets[i]; offsets[i] = offsets[j]; offsets[j] = tempid; tempid = ids[i]; ids[i] = ids[j]; ids[j] = tempid; } @Override public int compare(int i, int j) { // Get end of line int eol = skipToEOL(element_bytes, offsets[i]); line.set(element_bytes, offsets[i], eol - offsets[i] - 1); stockObject.fromText(line); double xi = (stockObject.getMBR().x1 + stockObject.getMBR().x2) / 2; eol = skipToEOL(element_bytes, offsets[j]); line.set(element_bytes, offsets[j], eol - offsets[j] - 1); stockObject.fromText(line); double xj = (stockObject.getMBR().x1 + stockObject.getMBR().x2) / 2; if (xi < xj) return -1; if (xi > xj) return 1; return 0; } }; sortableY = new IndexedSortable() { @Override public void swap(int i, int j) { // Swap id int tempid = offsets[i]; offsets[i] = offsets[j]; offsets[j] = tempid; tempid = ids[i]; ids[i] = ids[j]; ids[j] = tempid; } @Override public int compare(int i, int j) { int eol = skipToEOL(element_bytes, offsets[i]); line.set(element_bytes, offsets[i], eol - offsets[i] - 1); stockObject.fromText(line); double yi = (stockObject.getMBR().y1 + stockObject.getMBR().y2) / 2; eol = skipToEOL(element_bytes, offsets[j]); line.set(element_bytes, offsets[j], eol - offsets[j] - 1); stockObject.fromText(line); double yj = (stockObject.getMBR().y1 + stockObject.getMBR().y2) / 2; if (yi < yj) return -1; if (yi > yj) return 1; return 0; } }; } final IndexedSorter sorter = new QuickSort(); final IndexedSortable[] sortables = new IndexedSortable[3]; sortables[SplitStruct.DIRECTION_T] = sortableT; sortables[SplitStruct.DIRECTION_X] = sortableX; sortables[SplitStruct.DIRECTION_Y] = sortableY; sorter.sort(sortables[direction], index1, index2); // Partition into maxEntries partitions (equally) and // create a SplitStruct for each partition int i1 = index1; for (int iSplit = 0; iSplit < degree; iSplit++) { int i2 = index1 + (index2 - index1) * (iSplit + 1) / degree; SplitStruct newSplit; if (direction == 0) { newSplit = new SplitStruct(i1, i2, (byte) 1); } else if (direction == 1) { newSplit = new SplitStruct(i1, i2, (byte) 2); } else { newSplit = new SplitStruct(i1, i2, (byte) 0); } toBePartitioned.add(newSplit); i1 = i2; } } } // All nodes stored in level-order traversal Vector<SplitStruct> nodes = new Vector<SplitStruct>(); final Queue<SplitStruct> toBePartitioned = new LinkedList<SplitStruct>(); toBePartitioned.add(new SplitStruct(0, elementCount, SplitStruct.DIRECTION_X)); while (!toBePartitioned.isEmpty()) { SplitStruct split = toBePartitioned.poll(); if (nodes.size() < nonLeafNodeCount) { // This is a non-leaf split.partition(toBePartitioned); } nodes.add(split); } if (nodes.size() != nodeCount) { throw new RuntimeException( "Expected node count: " + nodeCount + ". Real node count: " + nodes.size()); } // Now we have our data sorted in the required order. Start building // the tree. // Store the offset of each leaf node in the tree FSDataOutputStream fakeOut = new FSDataOutputStream(new java.io.OutputStream() { // Null output stream @Override public void write(int b) throws IOException { // Do nothing } @Override public void write(byte[] b, int off, int len) throws IOException { // Do nothing } @Override public void write(byte[] b) throws IOException { // Do nothing } }, null, TreeHeaderSize + nodes.size() * NodeSize); for (int i_leaf = nonLeafNodeCount, i = 0; i_leaf < nodes.size(); i_leaf++) { nodes.elementAt(i_leaf).offsetOfFirstElement = (int) fakeOut.getPos(); if (i != nodes.elementAt(i_leaf).index1) throw new RuntimeException(); double t1, x1, y1, t2, x2, y2; // Initialize MBR to first object int eol = skipToEOL(element_bytes, offsets[i]); fakeOut.write(element_bytes, offsets[i], eol - offsets[i]); line.set(element_bytes, offsets[i], eol - offsets[i] - 1); stockObject.fromText(line); Prism mbr = stockObject.getMBR(); t1 = mbr.t1; x1 = mbr.x1; y1 = mbr.y1; t2 = mbr.t2; x2 = mbr.x2; y2 = mbr.y2; i++; while (i < nodes.elementAt(i_leaf).index2) { eol = skipToEOL(element_bytes, offsets[i]); fakeOut.write(element_bytes, offsets[i], eol - offsets[i]); line.set(element_bytes, offsets[i], eol - offsets[i] - 1); stockObject.fromText(line); mbr = stockObject.getMBR(); if (mbr.t1 < t1) t1 = mbr.t1; if (mbr.x1 < x1) x1 = mbr.x1; if (mbr.y1 < y1) y1 = mbr.y1; if (mbr.t2 > t2) t2 = mbr.t2; if (mbr.x2 > x2) x2 = mbr.x2; if (mbr.y2 > y2) y2 = mbr.y2; i++; } nodes.elementAt(i_leaf).set(t1, x1, y1, t2, x2, y2); } fakeOut.close(); fakeOut = null; // Calculate MBR and offsetOfFirstElement for non-leaves for (int i_node = nonLeafNodeCount - 1; i_node >= 0; i_node--) { int i_first_child = i_node * degree + 1; nodes.elementAt(i_node).offsetOfFirstElement = nodes.elementAt(i_first_child).offsetOfFirstElement; int i_child = 0; Prism mbr; mbr = nodes.elementAt(i_first_child + i_child); double t1 = mbr.t1; double x1 = mbr.x1; double y1 = mbr.y1; double t2 = mbr.t2; double x2 = mbr.x2; double y2 = mbr.y2; i_child++; while (i_child < degree) { mbr = nodes.elementAt(i_first_child + i_child); if (mbr.t1 < t1) t1 = mbr.t1; if (mbr.x1 < x1) x1 = mbr.x1; if (mbr.y1 < y1) y1 = mbr.y1; if (mbr.t2 > t2) t2 = mbr.t2; if (mbr.x2 > x2) x2 = mbr.x2; if (mbr.y2 > y2) y2 = mbr.y2; i_child++; } nodes.elementAt(i_node).set(t1, x1, y1, t2, x2, y2); } // Start writing the tree // write tree header (including size) // Total tree size. (== Total bytes written - 8 bytes for the size // itself) dataOut.writeInt(TreeHeaderSize + NodeSize * nodeCount + len); // Tree height dataOut.writeInt(height); // Degree dataOut.writeInt(degree); dataOut.writeInt(elementCount); //isColumnar dataOut.writeInt(columnarStorage ? 1 : 0); // write nodes for (SplitStruct node : nodes) { node.write(dataOut); } // write elements if (columnarStorage) { byte[] index_bs = index_bos.toByteArray(); byte[][] bss = new byte[bos.length][]; for (int i = 0; i < bss.length; i++) { bss[i] = bos[i].toByteArray(); } for (int element_i = 0; element_i < elementCount; element_i++) { //int eol = skipToEOL(element_bytes, offsets[element_i]); //dataOut.write(element_bytes, offsets[element_i], eol - offsets[element_i]); dataOut.write(index_bs, ids[element_i] * IndexUnitSize, IndexUnitSize); } for (int i = 0; i < fields.length; i++) { int fieldSize = 0; if (fields[i].getType().equals(Integer.TYPE)) { fieldSize = 4; } else if (fields[i].getType().equals(Long.TYPE)) { fieldSize = 8; } else if (fields[i].getType().equals(Double.TYPE)) { fieldSize = 8; } else { //throw new RuntimeException("Unsupported field type: " + fields[i].getType().getName()); continue; } for (int element_i = 0; element_i < elementCount; element_i++) { //int eol = skipToEOL(element_bytes, offsets[element_i]); //dataOut.write(element_bytes, offsets[element_i], eol - offsets[element_i]); dataOut.write(bss[i], ids[element_i] * fieldSize, fieldSize); } } } else { for (int element_i = 0; element_i < elementCount; element_i++) { int eol = skipToEOL(element_bytes, offsets[element_i]); dataOut.write(element_bytes, offsets[element_i], eol - offsets[element_i]); } } } catch (IOException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:org.apache.jackrabbit.core.persistence.bundle.util.BundleBinding.java
/** * Serializes a <code>PropertyState</code> to the data output stream * * @param out the output stream//from www. j a v a2 s.co m * @param state the property entry to store * @throws IOException if an I/O error occurs. */ public void writeState(DataOutputStream out, NodePropBundle.PropertyEntry state) throws IOException { // type & mod count out.writeInt(state.getType() | (state.getModCount() << 16)); // multiValued out.writeBoolean(state.isMultiValued()); // definitionId out.writeUTF(""); // values InternalValue[] values = state.getValues(); out.writeInt(values.length); // count for (int i = 0; i < values.length; i++) { InternalValue val = values[i]; switch (state.getType()) { case PropertyType.BINARY: BLOBFileValue blobVal = val.getBLOBFileValue(); long size = blobVal.getLength(); if (InternalValue.USE_DATA_STORE && dataStore != null) { int maxMemorySize = dataStore.getMinRecordLength() - 1; if (size < maxMemorySize) { writeSmallBinary(out, blobVal, state, i); } else { out.writeInt(BINARY_IN_DATA_STORE); try { val.store(dataStore); } catch (RepositoryException e) { String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " size=" + val.getBLOBFileValue().getLength(); log.error(msg, e); throw new IOException(msg); } out.writeUTF(val.toString()); } break; } // special handling required for binary value: // spool binary value to file in blob store if (size < 0) { log.warn("Blob has negative size. Potential loss of data. " + "id={} idx={}", state.getId(), String.valueOf(i)); out.writeInt(0); values[i] = InternalValue.create(new byte[0]); blobVal.discard(); } else if (size > minBlobSize) { out.writeInt(BINARY_IN_BLOB_STORE); String blobId = state.getBlobId(i); if (blobId == null) { try { InputStream in = blobVal.getStream(); try { blobId = blobStore.createId(state.getId(), i); blobStore.put(blobId, in, size); state.setBlobId(blobId, i); } finally { IOUtils.closeQuietly(in); } } catch (Exception e) { String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " size=" + size; log.error(msg, e); throw new IOException(msg); } try { // replace value instance with value // backed by resource in blob store and delete temp file if (blobStore instanceof ResourceBasedBLOBStore) { values[i] = InternalValue .create(((ResourceBasedBLOBStore) blobStore).getResource(blobId)); } else { values[i] = InternalValue.create(blobStore.get(blobId)); } } catch (Exception e) { log.error("Error while reloading blob. truncating. id=" + state.getId() + " idx=" + i + " size=" + size, e); values[i] = InternalValue.create(new byte[0]); } blobVal.discard(); } // store id of blob as property value out.writeUTF(blobId); // value } else { // delete evt. blob byte[] data = writeSmallBinary(out, blobVal, state, i); // replace value instance with value // backed by resource in blob store and delete temp file values[i] = InternalValue.create(data); blobVal.discard(); } break; case PropertyType.DOUBLE: out.writeDouble(val.getDouble()); break; case PropertyType.LONG: out.writeLong(val.getLong()); break; case PropertyType.BOOLEAN: out.writeBoolean(val.getBoolean()); break; case PropertyType.NAME: writeQName(out, val.getQName()); break; case PropertyType.REFERENCE: writeUUID(out, val.getUUID()); break; default: // because writeUTF(String) has a size limit of 64k, // we're using write(byte[]) instead byte[] bytes = val.toString().getBytes("UTF-8"); out.writeInt(bytes.length); // length of byte[] out.write(bytes); // byte[] } } }
From source file:org.apache.jackrabbit.core.persistence.bundle.util.BundleBinding.java
/** * Serializes a <code>PropertyState</code> to the data output stream * * @param out the output stream//from ww w. java2s .co m * @param state the property entry to store * @throws IOException if an I/O error occurs. */ public void writeState(DataOutputStream out, NodePropBundle.PropertyEntry state) throws IOException { // type & mod count out.writeInt(state.getType() | (state.getModCount() << 16)); // multiValued out.writeBoolean(state.isMultiValued()); // definitionId out.writeUTF(state.getPropDefId().toString()); // values InternalValue[] values = state.getValues(); out.writeInt(values.length); // count for (int i = 0; i < values.length; i++) { InternalValue val = values[i]; switch (state.getType()) { case PropertyType.BINARY: try { long size = val.getLength(); if (dataStore != null) { int maxMemorySize = dataStore.getMinRecordLength() - 1; if (size < maxMemorySize) { writeSmallBinary(out, val, state, i); } else { out.writeInt(BINARY_IN_DATA_STORE); val.store(dataStore); out.writeUTF(val.toString()); } break; } // special handling required for binary value: // spool binary value to file in blob store if (size < 0) { log.warn("Blob has negative size. Potential loss of data. " + "id={} idx={}", state.getId(), String.valueOf(i)); out.writeInt(0); values[i] = InternalValue.create(new byte[0]); val.discard(); } else if (size > minBlobSize) { out.writeInt(BINARY_IN_BLOB_STORE); String blobId = state.getBlobId(i); if (blobId == null) { try { InputStream in = val.getStream(); try { blobId = blobStore.createId(state.getId(), i); blobStore.put(blobId, in, size); state.setBlobId(blobId, i); } finally { IOUtils.closeQuietly(in); } } catch (Exception e) { String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " size=" + size; log.error(msg, e); throw new IOException(msg); } try { // replace value instance with value // backed by resource in blob store and delete temp file if (blobStore instanceof ResourceBasedBLOBStore) { values[i] = InternalValue .create(((ResourceBasedBLOBStore) blobStore).getResource(blobId)); } else { values[i] = InternalValue.create(blobStore.get(blobId)); } } catch (Exception e) { log.error("Error while reloading blob. truncating. id=" + state.getId() + " idx=" + i + " size=" + size, e); values[i] = InternalValue.create(new byte[0]); } val.discard(); } // store id of blob as property value out.writeUTF(blobId); // value } else { // delete evt. blob byte[] data = writeSmallBinary(out, val, state, i); // replace value instance with value // backed by resource in blob store and delete temp file values[i] = InternalValue.create(data); val.discard(); } } catch (RepositoryException e) { String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " value=" + val; log.error(msg, e); throw new IOException(msg); } break; case PropertyType.DOUBLE: try { out.writeDouble(val.getDouble()); } catch (RepositoryException e) { // should never occur throw new IOException("Unexpected error while writing DOUBLE value."); } break; case PropertyType.DECIMAL: try { writeDecimal(out, val.getDecimal()); } catch (RepositoryException e) { // should never occur throw new IOException("Unexpected error while writing DECIMAL value."); } break; case PropertyType.LONG: try { out.writeLong(val.getLong()); } catch (RepositoryException e) { // should never occur throw new IOException("Unexpected error while writing LONG value."); } break; case PropertyType.BOOLEAN: try { out.writeBoolean(val.getBoolean()); } catch (RepositoryException e) { // should never occur throw new IOException("Unexpected error while writing BOOLEAN value."); } break; case PropertyType.NAME: try { writeQName(out, val.getName()); } catch (RepositoryException e) { // should never occur throw new IOException("Unexpected error while writing NAME value."); } break; case PropertyType.WEAKREFERENCE: case PropertyType.REFERENCE: writeID(out, val.getNodeId()); break; default: // because writeUTF(String) has a size limit of 64k, // we're using write(byte[]) instead byte[] bytes = val.toString().getBytes("UTF-8"); out.writeInt(bytes.length); // length of byte[] out.write(bytes); // byte[] } } }
From source file:org.apache.jackrabbit.core.persistence.bundle.util.BundleBinding.java
/** * Serializes a <code>PropertyState</code> to the data output stream * * @param out the output stream/*from w w w.j ava 2s . c o m*/ * @param state the property entry to store * @throws IOException if an I/O error occurs. */ public void writeState(DataOutputStream out, NodePropBundle.PropertyEntry state) throws IOException { // type & mod count out.writeInt(state.getType() | (state.getModCount() << 16)); // multiValued out.writeBoolean(state.isMultiValued()); // definitionId out.writeUTF(state.getPropDefId().toString()); // values InternalValue[] values = state.getValues(); out.writeInt(values.length); // count for (int i = 0; i < values.length; i++) { InternalValue val = values[i]; switch (state.getType()) { case PropertyType.BINARY: try { long size = val.getLength(); if (dataStore != null) { int maxMemorySize = dataStore.getMinRecordLength() - 1; if (size < maxMemorySize) { writeSmallBinary(out, val, state, i); } else { out.writeInt(BINARY_IN_DATA_STORE); val.store(dataStore); out.writeUTF(val.toString()); } break; } // special handling required for binary value: // spool binary value to file in blob store if (size < 0) { log.warn("Blob has negative size. Potential loss of data. " + "id={} idx={}", state.getId(), String.valueOf(i)); out.writeInt(0); values[i] = InternalValue.create(new byte[0]); val.discard(); } else if (size > minBlobSize) { out.writeInt(BINARY_IN_BLOB_STORE); String blobId = state.getBlobId(i); if (blobId == null) { try { InputStream in = val.getStream(); try { blobId = blobStore.createId(state.getId(), i); blobStore.put(blobId, in, size); state.setBlobId(blobId, i); } finally { IOUtils.closeQuietly(in); } } catch (Exception e) { String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " size=" + size; log.error(msg, e); throw new IOException(msg); } try { // replace value instance with value // backed by resource in blob store and delete temp file if (blobStore instanceof ResourceBasedBLOBStore) { values[i] = InternalValue .create(((ResourceBasedBLOBStore) blobStore).getResource(blobId)); } else { values[i] = InternalValue.create(blobStore.get(blobId)); } } catch (Exception e) { log.error("Error while reloading blob. truncating. id=" + state.getId() + " idx=" + i + " size=" + size, e); values[i] = InternalValue.create(new byte[0]); } val.discard(); } // store id of blob as property value out.writeUTF(blobId); // value } else { // delete evt. blob byte[] data = writeSmallBinary(out, val, state, i); // replace value instance with value // backed by resource in blob store and delete temp file values[i] = InternalValue.create(data); val.discard(); } } catch (RepositoryException e) { String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " value=" + val; log.error(msg, e); throw new IOException(msg); } break; case PropertyType.DOUBLE: try { out.writeDouble(val.getDouble()); } catch (RepositoryException e) { // should never occur throw new IOException("Unexpected error while writing DOUBLE value."); } break; case PropertyType.DECIMAL: try { writeDecimal(out, val.getDecimal()); } catch (RepositoryException e) { // should never occur throw new IOException("Unexpected error while writing DECIMAL value."); } break; case PropertyType.LONG: try { out.writeLong(val.getLong()); } catch (RepositoryException e) { // should never occur throw new IOException("Unexpected error while writing LONG value."); } break; case PropertyType.BOOLEAN: try { out.writeBoolean(val.getBoolean()); } catch (RepositoryException e) { // should never occur throw new IOException("Unexpected error while writing BOOLEAN value."); } break; case PropertyType.NAME: try { writeQName(out, val.getName()); } catch (RepositoryException e) { // should never occur throw new IOException("Unexpected error while writing NAME value."); } break; case PropertyType.WEAKREFERENCE: case PropertyType.REFERENCE: writeUUID(out, val.getUUID()); break; default: // because writeUTF(String) has a size limit of 64k, // we're using write(byte[]) instead byte[] bytes = val.toString().getBytes("UTF-8"); out.writeInt(bytes.length); // length of byte[] out.write(bytes); // byte[] } } }
From source file:carnero.cgeo.original.libs.Base.java
public boolean runExternalMap(int application, Activity activity, Resources res, Warning warning, GoogleAnalyticsTracker tracker, Cache cache, Waypoint waypoint, Double latitude, Double longitude) { if (cache == null && waypoint == null && latitude == null && longitude == null) { return false; }//from w w w. ja v a2s .com if (application == mapAppLocus) { // locus try { final Intent intentTest = new Intent(Intent.ACTION_VIEW); intentTest.setData(Uri.parse("menion.points:x")); if (isIntentAvailable(activity, intentTest) == true) { final ArrayList<Waypoint> waypoints = new ArrayList<Waypoint>(); // get only waypoints with coordinates if (cache != null && cache.waypoints != null && cache.waypoints.isEmpty() == false) { for (Waypoint wp : cache.waypoints) { if (wp.latitude != null && wp.longitude != null) { waypoints.add(wp); } } } final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final DataOutputStream dos = new DataOutputStream(baos); dos.writeInt(1); // not used if (cache != null) { if (waypoints == null || waypoints.isEmpty() == true) { dos.writeInt(1); // cache only } else { dos.writeInt((1 + waypoints.size())); // cache and waypoints } } else { dos.writeInt(1); // one waypoint } int icon = -1; if (cache != null) { icon = getIcon(true, cache.type, cache.own, cache.found, cache.disabled || cache.archived); } else if (waypoint != null) { icon = getIcon(false, waypoint.type, false, false, false); } else { icon = getIcon(false, "waypoint", false, false, false); } if (icon > 0) { // load icon Bitmap bitmap = BitmapFactory.decodeResource(res, icon); ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos2); byte[] image = baos2.toByteArray(); dos.writeInt(image.length); dos.write(image); } else { // no icon dos.writeInt(0); // no image } // name if (cache != null && cache.name != null && cache.name.length() > 0) { dos.writeUTF(cache.name); } else if (waypoint != null && waypoint.name != null && waypoint.name.length() > 0) { dos.writeUTF(waypoint.name); } else { dos.writeUTF(""); } // description if (cache != null && cache.geocode != null && cache.geocode.length() > 0) { dos.writeUTF(cache.geocode.toUpperCase()); } else if (waypoint != null && waypoint.lookup != null && waypoint.lookup.length() > 0) { dos.writeUTF(waypoint.lookup.toUpperCase()); } else { dos.writeUTF(""); } // additional data :: keyword, button title, package, activity, data name, data content if (cache != null && cache.geocode != null && cache.geocode.length() > 0) { dos.writeUTF("intent;c:geo;carnero.cgeo;carnero.cgeo.cgeodetail;geocode;" + cache.geocode); } else if (waypoint != null && waypoint.id != null && waypoint.id > 0) { dos.writeUTF("intent;c:geo;carnero.cgeo;carnero.cgeo.cgeowaypoint;id;" + waypoint.id); } else { dos.writeUTF(""); } if (cache != null && cache.latitude != null && cache.longitude != null) { dos.writeDouble(cache.latitude); // latitude dos.writeDouble(cache.longitude); // longitude } else if (waypoint != null && waypoint.latitude != null && waypoint.longitude != null) { dos.writeDouble(waypoint.latitude); // latitude dos.writeDouble(waypoint.longitude); // longitude } else { dos.writeDouble(latitude); // latitude dos.writeDouble(longitude); // longitude } // cache waypoints if (waypoints != null && waypoints.isEmpty() == false) { for (Waypoint wp : waypoints) { if (wp == null || wp.latitude == null || wp.longitude == null) { continue; } final int wpIcon = getIcon(false, wp.type, false, false, false); if (wpIcon > 0) { // load icon Bitmap bitmap = BitmapFactory.decodeResource(res, wpIcon); ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos2); byte[] image = baos2.toByteArray(); dos.writeInt(image.length); dos.write(image); } else { // no icon dos.writeInt(0); // no image } // name if (wp.lookup != null && wp.lookup.length() > 0) { dos.writeUTF(wp.lookup.toUpperCase()); } else { dos.writeUTF(""); } // description if (wp.name != null && wp.name.length() > 0) { dos.writeUTF(wp.name); } else { dos.writeUTF(""); } // additional data :: keyword, button title, package, activity, data name, data content if (wp.id != null && wp.id > 0) { dos.writeUTF("intent;c:geo;carnero.cgeo;carnero.cgeo.cgeowaypoint;id;" + wp.id); } else { dos.writeUTF(""); } dos.writeDouble(wp.latitude); // latitude dos.writeDouble(wp.longitude); // longitude } } final Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse("menion.points:data")); intent.putExtra("data", baos.toByteArray()); activity.startActivity(intent); sendAnal(activity, tracker, "/external/locus"); return true; } } catch (Exception e) { // nothing } } if (application == mapAppRmaps) { // rmaps try { final Intent intent = new Intent("com.robert.maps.action.SHOW_POINTS"); if (isIntentAvailable(activity, intent) == true) { final ArrayList<String> locations = new ArrayList<String>(); if (cache != null && cache.latitude != null && cache.longitude != null) { locations.add(String.format((Locale) null, "%.6f", cache.latitude) + "," + String.format((Locale) null, "%.6f", cache.longitude) + ";" + cache.geocode + ";" + cache.name); } else if (waypoint != null && waypoint.latitude != null && waypoint.longitude != null) { locations.add(String.format((Locale) null, "%.6f", waypoint.latitude) + "," + String.format((Locale) null, "%.6f", waypoint.longitude) + ";" + waypoint.lookup + ";" + waypoint.name); } intent.putStringArrayListExtra("locations", locations); activity.startActivity(intent); sendAnal(activity, tracker, "/external/rmaps"); return true; } } catch (Exception e) { // nothing } } if (application == mapAppAny) { // fallback try { if (cache != null && cache.latitude != null && cache.longitude != null) { activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("geo:" + cache.latitude + "," + cache.longitude))); // INFO: q parameter works with Google Maps, but breaks cooperation with all other apps } else if (waypoint != null && waypoint.latitude != null && waypoint.longitude != null) { activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("geo:" + waypoint.latitude + "," + waypoint.longitude))); // INFO: q parameter works with Google Maps, but breaks cooperation with all other apps } sendAnal(activity, tracker, "/external/native/maps"); return true; } catch (Exception e) { // nothing } } Log.i(Settings.tag, "cgBase.runExternalMap: No maps application available."); if (warning != null && res != null) { warning.showToast(res.getString(R.string.err_application_no)); } return false; }