How to avoid string copy when spliting the string?

Hi all,
I have some old APIs (which I do not have control over) that expects the caller to pass the "content" of a text file as either String or String[] (each element is a line in the text file). Since the text file can be huge, I want to save memory and try to only hold 1 copy of the file content in memory. So, the question boils down to how can I make a String variable and a String[] variable share one single piece of memory.
After digging into the source code, I notice the String.substring(...) always makes allocate a new memory space for the returning string if the result substring is smaller than 1/4 of the actual String. The String.split() method internally also invokes the String.substring(...) method.
This creates a problem for me. If I first load the content of a file into a String "a". Then I call "String[] array = a.split("\n")", as the result, each element in the resulting array will be a copy of the substring of the original String "a" (since each line in my text file will be smaller than 1/4 of the entire file). StringTokenizer seems to have the same problem. Is there anything else I could do?
I really would like to extends the String class and override the substring method to make it always "share" the same memory content. But since String class is declared as final, I cannot do this. I think maybe the String.split() method implementation should make the return String[] from the split function share the same memory space as the original string. Any workaround anyone can suggest?
Thanks
-- Gary

While you could try to create some sort of custom class, if you want something that is already in java, you need to use one of the instantiations of CharSequence
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/CharSequence.html
Personally, I would recommend CharBuffer. From the look of it, it seems most like what you want.
Hi all,
I have some old APIs (which I do not have control
rol over) that expects the caller to pass the
"content" of a text file as either String or String[]
(each element is a line in the text file). Since the
text file can be huge, I want to save memory and try
to only hold 1 copy of the file content in memory.
So, the question boils down to how can I make a
a String variable and a String[] variable share one
single piece of memory.
After digging into the source code, I notice the
the String.substring(...) always makes allocate a new
memory space for the returning string if the result
substring is smaller than 1/4 of the actual String.
The String.split() method internally also invokes
s the String.substring(...) method.
This creates a problem for me. If I first load
oad the content of a file into a String "a". Then I
call "String[] array = a.split("\n")", as the result,
each element in the resulting array will be a copy of
the substring of the original String "a" (since each
line in my text file will be smaller than 1/4 of the
entire file). StringTokenizer seems to have the same
problem. Is there anything else I could do?
I really would like to extends the String class and
nd override the substring method to make it always
"share" the same memory content. But since String
class is declared as final, I cannot do this. I
think maybe the String.split() method implementation
should make the return String[] from the split
function share the same memory space as the original
string. Any workaround anyone can suggest?
Thanks
-- Gary

Similar Messages

  • How to avoid launching Labview when invoking VI by Teststand

    Hi, friends.
    I'm a learner of teststand. I wrote some VIs by Labview. Now I want to invoke them in teststand environment. But when I run the sequence. Labview will be automatically launched. How to avoid launching Labview when I run the sequence?
    Thanks.
    Solved!
    Go to Solution.

    I'm not sure that I follow what you are saying..  But it sounds like when the VI is called, the LabVIEW development software is started and you want to avoid that.
    If you create an executable or dll and call that from within TestStand, it should not call the development environment.  You would need to have the LabVIEW run-time engine installed on the target PC. 
    It has been many years since I've used TestStand with LV, so my memory may not be accurate.  I think the above had worked for me.  However, it seems that I recall something about having the LV code in debug mode causing it to open the development environment.... but it's a distant memory.    You're not using breakpoints where the LV VI is being called, are you?
    R

  • How not allow music start when conect the iphone5 to other device by using Bluetooth?

    How not allow music start when conect the iphone5 to other device by using Bluetooth?

    Hi Ralph,
    Ty for answer. I connect my iPhone5 to my car audio system by Bluetooth. I can listen sounds from any app installed on my iPhone. The music app starts immediatly, without any command, when the connection via Bluetooth is done. It disturbs me because I want to hear sound from another application. At this moment I do not want to listen to music. I stop the music but it return again and again every time the other app stop momentarily to talk. (This is a navigation app)
    I would like to know how to configure my iPhone5 to avoid this.
    Kind regards
    Edilson

  • Does anyone Know how to fix error 0xe8000065 when connecting the ipod touch 4g

    does anyone Know how to fix error 0xe8000065 when connecting the ipod touch 4g

    See:
    iPhone, iPad, iPod touch: Unknown error containing '0xE' when connecting

  • How to hide system tables when using the Oracle SQL Developer?

    Hi,
    I would like to know how can I show only the tables that I created under the Tables tree? I didnt find a way to create a separate database using the Oracle Sql Developer. I see all the tables together, and would like to differentiate between different databases.
    Can anyone explain to me how to do these things?
    Thanks,

    Hi,
    I would like to know how can I show only the tables that I created under the Tables tree? Your posting is not clear,again tell something more on tables tree,what u want to achieve with it.
    How to hide system tables when using the Oracle SQL Developer? if u connected with sys, system or user with dba role then u have a privilege to see these tables,so revoke the privilege/role from ur user to view this tables if ur connected other then sys,system,
    I didnt find a way to create a separate database using the Oracle Sql Developer. DBCA is a tool for creating the new database.
    Kuljeet

  • How to avoid data repetation when using select statements with innerjoin

    how to avoid data repetation when using select statements with innerjoin.
    thanks in advance,
    satheesh

    you can use a query like this...
      SELECT DISTINCT
             frg~prc_group1                  "Product Group 1
             frg~prc_group2                  "Product Group 2
             frg~prc_group3                  "Product Group 3
             frg~prc_group4                  "Product Group 4
             frg~prc_group5                  "Product Group 5
             prc~product_id                  "Product ID
             txt~short_text                  "Product Description
    UP TO 10 ROWS
    INTO TABLE l_i_data
    FROM
    Joining CRMM_PR_SALESG and
    COMM_PR_FRG_ROD
    crmm_pr_salesg AS frg
    INNER JOIN comm_pr_frg_rod AS prd
    ON frgfrg_guid = prdfragment_guid
    Joining COMM_PRODUCT and
    COMM_PR_FRG_ROD
    INNER JOIN comm_product AS prc
    ON prdproduct_guid = prcproduct_guid
    Joining COMM_PRSHTEXT and
    COMM_PR_FRG_ROD
    INNER JOIN comm_prshtext AS txt
    ON prdproduct_guid = txtproduct_guid
    WHERE frg~prc_group1 IN r_zprc_group1
       AND frg~prc_group2 IN r_zprc_group2
       AND frg~prc_group3 IN r_zprc_group3
       AND frg~prc_group4 IN r_zprc_group4
       AND frg~prc_group5 IN r_zprc_group5.
    reward it it helps
    Edited by: Apan Kumar Motilal on Jun 24, 2008 1:57 PM

  • How to raise mail notification when ever the new event is added to calendar list in sharepoint 2010?

    Hi experts, I need guidance or the way how we can raise mail,
    When ever the new item is added / updated / deleted / cancelled to a calendar list in SharePoint 2010.
    Shall we achieve it through using SharePoint designer 2010.
    If its help to fix the requirement.
    Thanks and Regards,
    Dhayanand Kalimidi

    >I need to send the mail before 1 week from the date
    This is different requirement. Initially you said you want send email when item is added / updated / deleted in list. For this you can use default alert to send instant email as  suggested before. 
    Now if you want to send reminder email then you can use designer WF and calculate the due date by using variable. Refer this link for your ref:
    http://mysharepointchronicles.wordpress.com/2012/11/05/sharepoint-list-with-workflow-email-reminder-set-to-send-30-days-from-created-date/
    http://social.msdn.microsoft.com/Forums/sharepoint/en-US/3211ee00-92de-47f1-b611-4c9609f92ab7/how-do-i-set-sharepoint-2010-to-send-a-reminder-email-a-month-after-a-date-already-listed?forum=sharepointdevelopmentlegacy
    Remember event receiver can not send reminder email or scheduled email so for this either use designer WF or create timer job.
    Hope it could help
    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

  • I have an interactive CD-Rom as part of my Italian language text, and have loaded it onto the iMac.  How can I also copy it to the iPad?

    I have an interactive CD-Rom as part of my Italian language text, and have loaded it onto the iMac.  How can I also copy it to the iPad?

    You can only install apps from the iTunes app store on your computer and the App Store app directly on the iPad. Does the maker of the CD have an app in the store with the CD contents already in it ?

  • How to avoid safety warning when opening a pdf from an link in another pdf?

    I make pdf in Acrobat with links to other pdf files av have made. When i try to click a link to open the pdf, i get the safety question if trust it or if i will block it. How to avoid that question all the time?

    Hello Kvalitetetsansvarlig
    Try adding the PDF to the trust location in Acrobat
    Edit -> Preferences-> Security (Enhanced), Click on "Add File", and browse to your file.
    Click on "OK" button and check.
    Regards,
    Deepak

  • How to Avoid another Black X Over the Battery?

    Forgive me if this is already a topic. If so, please direct me to the correct thread.
    I was recently working on my Late 2010 Macbook Pro 13 inch laptop when it just shutdown. It took about 5 minutes to start back up, and I immidatley noticed that there was a black X over the battery symbol, and the fan was running at a full (and a very loud) speed. I shut it down and plugged in the power cord, which only glowed very faintly.
    After some googleing, I figured out what was wrong and fixed it using a different thread. I basically had to restart the SMC. The thread I used is here: http://support.apple.com/kb/HT3964
    However, even though it helped me fix the problem, it didn't answer any questions. So, if you could please help me. Why did this happen to my Mac? How did resetting the SMC fix the problem? How can I avoid this problem in the future? Is this an issue with the battery, or is it something else?
    I want to know what I did to make this happen and how to avoid it in the future, if it's possible. All input is appreciated. I am not very computer savy when it comes to the inner workings, so I'm hoping someone smarter than me can help me understand what just happened with my computer.
    Thanks!

    An SMC reset can 'fix' several battery issues -
    A portable Mac doesn't appear to respond properly when you close or open the lid.
    The computer sleeps or shuts down unexpectedly.
    The battery does not appear to be charging properly.
    The MagSafe power adaptor LED doesn't appear to indicate the correct activity
    - but I've no idea why you had your problem in the first place, so I can't tell you how to avoid it in the future. Maybe you let your battery drain completely down? I don't know - but now you know the 'fix' for it should it happen again.
    Clinton

  • Ken Burns: how to avoid cutting some part of the photo ?

    I like the ken Burns effect.
    Unfortunately on some photo, the face of the subject are cutted using that effect.
    There's a way to 'mitigate' that or, better, how to setup the photo (i use Aperture) to avoid that problem ?

    thanks for the clarification
    hoping not being too ot: i use aperture, may you please explain how to create a new album without create new master photo (to save space on my disk) and use the photo resized, as said to 16:9, on my apple tv ?
    if i don't create new master photo and create new version when syncronized the new version photo i loose 16:9 formatting on apple tv (hope is clear ).

  • How to avoid clear record when tab pages changes

    Hi All
    I am using oracle forms 10g and db 10g.
    I have created a form with four tab pages. Namely "EXPENSE" , "AMOUNT_DETAILS", "SUPPLIER" , "ACCOUNT".
    When i enter a data in page 1 ie Expense and move to next page page2 "AMOUNT_DETAILS", and enters data in page3 "SUPPLIER" and when i come back to page1 "EXPENSE" and also Page2 "AMOUNT" the data get cleared in Tab pages. There is no data again i need to enter the data manually.
    Can any one suggest me how to avoid this clear data.
    Thanks & Regards
    Srikkanth

    Hi,
    Thanks once again for your quick response.
    I have checked it , i was working with oracle apps.Now i have entered all the datas in the four tab and press save button in the screen at that time also the data get cleared.Can you please tell is there any work around for this.
    regards
    Srikkanth

  • How to avoid shared locks when validating foreign keys?

    I have a table with a FK and I want to update a row in that table without being blocked by another transaction which is updating the parent row at the same time. Here is an example:
    CREATE TABLE dbo.ParentTable
    PARENT_ID int NOT NULL,
    VALUE varchar(128) NULL,
    CONSTRAINT PK_ParentTable PRIMARY KEY (PARENT_ID)
    GO
    CREATE TABLE dbo.ChildTable
    CHILD_ID int NOT NULL,
    PARENT_ID INT NULL,
    VALUE varchar(128) NULL,
    CONSTRAINT PK_ChildTable PRIMARY KEY (CHILD_ID),
    CONSTRAINT FK_ChildTable__ParentTable FOREIGN KEY (PARENT_ID)
    REFERENCES dbo.ParentTable (PARENT_ID)
    GO
    INSERT INTO ParentTable(PARENT_ID, VALUE)
    VALUES (1, 'Some value');
    INSERT INTO ChildTable(CHILD_ID, PARENT_ID, VALUE)
    VALUES (1, 1, 'Some value');
    GO
    Now I have 2 transactions running at the same time:
    The first transaction:
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;BEGIN TRAN
    UPDATE ParentTable
    SET VALUE = 'Test'
    WHERE PARENT_ID = 1;
    The second transaction:
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;BEGIN TRAN
    UPDATE ChildTable
    SET VALUE = 'Test',
    PARENT_ID = 1
    WHERE CHILD_ID = 1;
    If 'UPDATE ParentTable' statement runs a bit earlier, then 'UPDATE ChildTable' statement is blocked untill the first transaction is committed or rollbacked. It happens because SQL Server acquires shared locks when validating foreign keys, even
    if the transaction is using read uncommitted, read committed snapshot (read committed using row versioning) or snapshot isolation level. I cannot see why change in the ParentTable.VALUE should prevent me from updating ChildTable. Please note that ParentTable.PARENT_ID
    is not changed by the first transaction, which means that from FK's point of view whatevere is set to the ParentTable.VALUE is never a problem for referential integrity. So, such blocking behavior seems to me not logical. Furthermore, it contradicts to the
    MSDN:
    Transactions running at the READ UNCOMMITTED level do not issue shared locks to prevent other transactions from modifying data read by the current transaction. READ UNCOMMITTED transactions are also not blocked by exclusive locks that would prevent the
    current transaction from reading rows that have been modified but not committed by other transactions. 
    Does anybody know how to workaround the issue? In other words, are there any tricks to avoid shared locks when validating foreign keys? (Disabling FK is not an option.) Thank you.
    Alexey

    If you change the primary key of the parent table to be nonclustered, there is no blocking.
    Indeed, when I update ParentTable.VALUE, then:
    in case of PK_ParentTable is clustered, a particular row in the clustered index is locked (request_mode:X, resource_type: KEY)
    in case of PK_ParentTable is non-clustered, a particular physical row in the heap is locked (request_mode:X, resource_type: RID).
    and when I update ChildTable.PARENT_ID, then:
    in case of PK_ParentTable is clustered, this index is used to verify referential integrity:
    in case of PK_ParentTable is non-clustered, again this index is used to verify referential integrity, but this time it is not locked:
    It is important to note that in both cases SQL Server acquires shared locks when validating foreign keys. The principal difference is that in case of clustered PK_ParentTable the request is blocked, while for non-clustered index it is granted.
    Thank you, Erland for the idea and explanations.
    The only thing that upsets me is that this solution cannot be applied, because I don't want to convert PK_ParentTable from clustered to non-clustered just to avoid blocking issues. It is a pity that SQL Server is not smart enough to realize that:
    ParentTable.PARENT_ID is not changed and, as a result, should not be locked
    ChildTable.PARENT_ID is not actually changed either (old value in my example is 1 and the new value is also 1) and, as a result, there is no need at all for validating the foreign key.
    In fact, the problem I described is just a tip of the iceberg. The real challenge is that I have deadlocks because of the FK validation. In reality, the first transaction has an additional statement which updates ChildTable:
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
    BEGIN TRAN
    UPDATE ParentTable
    SET VALUE = 'Test'
    WHERE PARENT_ID = 1;
    UPDATE ChildTable
    SET VALUE = 'Test'
    WHERE PARENT_ID = 1;
    The result is famous message:
    Msg 1205, Level 13, State 51, Line xx
    Transaction (Process ID xx) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
    I know that if I change the order of the two statements, it will solve the deadlock issue. But let's imagine I cannot do it. What are the other options?
    Alexey

  • How to avoid to print decimals in the output

    Hi,
    Please suggest me how to avoid getting decimals in the output.
    The variable that i am using is RKMNG. While declaring it i have used it in this manner,  rkmng type p decimals 0.
    but this only avoids fetching the data with decimals, when i am trying to print this value to the output using ALV FM, the decimals are appearing.
    Please can u provide me how to remove these three decimals that are appearing in the output.
    Thanks,

    do this :
    data : lv_var like i.
    move rkmng to lv_var.
    print lv_var.

  • How to avoid opening photos when connecting a camera

    how can I avoid opening photos when connecting a camera?

    Connect the Camera.
    Launch Image Capture (in the Applications Folder)
    You can set what happens when you connect this device in the lower left of the Image Capture window.

Maybe you are looking for