Wednesday, April 6, 2011

Can I disable FF3 back button cache?

I found out that when pressing back button it gets previous page from browser cache even if I send following headers:

Test1.aspx

Server              ASP.NET Development Server/9.0.0.0
Date         Wed, 24 Mar 2010 17:49:40 GMT
X-AspNet-Version 2.0.50727
Location         Test2.aspx
Cache-Control no-cache, no-store
Pragma         no-cache
Expires         -1
Content-Type text/html; charset=utf-8
Content-Length 189
Connection         Close
From stackoverflow
  • Cache-control and such things only tell browser NOT to save in cache the downloaded stuff (js, css, images, etc.). It does not relate with the History of visited pages.

    You shouldn't try to modify browser's data. Instead, you'd handle events and stop the ones you don't want to happen in your site.

    Sergej Andrejev : What if I have a sensitive data. Say a public vote kiosk. User choses president candidate and then clicks submit. Now next voter goes to kiosk, presses back button and sees what candidate previous user has selected, because browser didn't even try to get latest version from server and displayed his from cache. What should I do then?
    Alfabravo : It means your app behaves wrong. Transactions, sessions and that kind of things are meant to handle restrictions in access to sensitive data. When webapps say "remember to close your session", they mean it! Also, if your app places confidential data in browser's cache, it means you're opening a new security breach. Plain and simple (unless you cipher it!). You can't rely on unauthorized modifications to browser's data (by definition it is on user's side) in order to provide security. Too much like DRM, putting all the security in the same side.
  • expires should be a date+timestamp and cache-control"s "must-revalidata" & "max-age" might help as well?

    Expires: Wed, 11 Jan 1984 05:00:00 GMT
    Cache-Control: no-cache, must-revalidate, max-age=0
    
    Sergej Andrejev : Actually the answer was that simple. But I want to aware others. Browsers don't cache pages, they cache requests. So if you have one page Test.aspx with one link to itself. And user first opens the page, then clicks on the link (here no-cache is added) and then presses back button the page will be retrieved from the cache because first time it was downloaded without no-cache directive
    futtta : well, browsers cache responses, which in general are pages, no? and if i'm not mistaking, "no-cache" will already be in the http response header even upon the first request.

Saving page as PDF or as HTML but with all entries

Hello every one :)

I wont to create a pdf from a form on my page, but the Problem is, I need it excactly like the page with form, all entires.

So I have for example

2 Input Fields, 7 Radio, 2 Checkboxes, and as result i need a PDF with the same sructure, but if someone check the checkbox, it must be saved in pdf.

I have tryed to save the html content of the page on submit, and save it first in html file, but the problem is, my selections woundn't be saved.

The result must have the same as i would print my form.

I hope someone can help.

The Code i using to save the page content.

> $(document).ready(function(){
>       $('input[type=submit]').click( function()   {           
> var formname = $("body").find("form").attr("name");
> var htmldata = $("form[name="+formname+"]").html();
> var enchtmldata = ncodeURIComponent(htmldata);
> $.ajax({  
> type: "post",             
> data: "data="+enchtmldata,
> url: "makepdf.php",           
> success: function()
>   {
>   alert("success");           },
> error: function()             {
>   alert("error");             }           });
> 
> }); });

PS: I using PHP and jQuery

ADDED: I think it is better to try first of all to save the form page as html, but to keep the entries in it. After that to try to convert it. But the Problem is, to save it with all data.

ADDED: how can I add a attr selected to an option field?

From stackoverflow
  • Run this in your browser's javascript command line, copy-paste the result to text editor and save as html file.

    $('body').text($('html').html())
    

    Depending on your browser, this may also work when copy-pasted to address bar:

    javascript:$('body').text($('html').html())
    

    The output is "dynamic" page source, i.e. DOM tree rendered into text.

    Fincha : The Problem is, if I have writen somethink in the field, it wouldn't be saved by using .html() or .text() ... the same is with checkboxes :(
    jholster : You are right, my suggestion doesn't work.
  • What do you use to convert the HTML to PDF? I can recommend TCPDF for that job: http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf

    It can convert HTML with CSS to a PDF form. I haven't tested it myself but I am quite sure that it will even keep the checked option. But for sure you have to create the HTML with PHP to have the checkbox checked.

    <input type="checkbox"<?php echo ($_REQUEST['checkbox_name'])? ' checked="checked"' : '' ?> />
    

    You should than be able to create the correct PDF form with the TCPDF Class.

    Fincha : Hm, this class is very usefull. But first of all, I need to save the html page ast, txt or thml this all inputs and after that, convert it. I using this jquery code, but all i get, is the form without entries
    Kau-Boy : But if you sent the form to itself and that add all the values in the $_REQUEST array to the value attributes of input and textare and select/check all other elements, you'll get the correct form state in the HTML source. Or just try my new suggestion.
    Fincha : hm, i will try it now
    Fincha : I have a problem to do this, a can't change forms :/
    Kau-Boy : You don't have access to the form source code? That's bad. Could you duplicate the form source code as a PDF template and add the form data to that template? That might make it a lot easier.
  • Ok, I think the problem ist, that checking a checkbox in the browser doesn't change the HTML source. So if you want to transfer the HTML via AJAX, you have to "convert" the checked state within the browser to a real source checked state.

    I am not an jQuery expert, but you shoul select all input[type="checkbox"] and test if they are checked. If they are, add an attribute "checked" with the value "checked" to the checkbox. Otherwise remove the checked attribute if it is present.

    The source HTML will than have all the correct checked states. Without this method you might get some the checkboxe that have been preselected with PHP but have been deselected by the user.

    I hope my answer was clear enough, sorry for my english :)

    Fincha : yes that it the problem, i will try to do this
    Fincha : $(this).attr("value", $(this).val()); doesn't works
    Kau-Boy : As I already mentioned, my jQeury knowledge is limited to the basics. I could give you a solution with Prototype. But that's what you have to do: select all input, textarea and select elements. For alls input that are not checkboxes or radio buttons, set the value attribute. For checkboxes and radio buttons and selects check for the "checked" or "selected" state and add a checked or selected attribute to them. For textareas append the value as a text child node to the textarea. But how to solve that with jQuery you might have to ask a jQuery expert.
  • Have you tried placing the page as a background with the variables as conditional overlays? I have achieved this in the past using FPDF. Radio buttons can be placed as an image ontop of the existing screen dump. You will have no problem putting the text in the right place also.

  • Is there any reason you can't just take the form data and send it to a sever process that will store the form data in a database? From there it should be a relatively simple process to populate the HTML form and generate a PDF form from the database.

    Fincha : sorry, no way :(

Securing S3 via your own application

Imagine the following use case:

You have a basecamp style application hosting files with S3. Accounts all have their own files, but stored on S3.

How, therefore, would a developer go about securing files so users of account 1, couldn't somehow get to files of account 2?

We're talking Rails if that's a help.

From stackoverflow
  • If you want to restrict control of those remote resources you could proxy the files through your app. For something like S3 this may defeat the purpose of what you are trying to do, but it would still allow you to keep the data with amazon and restrict access.

    You should be careful with an approach like this as it could cause your ruby thread to block while it is proxying the file, which could become a real problem with the application.

    Jacek Konieczny : It doesn't have to be proxied directly by the RoR. The HTTP server below may be configured to do the proxying and RoR would only provide some kind of security credentials (probably a cookie value to test). This setup may be quite complicated, depends on the HTTP server used, requires access to server configuration and may require extra helpers (like FastCGI authenticator app or some authentication hook). Though, the performance gain may be worth it in some cases.
    Neil Middleton : I wonder if a simple proxy -> server side redirect would provide enough to stop the vast majority of idle surfers rather than going the whole hog...
  • Serve the files using an EC2 Instance

    If you set your S3 bucket to private, then start up an EC2 instance, you could serve your files on S3 via EC2, using the EC2 instance to verify permissions based on your application's rules. Because there is no charge for EC2 to transfer to/from S3 (within the same region), you don't have to double up your bandwidth consumption costs at Amazon.

    Neil Middleton : I'm not really wanting to have a bucket per account as I think this won't scale particularly well...
    John Douthat : you can still keep all your objects in one S3 bucket.
  • I haven't tackled this exact issue. But that doesn't stop me from having an opinion :)

    Check out cancan:

    It allows custom authorization schemes, without too much hassle.

Setting jvmargs in log4j.properties file

Hey

This is a bit of a weird request but I am trying to set some jvmargs in the log4j.properties file. At present I use ant to for example set some args....

jvmarg value="-Dmail.smtp.socketFactory.port=465"

... but it would be great to group a few of these logging relevant arguments into the .properties file. Does anyone know how to do this?

Thanks in advance!

From stackoverflow
  • Log4j is only going to read the properties file after the JVM has already started - that means it can't affect the JVM arguments.

  • If your example is from your actual situation then you can set this value programatically for the java mail...

    The SMTP protocol provider supports the following properties, which may be set in the JavaMail Session object. The properties are always set as strings; the Type column describes how the string is interpreted. For example, use

        props.put("mail.smtp.port", "888");
    

    http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html

    This example above should work for mail.smtp.socketFactory.port also.

    Thomas : This is something we have looked at, but ideally we would like to keep the configuration separate from the code so we can swap in different logging systems easily at a later date... I am trying to set: mail.smtp.port mail.smtp.socketFactory.class mail.smtp.socketFactory.port mail.smtp.socketFactory.fallback Setting them in the ant build file is ok... but not ideal :(
  • If the properties can be added after JVM startup, you could add a property to your properties file that lists all properties that you want to add to the SystemProperties collection, something like:

    # property names of system properties
    systemprops=mail.smtp.port mail.smtp.socketFactory.class
    
    mail.smtp.port=465
    mail.smtp.socketFactory.class=some.class
    

    Your startup code can read the systemprops value, split on whitespace and add the resulting list of properties to the SystemProperties collection while reading the values from your properties collection.

    This way your code does not need to know which properties to add to system props, only that the properties to add are defined by the systemprops property.

Estimate gaussian (mixture) density from a set of weighted samples

Assume I have a set of weighted samples, where each samples has a corresponding weight between 0 and 1. I'd like to estimate the parameters of a gaussian mixture distribution that is biased towards the samples with higher weight. In the usual non-weighted case gaussian mixture estimation is done via the EM algorithm. Does anyone know an implementation (any language is ok) that permits passing weights? If not, does anyone know how to modify the algorithm to account for the weights? If not, can some one give me a hint on how to incorporate the weights in the initial formula of the maximum-log-likelihood formulation of the problem?

Thanks!

From stackoverflow
  • Just a suggestion as no other answers are sent.

    You could use the normal EM with GMM (OpenCV for ex. has many wrappers for many languages) and put some points twice in the cluster you want to have "more weight". That way the EM would consider those points more important. You can remove the extra points later if it does matter.

    Otherwise I think this goes quite extreme mathematics unless you have strong background in advanced statistics.

  • You can calculate a weighted log-Likelihood function; just multiply the every point with it's weight. Note that you need to use the log-Likelihood function for this.

    So your problem reduces to minimizing $-\ln L = \sum_i w_i \ln f(x_i|q)$ (see the Wikipedia article for the original form).

Tell me some Http-streaming tutorial or example.

Could you tell me some http-streaming tutorial or example ( used also by Gmail ) ?

I want to create a simple chat for college project.

Attention: I am talking about Http-streaming and NOT long-polling.

Thanks ;)

From stackoverflow
  • Check here. Some cool intro about Comet. There is also a mention for Jetty and its free chat client but they are in Java not PHP. Will keep looking and post if I find something useful with PHP. Just FYI, jquery has a plugin for comet but I haven't tried it yet.

    xRobot : jquery has a plugin for long-polling. I need a http-streaming example and not long-polling as I write above ;)
    pinaki : I suppose http://ajaxian.com/archives/picomet-simple-comet-library?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+ajaxian+%28Ajaxian+Blog%29 should be fine for you.
    pinaki : Just saw your comment come up. What I know is that long-polling is AJAX, http-streaming is Comet (or server push). Am I getting this wrong?
    pinaki : So i knew wrong :). Just confirmed from wikipedia. Not sure what technology the other link uses though. It says comet and as I just read Comet is an umbrella term for both http-streaming and long polling. Let me know if you manage to find anything on this.
    xRobot : ok.......... :)
  • "Comet" is just an umbrella term that includes HTTP streaming.

    For some good examples, you can check out our WebSync demos.

    For some tutorials on using it, you can check out the tutorials.

Speed up :visible:input selector avoiding filter

I have a jQuery selector that is running way too slow on my unfortunately large page:

$("#section").find(":visible:input").filter(":first").focus();

Is there a quicker way to select the first visible input without having to find ALL the visible inputs and then filtering THAT selection for the first? I want something like :visible:input:first but that doesn't seem to work.

[Edit] Here's the basic idea of what #section looks like:

<div id="section">
    <div>
        Some text <input type="text">
    </div>
    <div>
        etc. etc. <input type="text">
    </div>
</div>
From stackoverflow
  • $(":input:visible:first", "#section").focus();
    

    If you first filter for the type of control you avoid checking the :visible on all the #section's elements.

    It seems like you need only to catch the first input type="text" visible.
    This should be a bit faster.

    $("input[type='text']:visible:first", "#section").focus();
    
    Nick Craver : This is a different selector, `:input` != `input`
    Alex Bagnolini : Fixed, thank you.
    macca1 : Thanks, this update is about 80ms faster. However this selector is still costing 156ms in IE6. I feel like this should be a quick 16ms lookup or less. Is there any other way to optimizer this?
    macca1 : I'm accepting the first part of this answer. Seems like the fastest way possible of doing this. Thanks!
  • How about adding class="default_field" to the default field for each page, then using $('.default_field').focus();?

    How easy this is to do depends on your server-side technology of course, but the advantages are that it takes the processing burden off of the client (which is extra important for IE6), and it also gives you the flexibility to choose a default input other than the very first one on pages where it's appropriate.

How do I do this with the user agent?

If the user-agent includes the word "myapp", then alert('hi');

BTW, I am using JQuery.

From stackoverflow
  • if (navigator.userAgent.indexOf("myapp") !== -1) {
      alert("hi");
    }
    

    I recommend you don't do user-agent sniffing.

    Justin Johnson : +1 For the recommendation against sniffing. For the rest of the class: Instead of user-agent sniffing, *feature detection* should be used to determine the type of browser that you are dealing with.

iTunes Connect rejects my binary because I used a pre-release version of the SDK, what should I do?

I downloaded a pre-release version of the iPhone SDK and tried to update one of my existing apps using a binary I built with it. Obviously you are not supposed to do this but I had forgotten about the warning when I installed the pre-release SDK. Anyway - I have two questions:

  1. Can I simply set the base SDK to an earlier version in the build settings and get around this problem?

  2. If not, then what should I do?

From stackoverflow
  • You need to download the release version of Xcode with the release iPhone SDK. You can't use the SDK downloaded from the iPad beta version for any release products.

    Just go to http://developer.apple.com/iphone/index.action and click on iPhone SDK 3.1 and download it. Then build your app bundle with that and submit it to Apple.

    bartvdpoel : You can install the SDK's in two different directories. So you can use both the beta stuff and the production stuff. Works like a charm.
  • Before you download a pre-release version of the SDK from developer.apple.com, there are several prominent warnings, one of which clearly states that pre-release versions of Xcode / iPhone SDK can NOT be used to build production binaries. So the answer to 1. is an emphatic no.

    As far as I can tell, the only option to be able to build production binaries that iTunes Connect will accept is to delete the pre-release version of Xcode / iPhone SDK and re-install the older stable version.

  • If you upgraded using the beta download (as opposed to installing on a fresh box that has never seen SDK or Xcode), set the base SDK to a released version of the OS. Compile a Distribution build, submit to the app store. The old SDK is still there, so that will get used. All of the prominent warnings I have seen state the the SDK cannot be used, without mention of Xcode.

    Before anyone scoffs, note that I submitted an app on March 13 using the version of Xcode (3.2.2) included in Beta 4, setting Base SDK to 3.1. It was approved on the 15th. I've also done this during the 3.0 beta for 2.x apps.

    EDIT: on the other hand, acceptance apparently isn't always guaranteed (note that this person had also submitted an app that did get accepted): http://stackoverflow.com/questions/2466399/the-binary-you-uploaded-was-invalid-a-pre-release-beta-version-of-the-sdk-was-us

    Prairiedogg : I've actually had a similar experience. I submitted a binary with an earlier pre-release and had it accepted. However I think things may have changed with the most recent pre-release, iPhone OS 3.2 beta 5, which is the one that gave a rejection today. Either that or they added stricter validation on the server side.

JSON: Jackson stream parser - is it really worth it?

I'm making pretty heavy use of JSON parsing in an app I'm writing. Most of what I have done is already implemented using Android's built in JSONObject library (is it json-lib?).

JSONObject appears to create instances of absolutely everything in the JSON string... even if I don't end up using all of them.

My app currently runs pretty well, even on a G1.

My question is this: are the speed and memory benefits from using a stream parser like Jackson worth all the trouble?

By trouble, I mean this: As far as I can tell, there are three downsides to using Jackson instead of the built in library:

  1. Dependency on an external library. This makes your .apk bigger in the end. Not a huge deal.
  2. Your app is more fragile. Since the parsing is not done automatically, it is more vulnerable to changes in the JSON text that it's parsing (perhaps I'm wrong about this).
  3. Writing code to parse JSON via a stream parser is ugly and tedious.
From stackoverflow
  • Guess you've pretty much answered your own question. :)
    Using the built-in JSON parser myself and have never looked for an alternative.

  • I think question is whether built-in one is good enough. If it is, sure, minimizing dependencies is often a good strategy. Good enough can refer to both efficiency and ease of use.

    For what it's worth, Jackson also has a decent tree model as well as full data binding. Tree model is significantly faster than default parser (parsing is 3x-5x faster on J2SE, probably similarly on other platforms like Android, tree model itself is probably more efficient as well). Or: if you don't want dependency to second jar (mapper is needed for tree model and data binding), writing your own tree to cover your use cases is simple too. Either use basic HashMap/ArrayList/wrappers, or have your own classes if you prefer. Builder would be maybe 40 lines of code top.

Update table of two different database

This is the query to update table in one database that is of other

Update
  test.temp a,
  test2.temp b
Set a.name=b.name
Where a.no=b.no;

Now I don't want to write every field i.e a.name=b.name

Is there any solution?

From stackoverflow
  • This link might help.

Is it possible to call API's from the provided url using curl

i hv to call API's from a given url in my code. I try to call url using curl. now i want to use APi's provided by this url in my code

From stackoverflow
  • A web service API typically just communicates over the HTTP protocol. You send some specific requests and receive answers in a certain format, the same thing your browser does with websites. cURL is a great tool to do so.

    The specifics will really depend on the API in question, you should study whatever documentation they provide for it.

  • If you are working with APIS that are based on xml and support the soap protocol a better idea will be just to use soap then curl (less code needed)

when i moved from FW3.5 to FW2.0 i got error

hi

i have buield my project on VS2008 under FrameWork 3.5

now i need to move to FrameWork 2.0

when i done this, i got this error:

Error 1 The type or namespace name 'TypedTableBase' does not exist in the namespace 'System.Data' (are you missing an assembly reference?) C:\Documents and Settings\xxx\Desktop\demo2005\demo\MyDB.Designer.cs 391 68 demo

in this line:

 public partial class DO1TblDataTable : global::System.Data.TypedTableBase<DO1TblRow> {

i think that it something with the crystal-report

thank's in advance

From stackoverflow
  • The Typed Dataset generator was changed in .Net 3.5 to inherit the table classes from TypedTableBase<TRow>. (This adds LINQ support by implementing IEnumerable<T>)

    You need to right-click the typed dataset and click Run Custom Tool to regenerate it using the .Net 2.0 code generator.

    Gold : where i can find "Run Custom Tool" ?
    SLaks : Right-click the typed dataset in Solution Explorer.
    Gold : thank's !!! it's work !!!

Access denied at webservice

Hi,

I have a winforms, and it connecting wit webservice.

Webservice has method which create folder

Directory.Create(path);

Webservice is at company server, and this folder must me create at another disc in out company.

When I invoke this method, i get exception "Access denied to path..."

When webservice was running at my computer everything was ok.

I have full access to this network disc.

But how to set full access to my company server??

When I check this method:

[WebMethod]
        public string GetNameOfUser()
        {
            return WindowsIdentity.GetCurrent().Name;
        }

I get <string>NT AUTHORITY\NETWORK SERVICE</string>

Rest of webmethods without directory instructions working properly

Edited Structure

Computer A is in domain and there is running win forms

Server A : there is running webservice

Network disc: there are folders with important files for winforms application

yes, we have a domain.Computer A has access to network disc.

From stackoverflow
  • As I understand, your application tries to create a folder which resides on a different server than the web application.

    Your web application is running under a local account NT AUTHORITY\NETWORK SERVICE. In order to access a network share you need it to be running under an account available on both machines, for example a domain account.

    If both the web server and the server with the network share are on a domain, you can:

    1. Create a domain account
    2. Grant the account write access to the parent folder where you want to create new folders
    3. Make the Application Pool on your web server run under the domain account

    This will should let your web application create foldera on the other server.

    : Could you give any tips how to deal with point 3 ?? I have no idea
    Mikael Svenson : It's a setting on IIS. If you are using IIS6 check out http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/f05a7c2b-36b0-4b6e-ac7c-662700081f25.mspx?mfr=true. For IIS7 check out http://technet.microsoft.com/en-us/library/cc771170(WS.10).aspx
  • When you were running your web-app on your own machine, it worked onder your own account, therefore it could create directories.

    On the server the app runs under an account with very low rights. If you want to create those new directories below a specific fixed directory, you can allow that server account to modify that particular directory.

JQuery: Problem in getting element's width in Chrome

Hello,

Suppose I have this image:

<img src="images/01.jpg" border="0" rel="shadow" />

Then with JQuery, I get its width using:

$('[rel="shadow"]').width();

Firefox and IE report correct dimensions of the image eg 140px while Chrome reports 0. How to solve this problem?

Note:

I don't want to set explicit width for images eg:

<img src="images/01.jpg" border="0" rel="shadow" width="140" />

So, how to get width in cross-browser way which is not defined in width attribute of elements?

Thanks

From stackoverflow
  • $(window).load(
        function() {
            alert($('img').width());
        }
    );
    

    This will work: Test case. Basically it will wait until images load before executing the code (when the ready function fires the document has been fully loaded but images haven't).

    Sarfraz : @Andreas Bonini: Perfect, load never popped up in my mind, Thanks :)

Database connection via Hibernate in servlets

What is the best place in servlet for Hibernate code that returns HibernateSessionFactory ?

I saw many examples: ones put db connection in service methods. Others - use smth like HibernateUtil (Singleton) that returns HibernateSessionFactory.

I don't know is it safe to use HibernateUtil in multithreaded Servlets ?

From stackoverflow
  • Usually, you should use an MVC framework in favor of Servlets directly, but that's not your question, and I'm going to assume you have a good reason to be implementing your own Servlets. On to the answer...

    Per this - https://www.hibernate.org/hib_docs/v3/api/org/hibernate/SessionFactory.html:

    Implementors must be threadsafe.

    and

    SessionFactorys are immutable. The behaviour of a SessionFactory is controlled by properties supplied at configuration time. These properties are defined on Environment.

    So it's OK to share an instance of SessionFactorys.

    In fact, from my experience, your HibernateUtil approach is the better approach, as SessionFactory creation can be very expensive.

    blackliteon : What DBCP You are using ? I see 2 options: cp3o & tomcat CP.
    blackliteon : About MVC: I use GWT as frontend, so I dont need to use MVC
    whaley : just because GWT is a front end doesn't mean you should eschew using MVC as a pattern on the server side. Servlets are really best served as a Controller. Your servlets really ought to be delegating data access to another set of classes of yours that use hibernate. Your servlets should be completely agnostic to the fact that hibernate even exists.
    Jack Leow : @blackliteon I'd recommend setting up data sources within your servlet container (say Tomcat), and accessing it via JNDI. You can find some examples here - http://tomcat.apache.org/tomcat-4.1-doc/jndi-datasource-examples-howto.html
    Jack Leow : Also, I agree with whaley, your servlets (I assume these are GWT RPC servlets) should serve as "controllers". Your Hibernate stuff should be hidden behind DAOs, and ideally, you'd have a layer (sometimes called "managers" or "facades") between the two.
    blackliteon : @Javk Leow: Tomcat 5.5 DBCP documentation: http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html
  • Use the Open Session in View pattern (see the filter implementation).

Writing to a file in Unicode

I am having some problems writing to a file in unicode inside my c program. I am trying to write a unicode Japanese string to a file. When I go to check the file though it is empty. If I try a non-unicode string it works just fine. What am I doing wrong?

setlocale(LC_CTYPE, "");
FILE* f;
f = _wfopen(COMMON_FILE_PATH,L"w");
fwprintf(f,L"日本語");
fclose(f);

Oh about my system: I am running Windows. And my IDE is Visual Studio 2008.

From stackoverflow
  • Doing the same with fopen() works for me here. I'm using Mac OS X, so I don't have _wfopen(); assuming _wfopen() isn't returning bad stuff to you, your code should work.

    Edit: I tested on cygwin, too - it also seems to work fine.

    Lefteris : Didn't work for me :( , _wfopen returns a normal FILE* pointer. I am running Windows. And my IDE is Visual Studio 2008.
  • I cannot find a reference to _wfopen on either of my boxes, I however don't see why opening it with fopen should cause a problem, all you need is a file pointer.

    What matters is if or not C recognizes the internal Unicode's values and pushes those binary values to the file properly.

    Try just using fopen as Carl suggested, it should work properly.

    Edit: if it still doesn't work you may try defining the characters as their integer values and pushing them with fwprintf(), I know that's cumbersome and not a good fix in the long run, but it should work as well.

  • You might need to add the encoding to the mode. Possibly this:

    f = _wfopen(COMMON_FILE_PATH,L"w, ccs=UTF-16LE");
    
    Lefteris : Thanks .. this worked. But that creates an additional question. I actually wanted this so that I could be able to write unicode to files generally. Japanese was just an example. This will work for all unicode supported languages, correct?
    Mark Wilkins : Correct. It should not be language dependent.

SQL Server - How to display master details data in columns

I have two tables, to be concise let’s call them TableA and TableB. This is the schema:

TableA
ID – int
Name varchar(50)

TableB
ID – int
TableA_Fk – int
Value varchar(50)

Each record in table A can have at most 9 records in table B. I want to be able to retrieve the data in a columnar form:

TableA-Name, TableB-Value1, … TableB-Value9

Is this possible using queries? Thanks!

From stackoverflow
  • You will require a LEFT JOIN and a PIVOT table

  • You could do something like:

    SELECT rank() OVER (ORDER BY tableA_FK) as rank, tableA_fk, value
    INTO #temp
    FROM TableB b
    ORDER BY rank 
    
    
    SELECT a.Name, 
           CASE WHEN t.rank = 1 THEN t.Value ELSE NULL END AS TableB-Value1,
           CASE WHEN t.rank = 2 THEN t.Value ELSE NULL END AS TableB-Value2,
           CASE WHEN t.rank = 3 THEN t.Value ELSE NULL END AS TableB-Value3,
           .... (etc.)
    FROM TableA a
    INNER JOIN #temp t ON a.Id = t.tableA_fk
    

    You need Sql Server 2005 or up. Sorry, but I don't have Sql Server (or the time) to test this well. Hope this gives you an idea and helps.

    OMG Ponies : You'll want to partition by the fk rather than ORDER
  • This should do it, in addition to be DBRM independant.

    SELECT A.Name
        , SUM(CASE WHEN B.Value = 1 THEN 1 ELSE NULL END) AS B_Value_1
        , SUM(CASE WHEN B.Value = 2 THEN 2 ELSE NULL END) AS B_Value_2
        , SUM(CASE WHEN B.Value = 3 THEN 3 ELSE NULL END) AS B_Value_3
        , SUM(CASE WHEN B.Value = 4 THEN 4 ELSE NULL END) AS B_Value_4
        , SUM(CASE WHEN B.Value = 5 THEN 5 ELSE NULL END) AS B_Value_5
        , SUM(CASE WHEN B.Value = 6 THEN 6 ELSE NULL END) AS B_Value_6
        , SUM(CASE WHEN B.Value = 7 THEN 7 ELSE NULL END) AS B_Value_7
        , SUM(CASE WHEN B.Value = 8 THEN 8 ELSE NULL END) AS B_Value_8
        , SUM(CASE WHEN B.Value = 9 THEN 9 ELSE NULL END) AS B_Value_9
    FROM A
        INNER JOIN B ON B.TableA_FK = A.ID
    GROUP BY A.Name
    ORDER BY A.Name
    

Why is it a bad practice to call System.gc?

After answering to a question about how to force-free objects in Java (the guy was clearing a 1.5GB HashMap) with System.gc(), I've been told it's a bad practice to call System.gc() manually, but the comments seemed mitigated about it. So much that no one dared to upvote it, nor downvote it.

I've been told there it's a bad practice, but then I've also been told garbage collector runs don't systematically stop the world anymore, and that it could also be only seen as a hint, so I'm kind of at loss.

I do understand that usually the JVM knows better than you when it needs to reclaim memory. I also understand that worrying about a few kilobytes of data is silly. And I also understand that even megabytes of data isn't what it was a few years back. But still, 1.5 gigabyte? And you know there's like 1.5 GB of data hanging around in memory; it's not like it's a shot in the dark. Is System.gc() systematically bad, or is there some point at which it becomes okay?

So the question is actually double:

  • Why is it or not a bad practice to call System.gc()? Is it really a hint under certain implementations, or is it always a full collection cycle? Are there really garbage collector implementations that can do their work without stopping the world? Please shed some light over the various assertions people have made.
  • Where's the threshold? Is it never a good idea to call System.gc(), or are there times when it's acceptable? If any, what are those times?
From stackoverflow
  • The reason everyone always says to avoid System.gc() is that it is a pretty good indicator of fundamentally broken code. Any code that depends on it for correctness is certainly broken; any that rely on it for performance are most likely broken.

    You don't know what sort of garbage collector you are running under. There are certainly some that do not "stop the world" as you assert, but some JVMs aren't that smart or for various reasons (perhaps they are on a phone?) don't do it. You don't know what it's going to do.

    Also, it's not guaranteed to do anything. The JVM may just entirely ignore your request.

    The combination of "you don't know what it will do," "you don't know if it will even help," and "you shouldn't need to call it anyway" are why people are so forceful in saying that generally you shouldn't call it. I think it's a case of "if you need to ask whether you should be using this, you shouldn't"

    EDIT to address a few concerns from the other thread:

    After reading the thread you linked, there's a few more things I'd like to point out. First, someone suggested that calling gc() may return memory to the system. That's certainly not necessarily true - the Java heap itself grows independently of Java allocations.

    As in, the JVM will hold memory (many tens of megabytes) and grow the heap as necessary. It doesn't necessarily return that memory to the system even when you free Java objects; it is perfectly free to hold on to the allocated memory to use for future Java allocations.

    To show that it's possible that System.gc() does nothing, view:

    http://bugs.sun.com/view_bug.do?bug_id=6668279

    and in particular that there's a -XX:DisableExplicitGC VM option.

    zneak : How can code depend on garbage collection for its correctness? Also, can I infer that your answer to question 2 would be "it's never a good thing"?
    Steven Schlansker : You may be able to construct some weird Rube Goldberg-esque setup where the method in which the GC is run affects the correctness of your code. Perhaps it's masking some strange threading interaction, or maybe a finalizer has a significant effect on the running of the program. I'm not entirely sure it's possible but it may be, so I figured I'd mention it.
    Martin : @zneak you might for example have put critical code in finalizers (which is fundamentally broken code)
    Steven Schlansker : As for my direct answer to number 2, I'd say that the rule is that it's generally not worth your time. There are exceptions to every rule, though. I'm simply trying to explain the thought process. On a recent Sun JVM it probably won't cause much harm, but when you start moving to other JVMs it may not do what you expect anymore. That's why I'd in general avoid it.
    zneak : @Steve & @Martin: I didn't think about critical code in finalizers. It's been so long since I've written one I almost forgot about them. Thanks for reminding.
    Joachim Sauer : I'd like to add that there are a few corner cases where `System.gc()` is useful and might even be necessary. For example in UI applications on Windows it can greatly speed up the restoring-process of a Window when you call System.gc() *before* you minimize the Window (especially when it stays minimized for quite some time and parts of the process get swapped to disk).
  • Calling System.gc doesn't mean the GC is run. It's on its own thread, and it runs according to its own lights.

    As far as stopping the world goes, I think the newer generational models do a better job than their 1.0 incarnations.

    I'll repeat it: calling System.gc() does NOT run the garbage collector. That's been true since Java 1.0.

    zneak : Javadoc doesn't agree with you though: _When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects._ That being said, you didn't answer on if it's a good practice or not, and if there ever is a good time to call it.
    duffymo : It didn't define "best effort". It might have done absolutely nothing. Not a good practice, not now, not ever. I've never written code to call it, ever.
    zneak : So you mean it's a bad practice because it's unreliable and can potentially do nothing? Under which circumstances can it hurt your code? And, on a side note, I think "best efforts to recycle _all discarded objects_" should at least mean it's trying to.
    Steven Schlansker : See the links I put in my answer for the "best effort" claim...
    Stephen C : @duffymo - "calling System.gc() does NOT run the garbage collector" - that statement is not strictly correct. A more correct statement is "calling System.gc() **does not necessarily** run the garbage collector." The actual behavior is controlled by an -XX option ... at least what the documentation says.
    duffymo : It's not the calling of the GC that might do nothing that's a bad practice; it's writing code that makes you think that you can and should call it that's a bad sign. I'm saying that regardless of what the documentation says, I've never had to call System.gc. Ever. Is your app running on an app server? That's what handles threading in Java EE apps.
  • Yes, calling System.gc() doesn't guarantee that it will run, it's a request to the JVM that may be ignored. From the docs:

    Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects

    It's almost always a bad idea to call it because the automatic memory management usually knows better than you when to gc. It will do so when its internal pool of free memory is low, or if the OS requests some memory be handed back.

    It might be acceptable to call System.gc() if you know that it helps. By that I mean you've thoroughly tested and measured the behaviour of both scenarios on the deployment platform, and you can show it helps. Be aware though that the gc isn't easily predictable - it may help on one run and hurt on another.

    zneak : But also from the Javadoc: _When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects, which I see as a more imperative form of what you've posted about. Screw that, there's a bug report about it being misleading. As of which knows better, what are the harms of hinting the JVM?
    Steven Schlansker : Nothing at all, until you hint it incorrectly ;-)
    tom : The harm is that doing collection at the wrong time can be a huge slow down. The hint you are giving is probably a bad one. As for "best effort" comment, try it and see in a tool like JConsole. Sometimes clicking the "Perform GC" button does nothing
  • It has already been explained that calling system.gc() may do nothing, and that any code that "needs" the garbage collector to run is broken.

    However, the real reason that it is bad practice to call System.gc() is that it is inefficient. And in the worst case, it is horribly inefficient! Let me explain.

    A typical GC algorithm identifies garbage by traversing all non-garbage objects in the heap, and inferring that any object not visited must be garbage. From this, we can model the total work of of a garbage collection consists of one part that is proportional to the amount of live data, and another part that is proportional to the amount of garbage; i.e. work = (live * W1 + garbage * W2).

    Now suppose that you do the following in a single-threaded application.

    System.gc(); System.gc();
    

    The first call will (we predict) do (live * W1 + garbage * W2) work, and get rid of the outstanding garbage.

    The second call will do (live* W1 + 0 * W2) work and reclaim nothing. In other words we have done (live * W1) work and achieved absolutely nothing.

    We can model the efficiency of the collector as the amount of work needed to collect a unit of garbage; i.e. efficiency = (live * W1 + garbage * W2) / garbage. So to make the GC as efficient as possible, we need to maximize the value of garbage when we run the GC; i.e. wait until the heap is full. (And also, make the heap as big as possible. But that is a separate topic.)

    If the application does not interfere (by calling System.gc()), the GC will wait until the heap is full before running, resulting in efficient collection of garbage. But if the application forces the GC to run, the chances are that the heap won't be full, and the result will be that garbage is collected inefficiently. And the more often the application forces GC, the more inefficient the GC becomes.

    Note: the above explanation glosses over the fact that a typical modern GC partitions the heap into "spaces", the GC may dynamically expand the heap, the application's working set of non-garbage objects may vary and so on. Even so, the same basic principal applies across the board to all true garbage collectors. It is inefficient to force the GC to run.

    (I'm also excluding memory managers that use reference counting exclusively, but no current Java implementation uses that approach ... for good reason.)

    sleske : +1 Good explanation. Note however that this reasoning only applies if you care about throughput. If you want to optimize latentcy at specific points, forcing GC may make sense. E.g. (hypothetically speaking) in a game you might want to avoid delays during levels, but you don't care about delays during level load. Then it would make sense to force GC after level load. It does decrease overall throughput, but that's not what you are optimizing.
  • People have been doing a good job explaining why NOT to use, so I will tell you a couple situations where you should use it:

    (The following comments apply to Hotspot running on Linux with the CMS collector, where I feel confident saying that System.gc() does in fact always invoke a full garbage collection).

    JT : 1) After the initial work of starting up your application, you may be a terrible state of memory usage. Half your tenured generation could be full of garbage, meaning that you are that much closer to your first CMS. In applications where that matters, it is not a bad idea to call System.gc() to "reset" your heap to the starting state of live data.
    JT : 2) Along the same lines as #1, if you monitor your heap usage closely, you want to have an accurate reading of what your baseline memory usage is. If the first 2 minutes of your application's uptime is all initialization, your data is going to be messed up unless you force (ahem... "suggest") the full gc up front.
    JT : 3) You may have an application that is designed to never promote anything to the tenured generation while it is running. But maybe you need to initialize some data up-front that is not-so-huge as to automatically get moved to the tenured generation. Unless you call System.gc() after everything is set up, your data could sit in the new generation until the time comes for it to get promoted. All of a sudden your super-duper low-latency, low-GC application gets hit with a HUGE (relatively speaking, of course) latency penalty for promoting those objects during normal operations.
    JT : 4) It is sometimes useful to have a System.gc call available in a production application for verifying the existence of a memory leak. If you know that the set of live data at time X should exist in a certain ratio to the set of live data at time Y, then it could be useful to call System.gc() a time X and time Y and compare memory usage.
    zneak : Can't you edit your message? Important contents should be there, not in the comments.
  • Maybe I write crappy code, but I've come to realize that clicking the trash-can icon on eclipse and netbeans IDEs is a 'good practice'.

  • It's NOT a bad practice.

    But keep in mind that when you call System.gc() you have no guarantee that garbage collection will really be run. It's up to the JVM to decide if it follow your "suggestion" System.gc() .

    From the API:

    Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

    zneak : This quote from the Javadoc is so ambiguous considering the last sentence that it shouldn't be allowed to exist.
    Bruno Rothgiesser : zneak, I don't think that it's ambiguous, but the phrase "the JVM has made it's best effort" is definetily vague.
  • GC efficiency relies on a number of heuristics. For instance, a common heuristic is that write accesses to objects usually occur on objects which were created not long ago. Another is that many objects are very short-lived (some objects will be used for a long time, but many will be discarded a few microseconds after their creation).

    Calling System.gc() is like kicking the GC. It means: "all those carefully tuned parameters, those smart organizations, all the effort you just put into allocating and managing the objects such that things go smoothly, well, just drop the whole lot, and start from scratch". It may improve performance, but most of the time it just degrades performance.

    To use System.gc() reliably(*) you need to know how the GC operates in all its fine details. Such details tend to change quite a bit if you use a JVM from another vendor, or the next version from the same vendor, or the same JVM but with slightly different command-line options. So it is rarely a good idea, unless you want to address a specific issue in which you control all those parameters. Hence the notion of "bad practice": that's not forbidden, the method exists, but it rarely pays off.

    (*) I am talking about efficiency here. System.gc() will never break a correct Java program. It will neither conjure extra memory that the JVM could not have obtained otherwise: before throwing an OutOfMemoryError, the JVM does the job of System.gc(), even if as a last resort.

    sleske : +1 for mentioning that System.gc() does not prevent OutOfMemoryError. Some people believe this.
  • Lots of people seem to be telling you not to do this. I disagree. If, after a large loading process like loading a level, you believe that:

    1. You have a lot of objects that are unreachable and may not have been gc'ed. and
    2. You think the user could put up with a small slowdown at this point

    there is no harm in calling System.gc(). I look at it like the c/c++ inline keyword. It's just a hint to the gc that you, the developer, have decided that time/performance is not as important as it usually is and that some of it could be used reclaiming memory.

    Advice to not rely on it doing anything is correct. Don't rely on it working, but giving the hint that now is an acceptable time to collect if perfectly fine. I'd rather waste time at a point in the code where it doesn't matter (loading screen) than when the user is actively interacting with the program (like during a level of a game.)

    There is one time when i will force collection: when attempting to find out is a particular object leaks (either native code or large, complex callback interaction. Oh and any UI component that so much as glances at Matlab.) This should never be used in production code.

    sleske : +1 for GC while analyzing for mem leaks. Note that the information about heap usage (Runtime.freeMemory() et al.) is really only meaningful after forcing a GC, otherwise it would depend on when the system last bothered to run a GC.
  • If JVM is on edge of OutOfMemoryError, it will run the GC at any way. If that didn't help (and your code thus dies with an OOME), then either the code simply requires more memory, or the code is simply memory-inefficient. Run a profiler to find that out.

    In a nut: calling System#gc() has no value. Let the GC do its work transparently, it's terribly good at it.

  • In my experience, using System.gc() is effectively a platform-specific form of optimization (where "platform" is the combination of hardware architecture, OS, JVM version and possible more runtime parameters such as RAM available), because its behaviour, while roughly predictable on a specific platform, can (and will) vary considerably between platforms.

    Yes, there are situations where System.gc() will improve (perceived) performance. On example is if delays are tolerable in some parts of your app, but not in others (the game example cited above, where you want GC to happen at the start of a level, not during the level).

    However, whether it will help or hurt (or do nothing) is highly dependent on the platform (as defined above).

    So I think it is valid as a last-resort platform-specific optimization (i.e. if other performance optimizations are not enough). But you should never call it just because you believe it might help(without specific benchmarks), because chances are it will not.