1 / 34

Garbage Collector

Garbage Collector. Mecanism prin care JVM (Java Virtual Machine) isi realizeaza managementul memoriei protected void finalize () throws Throwable ; // Met. Invocata de GC la eliminare. Are scopul de a realiza actiuni explicite inainte de eliminarea obiectului

suzy
Télécharger la présentation

Garbage Collector

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Garbage Collector Mecanism prin care JVM (Java Virtual Machine) isi realizeaza managementul memoriei protected void finalize() throws Throwable; // Met. Invocata de GC la eliminare. Are scopul de a realiza actiuni explicite inainte de eliminarea obiectului public static void gc(); // Lansarea voluntara a GC pana la finalizarea tuturor obiectelor fara referinta public static void runFinalization(); // lansarea metodei finalize() pt. obiectele candidat (fara referinta) nefinalizate inca (nu toate). Reda controlul apelatorului dupa finalizare (vezi exemplu)

  2. Fisiere si fluxuri de date

  3. FISIERE public class File extends Object implements Serializable, Comparable File(String cale); File(String dir, String fis); File(File dir,String fis); public String getPath(); public String getAbsolutePath(); public String getCanonicalPath() throws IOException; public boolean exists(); public boolean isDirectory(); public boolean isFile(); public long length(); public String[] list(); boolean canRead(); boolean canWrite(); boolean delete(); public File[] listFiles(); boolean renameTo(File numeNou); (Exp - TraversareRecursiva) (Exp - ComparatorDirectoare)

  4. public class RandomAccessFile extends Object implements DataOutput, DataInput, Closeable RandomAccessFile(String fis,String mod) throws FileNotFoundException; // mod: “r”,”rw” RandomAccessFile(File fis,String mod) throws FileNotFoundException; long getFilePointer() throws IOException; long length() throws IOException; void seek(long poz) throws IOException; int read() throws IOException; int read(byte dst[]) throws IOException; int read(byte dst[],int index,int lung) throws IOException; void write(int b) throws IOException; void write(byte srs[]) throws IOException; void write(byte srs[],int index,int lung) throws IOException; void close() throws IOException;

  5. Filtrarea fisierelor public final class FileNameExtensionFilter extends FileFilter public FileNameExtensionFilter(String description, String... extensions); // Descrierea asociata filtrarii si extensiile public boolean accept(File f); // intoarce true daca extensia fisierului coincide cu una din extensiile filtrului sau daca f este un director apartinator public String getDescription(); // intoarce descrierea asociata public String[] getExtensions(); //intoarce lista extensiilor asociate public class JFileChooser extends JComponent implements Accessible public JFileChooser() public JFileChooser(String currentDirectoryPath) public JFileChooser(File currentDirectory)

  6. public File getCurrentDirectory() public void setFileFilter(FileFilter filter) public FileFilter getFileFilter() public File getSelectedFile() public void setSelectedFile(File file) public File[] getSelectedFiles() public void setSelectedFiles(File[] selectedFiles) public int showOpenDialog(Component parent) throws HeadlessException public int showSaveDialog(Component parent) throws HeadlessException; Rezultat: •JFileChooser.CANCEL_OPTION •JFileChooser.APPROVE_OPTION •JFileChooser.ERROR_OPTION HeadlessException - Apare când un cod dependent de un echipament este apelat într-un mediu care nu contine echipamentul.

  7. Ierarhia claselor de fluxuri binare – fluxuri de intrare (intrare) InputStream ByteArrayInputStream FileInputStream FilterInputStream BufferedInputStream DataInputStream (implements DataInput) LineNumberInputStream PushbackInputStream ObjectInputStream (implements ObjectInput) PipedInputStream SequenceInputStream StringBufferInputStream

  8. public abstract class InputStream extends Objectimplements Closeable • public int available() throws IOException • public void close() throws IOException • public abstract int read() throws IOException • public int read(byte[] b) throws IOException • public int read(byte[] b, int off, int len) throws IOException

  9. public class FileInputStream extends InputStream FileInputStream(String cale) throws FileNotFoundException; FileInputStream(File fisier) throws FileNotFoundException; int read() throws IOException; int read(byte dst[]) throws IOException; int read(byte dst[],int index,int lung) throws IOException; int available() throws IOException; void close() throws IOException; long skip(long nrOcteti) throws IOException;

  10. Interfaţa DataInput - Citirea datelor structurate public void readFully(byte[] b, int off, int len) throws IOException public void readFully(byte[] b) throws IOException public boolean readBoolean() throws IOException public byte readByte() throws IOException public int readUnsignedByte() throws IOException public short readShort() throws IOException public int readUnsignedShort() throws IOException public char readChar() throws IOException public int readInt() throws IOException public long readLong() throws IOException public float readFloat() throws IOException public double readDouble() throws IOException public StringreadLine() throws IOException public StringreadUTF() throws IOException public int skipBytes(int n) throws IOException

  11. public class DataInputStream extends FilterInputStream implements DataInput protected InputStream in; Public DataInputStream(InputStream fluxIntrare); public static final StringreadUTF(DataInput in) throws IOException

  12. Ierarhia claselor de fluxuri binare – fluxuri de iesire (scriere) OutputStream ByteArrayOutputStream FileOutputStream FilterOutputStream BufferedOutputStream DataOutputStream (implements DataOutput) PrintStream ObjectOutputStream (implements ObjectOutput) PipedOutputStream

  13. public abstract class OutputStream extends Objectimplements Closeable, Flushable • public abstract void write(int b) throws IOException • public void write(byte[] b) throws IOException • public void write(byte[] b, int off, int len) throws IOException • public void flush() throws IOException • public void close() throws IOException

  14. public class FileOutputStream extendsOutputStream FileOutputStream(String cale) throws FileNotFoundException; FileOutputStream(File fisier) throws FileNotFoundException; void write(int b) throws IOException; void write(byte srs[]) throws IOException; void write(byte srs[],int index,int lung) throws IOException; void close() throws IOException; public void flush() throws IOException

  15. public class DataOutputStream extends FilterOutputStream implements DataOutput protected OutputStream out protected int written; public DataOutputStream(OutputStream fluxIesire); public final int size();

  16. Ierarhia claselor de fluxuri caracter – fluxuri de intrare (citire) Unitatea transerabila este caracterul unicode. Sunt fluxuri sincrone care folosesc un obiect lock pentru sincronizare. Reader BufferedReader LineNumberReader CharArrayReader FilterReader InputStreamReader FileReader PipedReader StringReader

  17. Ierarhia claselor de fluxuri caracter – fluxuri de iesire (scriere) Writer BufferedWriter CharArrayWriter FilterWriter OutputStreamWriter FileWriter PipedWriter PrintWriter StringWriter

  18. public abstract class Readerextends Object Campuri: • protected Object lock ; // obiect folosit in sincronizare Metode: • public int read() throws IOException • public int read(char[] cbuf) throws IOException • public abstract int read(char[] cbuf, int off, int len) throws IOException • public boolean ready() throws IOException

  19. public class FileReader extends InputStreamReader FileReader(File fis) throws FileNotFoundException; FileReader(String fis) throws FileNotFoundException;

  20. public class InputStreamReaderextends Reader Este un flux de legatura cu fluxurile binare. Legatura se face prin codificare. Fata de fluxurile character contine in plus informatii de codificare: public StringgetEncoding() Constructorii primesc si un parametru de tip Charset reprezentand codificarea: public InputStreamReader(InputStream in) public InputStreamReader(InputStream in, String charsetName) throws UnsupportedEncodingException public InputStreamReader(InputStream in, Charset cs) public InputStreamReader(InputStream in, CharsetDecoder dec)

  21. public abstract class Writer extends Objectimplements Appendable, Closeable, Flushable • protected Object lock; // sincronizare - idem Reader • public void write(int c) throws IOException • public void write(char[] cbuf) throws IOException • public abstract void write(char[] cbuf, int off, int len) throws IOException • public void write(String str) throws IOException • public void write(String str, int off, int len) throws IOException • public Writerappend(char c) throws IOException • public Writerappend(CharSequence csq, int start, int end) throws IOException

  22. public class FileWriter extends Writer FileWriter(File fis) throws FileNotFoundException; FileWriter(String fis) throws FileNotFoundException; void write(int ch) throws IOException; void write(byte srs[],int index,int lung) throws IOException; void write(String srs,int index,int lung) throws IOException;

  23. public class OutputStreamWriter extends Writer Flux de legatura ca si InputStreamReader. public StringgetEncoding(); // intoarce tipul de codare public OutputStreamWriter(OutputStream out) public OutputStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException public OutputStreamWriter(OutputStream out, Charset cs) public OutputStreamWriter(OutputStream out, CharsetDecoder dec)

  24. Fluxuri de nivel superior pentru manipulare linii (citeste/scrie o linie - pana la intalnirea caracterelor de sfarsit de linie la citire, sau scriind caracterele de sfarsit de linie la scriere) BufferedReader  BufferedReader(Reader flux); BufferedReader(Reader flux, int marime); String readLine() throws IOException; BufferedWriter BufferedWriter(Writer flux); BufferedWriter(Writer flux,int marime); void newLine() throws IOException;

  25. PrintWriter Flux de nivel superior pentru scriere date structurate in fisiere text. PrintWriter(Writer flux); PrintWriter(Writer flux, boolean autoFlush); - trimitere automata a liniei PrintWriter(OutputStream flux); PrintWriter(OutputStream flux, boolean autoFlush); void print(...); void println(...);

  26. Intrări/ieșiri la nivel scăzutJava NIO Java NIO oferă o alternativă de nivel scăzut pentru operațiunile de IO Java NIO folosește în principal clase de tip Buffer și Channel pentru citirea/scrierea datelor Java NIO permite realizarea unor operațiuni de intrare/ieșire asincrone Implementează conceptul de selector. Un obiect selector poate monitoriza canale multiple pe un singur fir de execuție

  27. Implementări Channel Un obiect Channelreprezintă o conexiune deschisă la o entitate, cum ar fi un fişier, un socket, sau orice componentă capabila de a efectua operaţii de intrare/iesire, citire/scriere Diferențe Channel-Stream: obiectul Channel este bidirecțional , Stream este unidirecțional obiectul Channel scrie/citește asincron Implementări Channel: FileChannel - citire/scriere din/în fișiere DatagramChannel - citire/scriere din/în rețea utilizând UDP SocketChannel - citire/scriere din/în rețea utilizând TCP ServerSocketChannel - permite crearea unui server TCP pentru citiri/scrieri în rețea

  28. FileChannel Obtinerea obiectelor Channel prin metodele open definite in Channel public static FileChannel open(Path path, OpenOption... options) throws IOException unde options pot fi APPEND, CREATE, CREATE_NEW,... constante definite in StandardOpenOption prin obiectele Stream: FileInputStream, FileOutputStream public FileChannel getChannel() prin obiecte RandomAccessFile public final FileChannel getChannel() Transferul intre obiecte Channel public abstract long transferFrom(ReadableByteChannel src, long position, long count) throws IOException public abstract long transferTo(long position,long count,WritableByteChannel target) throws IOException; // Transfer spre target. position,count se refera la canalul curent

  29. Implementări Buffer ByteBuffer , MappedByteBuffer , CharBuffer , DoubleBuffer , FloatBuffer , IntBuffer , LongBuffer , ShortBuffer Un Buffer este un bloc de memorie in care se pot scrie date care apoi pot fi citite. Proprietatile unui Buffer: capacity - dimensiunea maxima a Buffer-ului position - este indexul următorului element care poate fi citit sau scris la un moment dat limit - este indicele primului element care nu poate fi scris/citit mark - este un marcaj de pozitie retinut pentru reveniri ulterioare 0 <= mark <= position <= limit <= capacity Operațiuni Buffer: 1. Alocare: public static ByteBufferallocate(int capacity)

  30. Metode de scriere/citire 2. Scrierea datelor in Buffer din Channel prin metode read ale clasei FileChannel: public abstract int read(ByteBuffer dst) throws IOException; // intoarce numarul de bytes cititi -> position=limit 3. Scrierea voluntară in buffer: public abstract ByteBufferput(byte b) public abstract ByteBufferput(int index, byte b) 4. Citirea din Buffer si scrierea in Channel: public abstract int write(ByteBuffer src) throws IOException; // intoarce numarul de bytes cititi din src 5. Citirea voluntară din Buffer: public abstract byte get(); //citire de la pozitia curenta public abstract byte get(int index); //citire de la pozitia indicata

  31. Stările Buffer 7. Trecerea Buffer-ului din modul scriere în modul citire: public final Bufferflip(); //limit=position, position=0 8. Stergere Buffer public final Bufferclear(); //position=0, limit=capacity - Pregatire scriere in buffer public final Bufferrewind(); //position=0 public final Buffermark(); // este marcata pozitia curenta, mark=position public final Bufferreset(); // este stabilita pozitia pe marcajul precedent, position=mark

  32. CLASE UTILITAREClasa utilitara StringTokenizer Este folosita pentru formatare de siruri. public StringTokenizer(String str) public StringTokenizer(String str, String delim) public int countTokens() public boolean hasMoreTokens() public StringnextToken() public StringnextToken(String delim) public boolean hasMoreElements() public ObjectnextElement()

  33. Clasa utilitara Scanner Este folosita pentru citire informative structurata dintr-o sursa care poate fi un fisier sau un sir de caractere. public Scanner(File source) throws FileNotFoundException public Scanner(InputStream source) public Scanner(File source) throws FileNotFoundException public Scanner(String source) public ScanneruseDelimiter(String pattern) public boolean hasNext() public boolean hasNext(String pattern) public Stringnext() public Stringnext(String pattern) public boolean hasNextLine() public StringnextLine() public boolean hasNextInt() public int nextInt()

More Related