snipt

Ctrl+h for KB shortcuts

C#

c# / SQL reading varbinary as a stream, or converting to text

1
2
3
4
5
6
7
8
MemoryStream m = new MemoryStream(byteArrayFromDB);
StreamReader file = new StreamReader(m);

// If you need to read the stream out as text/string..
while ((line = file.ReadLine()) != null)
{
string code = line.Substring(line.Length - 12);
}
https://snipt.net/embed/a385f8d8f772cdf16e5e697302f1d7ec/
/raw/a385f8d8f772cdf16e5e697302f1d7ec/
a385f8d8f772cdf16e5e697302f1d7ec
csharp
C#
8
2019-05-20T11:40:55
True
False
False
/api/public/snipt/43355/
c-sql-reading-varbinary-as-a-stream-or-converting-to-text
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">MemoryStream</span> <span class="n">m</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MemoryStream</span><span class="p">(</span><span class="n">byteArrayFromDB</span><span class="p">);</span> </span><span id="L-2"><a name="L-2"></a><span class="n">StreamReader</span> <span class="n">file</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StreamReader</span><span class="p">(</span><span class="n">m</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="c1">// If you need to read the stream out as text/string..</span> </span><span id="L-5"><a name="L-5"></a><span class="k">while</span> <span class="p">((</span><span class="n">line</span> <span class="p">=</span> <span class="n">file</span><span class="p">.</span><span class="n">ReadLine</span><span class="p">())</span> <span class="p">!=</span> <span class="k">null</span><span class="p">)</span> </span><span id="L-6"><a name="L-6"></a><span class="p">{</span> </span><span id="L-7"><a name="L-7"></a><span class="kt">string</span> <span class="n">code</span> <span class="p">=</span> <span class="n">line</span><span class="p">.</span><span class="n">Substring</span><span class="p">(</span><span class="n">line</span><span class="p">.</span><span class="n">Length</span> <span class="p">-</span> <span class="m">12</span><span class="p">);</span> </span><span id="L-8"><a name="L-8"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
binary, c#, sql, stream

Java

Stream to String

1
2
3
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);
String theString = writer.toString();
https://snipt.net/embed/14f145a3a5201607e38146e124cf6585/
/raw/14f145a3a5201607e38146e124cf6585/
14f145a3a5201607e38146e124cf6585
java
Java
3
2019-05-22T11:44:18
True
False
False
/api/public/snipt/40663/
stream-to-string
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">StringWriter</span> <span class="n">writer</span> <span class="o">=</span> <span class="k">new</span> <span class="n">StringWriter</span><span class="o">();</span> </span><span id="L-2"><a name="L-2"></a><span class="n">IOUtils</span><span class="o">.</span><span class="na">copy</span><span class="o">(</span><span class="n">inputStream</span><span class="o">,</span> <span class="n">writer</span><span class="o">,</span> <span class="n">encoding</span><span class="o">);</span> </span><span id="L-3"><a name="L-3"></a><span class="n">String</span> <span class="n">theString</span> <span class="o">=</span> <span class="n">writer</span><span class="o">.</span><span class="na">toString</span><span class="o">();</span> </span></pre></div> </td></tr></table>
java, servlet, stream, string, tomcat

Python

File object for logging loggers

1
2
3
4
5
To get access to underlying stream (filelike) object in Python's logging system:

logger.handlers[0].stream

You'll have to figure out which handler for a logger object you want. I've only got one in my sample code. This snipt allows one to pass this stream object into interfaces that are expecting a file object.
https://snipt.net/embed/1d8dd5f16f3b326195971ffe3b495969/
/raw/1d8dd5f16f3b326195971ffe3b495969/
1d8dd5f16f3b326195971ffe3b495969
python
Python
5
2019-05-21T03:16:45
True
False
False
/api/public/snipt/10867/
file-object-for-logging-loggers
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">To</span> <span class="n">get</span> <span class="n">access</span> <span class="n">to</span> <span class="n">underlying</span> <span class="n">stream</span> <span class="p">(</span><span class="n">filelike</span><span class="p">)</span> <span class="nb">object</span> <span class="ow">in</span> <span class="n">Python</span><span class="s1">&#39;s logging system:</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="n">logger</span><span class="o">.</span><span class="n">handlers</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">stream</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="n">You</span><span class="s1">&#39;ll have to figure out which handler for a logger object you want. I&#39;</span><span class="n">ve</span> <span class="n">only</span> <span class="n">got</span> <span class="n">one</span> <span class="ow">in</span> <span class="n">my</span> <span class="n">sample</span> <span class="n">code</span><span class="o">.</span> <span class="n">This</span> <span class="n">snipt</span> <span class="n">allows</span> <span class="n">one</span> <span class="n">to</span> <span class="k">pass</span> <span class="n">this</span> <span class="n">stream</span> <span class="nb">object</span> <span class="n">into</span> <span class="n">interfaces</span> <span class="n">that</span> <span class="n">are</span> <span class="n">expecting</span> <span class="n">a</span> <span class="nb">file</span> <span class="nb">object</span><span class="o">.</span> </span></pre></div> </td></tr></table>
file, logging, stream