} // materializeMainSchemaTable() private DocumentSource createDocumentSource() { Debug.WriteLine("JsonDataContext.createDocumentSource()"); NInputStream input_stream = _resource.read(); try { // MappingJsonFactory jsonFactory = new MappingJsonFactory(); NJsonParser parser = new NJsonParser(input_stream); // jsonFactory.createParser(inputStream); logger.debug("Created JSON parser for resource: {0}", _resource); return(new JsonDocumentSource(parser, _resource.getName())); } catch (Exception e) { Debug.WriteLine("Unexpected error while creating JSON parser \n " + e.Message); try { Debug.WriteLine("Trying to close input_stream"); FileHelper.safeClose(input_stream); } catch (Exception e1) { throw new MetaModelException("Tried to close input_stream\n " + e1.Message); } throw new MetaModelException("... Unexpected error while creating JSON parser \n " + e.Message); } } // createDocumentSource()
/** * Method invoked by the Java serialization framework while deserializing * Row instances. Since previous versions of MetaModel did not use a * DataSetHeader, but had a reference to a List<SelectItem>, this * deserialization is particularly tricky. We check if the items variable is * there, and if it is, we convert it to a header instead. * * @param stream * @throws Exception */ private void readObject(Object o, NInputStream stream) // throws Exception { XmlSerializer serializer = new XmlSerializer(o.GetType()); object read_data = serializer.Deserialize(stream); //.readFields(); FieldInfo[] fields = read_data.GetType().GetFields(); FieldInfo items_field = NTypeUtils.getField(read_data.GetType(), "_items"); NGetField get_field = new NGetField(read_data, fields); try { // backwards compatible deserialization, convert items to header Object items = items_field.GetValue(read_data); //@SuppressWarnings("unchecked") List <SelectItem> itemsList = (List <SelectItem>)items; SimpleDataSetHeader header = new SimpleDataSetHeader(itemsList); FieldInfo field = NTypeUtils.getField(GetType(), "_header"); // field.setAccessible(true); field.SetValue(this, header); } catch (ArgumentException e) { // no backwards compatible deserialization needed. setWhileDeserializing(get_field, "_header"); } setWhileDeserializing(get_field, "_values"); setWhileDeserializing(get_field, "_styles"); } // readObject()
} // getLastModified() // @Override public override NInputStream read() // throws ResourceException { if (_file.isDirectory()) { return(new DirectoryInputStream(_file.Path, _file.Mode, this)); } NInputStream in_stream = FileHelper.getInputStream((NInputStream)_file); return(in_stream); } // read()
public NJsonParser(NInputStream input_stream) { Debug.WriteLine("new NJsonParser()"); byte[] data = input_stream.readFile(); _data_str = ""; foreach (byte b in data) { _data_str += (char)b; } //parse(_data_str); } // constructor
} // getOutputStream() public static NInputStream getInputStream(NInputStream stream_arg) // throws IllegalStateException { //try //{ // return new BufferedInputStream(new FileInputStream(stream_arg)); //} //catch (FileNotFoundException e) //{ // throw new InvalidOperationException(e.Message); //} //return new NInputStream(stream_arg.SafeFileHandle, FileAccess.Read); return(stream_arg); } // getInputStream()
} // available() private bool openNextFile() // throws IOException { if (_currentInputStream != null) { FileHelper.safeClose(_currentInputStream); _currentInputStream = null; } _currentFileIndex++; if (_currentFileIndex >= _files.Length) { return(false); } _currentInputStream = openStream(_currentFileIndex); return(true); } // openNextFile()
} // getBufferedReader public static void copy(NInputStream from_stream, NOutputStream to_stream) // throws IllegalStateException { try { byte[] buffer = new byte[1024 * 32]; int offset = 0; for (int value = from_stream.Read(buffer, offset++, 1); value != -1; value = from_stream.Read(buffer, offset++, 1)) { to_stream.Write(buffer, 0, value); } } catch (IOException e) { throw new InvalidOperationException(e.Message); } }
public void read(NAction <NInputStream> readCallback) { NInputStream in_stream = read(); try { readCallback.run(in_stream); } catch (Exception e) { throw new NResourceException("Error occurred in read callback", e); } finally { FileHelper.safeClose(in_stream); } } // read()
} // read() public E read <E>(NFunc <NInputStream, E> readCallback) { NInputStream in_stream = read(); try { E result = readCallback.eval(in_stream); return(result); } catch (Exception e) { throw new NResourceException("Error occurred in read callback", e); } finally { FileHelper.safeClose(in_stream); } } // read()
public static void copy(Resource from_res, Resource to_res) // throws IllegalStateException { Debug.Assert(from_res.isExists()); NInputStream in_stream = from_res.read(); try { NOutputStream out_stream = to_res.write(); try { copy(in_stream, out_stream); } finally { safeClose(out_stream); } } finally { safeClose(in_stream); } } // copy()
} // getInputStream() public static byte[] readAsBytes(NInputStream input_stream) { //ByteArrayOutputStream baos = new ByteArrayOutputStream(); MemoryStream memory_stream = new MemoryStream(); StreamWriter baos = new StreamWriter(memory_stream); StreamReader reader = new StreamReader(input_stream); try { copy(reader, baos); } finally { safeClose(input_stream); } string output_str = baos.ToString(); byte[] result = new byte[output_str.Length]; for (int i = 0; i < output_str.Length; i++) { result[i] = (byte)output_str[i]; } return(result); } // readAsBytes()
public JsonDataContext(NInputStream input_stream) : this(new FileResource(input_stream)) { Debug.WriteLine("new JsonDataContext(NInputStream)"); } // constructor
public static String readFileAsString(NInputStream in_stream, String encoding) // throws IllegalStateException { StreamReader br = getReader(in_stream, encoding); return(readAsString(br)); } // readFileAsString()
} // getBufferedWriter() public static StreamReader getBufferedReader(NInputStream inputStream, String encoding) // throws IllegalStateException { StreamReader reader = getReader(inputStream, encoding); return(reader); } // getBufferedReader()
} // getReader() public static String readFileAsString(NInputStream stream_arg) // throws IllegalStateException { return(readFileAsString(stream_arg, DEFAULT_ENCODING)); }
} // getWriter() //public static StreamWriter getWriter(NOutputStream file, String encoding) // throws IllegalStateException //{ // return getWriter(file, encoding, false); //} // getWriter() public static StreamReader getReader(NInputStream input_stream, String encoding_arg) // throws IllegalStateException { Encoding encoding_obj = null; try { if (encoding_arg == null || encoding_arg.ToLower().IndexOf("utf") != -1) { byte[] bom = new byte[4]; int unread; // auto-detect byte-order-mark //@SuppressWarnings("resource") NInputStream pushbackInputStream = new NInputStream(input_stream.SafeFileHandle, FileAccess.Read, bom.Length); int n = pushbackInputStream.Read(bom, 0, bom.Length); // Read ahead four bytes and check for BOM marks. if ((bom[0] == (byte)0xEF) && (bom[1] == (byte)0xBB) && (bom[2] == (byte)0xBF)) { encoding_arg = "UTF-8"; unread = n - 3; encoding_obj = Encoding.UTF8; } else if ((bom[0] == (byte)0xFE) && (bom[1] == (byte)0xFF)) { encoding_arg = "UTF-16BE"; unread = n - 2; } else if ((bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE)) { encoding_arg = "UTF-16LE"; unread = n - 2; } else if ((bom[0] == (byte)0x00) && (bom[1] == (byte)0x00) && (bom[2] == (byte)0xFE) && (bom[3] == (byte)0xFF)) { encoding_arg = "UTF-32BE"; encoding_obj = Encoding.UTF32; unread = n - 4; } else if ((bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) && (bom[2] == (byte)0x00) && (bom[3] == (byte)0x00)) { encoding_obj = Encoding.UTF32; encoding_arg = "UTF-32LE"; unread = n - 4; } else { unread = n; } if (unread > 0) { //pushbackInputStream.unread(bom, (n - unread), unread); } else if (unread < -1) { //pushbackInputStream.unread(bom, 0, 0); } input_stream = pushbackInputStream; } StreamReader inputStreamReader; if (encoding_arg == null) { inputStreamReader = new StreamReader(input_stream); } else { if (encoding_obj != null) { inputStreamReader = new StreamReader(input_stream, encoding_obj); } else { throw new IOException("getReader(): Encoding not available"); } } return(inputStreamReader); } catch (IOException e) { throw new InvalidOperationException(e.Message); } } // getReader()
} // writeStringAsFile() public static StreamReader getBufferedReader(NInputStream stream_arg) // throws IllegalStateException { return(getBufferedReader(stream_arg, DEFAULT_ENCODING)); } // getBufferedReader()